expandlelistview child click - android

I have an expandle listview witha few children. If i click a child i want it to open it own activity. Right now i got it to work to go to 1 activity only. So when for example when i click on Mambo beach i want it to open an activity with the information of Mambo beach. And when i click on Avis car Rental it should open that acitivity
below my example:
public class TodoFragment extends Fragment {
public TodoFragment(){}
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_todo,container, false);
}
#Override
public void onStart() {
super.onStart();
// get the listview
expListView = (ExpandableListView) getView().findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_LONG).show();
return false;
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
//You have to create next activity say NextActivity.java
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding group data
listDataHeader.add("Culture");
listDataHeader.add("Beaches");
listDataHeader.add("Car Rental");
listDataHeader.add("Dinner");
// Adding child data
List<String> culture= new ArrayList<String>();
culture.add("Grotten van Hato");
culture.add("Ostrich Farm");
culture.add("Shete Boka national Park");
culture.add("Landhuis Knip");
culture.add("Christoffelpark");
culture.add("Navy Museum");
culture.add("Post Museum");
List<String> beaches = new ArrayList<String>();
beaches.add("Mambo Beach");
beaches.add("Knip");
beaches.add("Playa Kalki");
beaches.add("Westpunt");
beaches.add("Boca Santa Cruz");
beaches.add("Cas Abao");
beaches.add("Playa PortoMari");
beaches.add("Kontiki Beach");
beaches.add("Jan Thiel Beach");
List<String> car = new ArrayList<String>();
car.add("budget rental");
car.add("Avis Rental");
car.add("Alamo Car Rental");
car.add("Noordstar Rental");
car.add("Europa Rental");
List<String> dinner = new ArrayList<String>();
dinner.add("Truk di Pan");
dinner.add("Burger King");
dinner.add("Punda Food");
// Header, Child data
listDataChild.put(listDataHeader.get(0), culture);
listDataChild.put(listDataHeader.get(1), beaches);
listDataChild.put(listDataHeader.get(2), car);
listDataChild.put(listDataHeader.get(3), dinner);
}
}

You could implement this by simply creating, and launching, a different Intent based on the value of childPosition. For example:
switch (childPosition)
{
case 0:
Intent intentChild0 = new Intent(yourMainActivity.this, ActivityForChild0.class);
startActivity(intentChild0);
break;
case 1:
Intent intentChild1 = new Intent(yourMainActivity.this, ActivityForChild1.class);
startActivity(intentChild1);
break;
//Etc...
default:
break;
}
Adding this to your OnChildClickListener will cause a different Activity to be launched depending on what child was clicked.

There are 2 solutions: one is switch the child position and redirect the activity.But this solution has a problem, because you will have many activities and your project will not be ordered.
The other solution is implement one activity and many fragments depends of how many child you have. In your activity you only redirect the fragment depends of the child id.

Related

FindViewById giving Unreachable statement error in Fragment activity [duplicate]

This question already has answers here:
I get the error "Unreachable statement" return in android
(3 answers)
Closed 5 years ago.
I'm pretty new to Fragment based activities. I'm working on a collapsible listview inside the CollapsingToolbarLayout.
Here, I'm facing Unreachable statement error in java file. Not able to get rid of it.
My code -
public class MenuActivity extends android.support.v4.app.Fragment {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_booktable, container, false);
// get the listview
expListView = (ExpandableListView) container.findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getActivity().getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getActivity().getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getActivity().getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("South Indian");
listDataHeader.add("Quick Bites");
listDataHeader.add("Soups");
// Adding child data
List<String> SouthIndian = new ArrayList<String>();
SouthIndian.add("Kara Bath");
SouthIndian.add("Chow Chow Bath");
SouthIndian.add("2 Idli 1 Vada");
SouthIndian.add("Rava Idli");
SouthIndian.add("Curd Vada");
SouthIndian.add("Masala Dosa");
List<String> QuickBites = new ArrayList<String>();
QuickBites.add("Veg Sandwich");
QuickBites.add("Veg Toast Sandwich");
QuickBites.add("Bread Butter Jam");
QuickBites.add("Bread Jam");
QuickBites.add("Pakoda");
QuickBites.add("Masala Puri");
List<String> Soups = new ArrayList<String>();
Soups.add("Tomato Soup");
Soups.add("Sweet corn Soup");
Soups.add("Veg Soup");
Soups.add("Veg Schezwan Soup");
Soups.add("Veg Noodles Soup");
listDataChild.put(listDataHeader.get(0), SouthIndian); // Header, Child data
listDataChild.put(listDataHeader.get(1), QuickBites);
listDataChild.put(listDataHeader.get(2), Soups);
}
}
How do I fix the error for the line expListView = (ExpandableListView) container.findViewById(R.id.lvExp); and make this code run?
move return inflater.inflate(R.layout.activity_booktable, container, false) to the end of method onCreateView()
Your first line in onCreateView already returns the View so the rest of the code is not executed:
return inflater.inflate(R.layout.activity_booktable, container, false);
You have to replace it with
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.activity_booktable, container, false);
...
return myView;
}
and then better do the findViewById on this view, e.g.
expListView = (ExpandableListView) myView.findViewById(R.id.lvExp);

setAdapter() returning null within ListFragment

So I'm trying to make a basic foreground for another app I'm making.
I'm just trying to get a listView to populate on screen within my fragment. (To create swipe-able tab views)
I keep getting a nullPointerException on this line here:
List.setAdapter(Adapter);
Here's my entire class:
public class List_View extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.listviewtest, container, false);
ListView List = (ListView) getActivity().findViewById(R.id.listView1);
List<HashMap<String, String>> Data = new ArrayList<HashMap<String, String>>();
{
HashMap<String, String> temp1 = new HashMap<String, String>();
temp1.put("Item", "Item 1");
Data.add(temp1);
HashMap<String, String> temp2 = new HashMap<String, String>();
temp2.put("Item", "Item 2");
Data.add(temp2);
HashMap<String, String> temp3 = new HashMap<String, String>();
temp3.put("Item", "Item 3");
Data.add(temp3);
}
SimpleAdapter Adapter = new SimpleAdapter(this.getActivity(),
Data, R.layout.custom_row_view, new String[] {
"Item" }, new int[] { R.id.text1});
List.setAdapter(Adapter);
List.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position) {
case 1:
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(
getActivity());
dlgAlert.setMessage("Set Message Text");
dlgAlert.setTitle("Set Message Title");
dlgAlert.setNeutralButton("View info",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
dlgAlert.setCancelable(false);
dlgAlert.create().show();
break;
}
}
});
return rootView;
}
}
So far as I can tell this shouldn't be returning null..
I'm thinking your inflate operation is returning null. Check this line
ListView List = (ListView) getActivity().findViewById(R.id.listView1);
You should be inflating your ListView from your parent view of your fragment, not from the parent activity.
Should be something like this instead
ListView List = (ListView) rootView.findViewById(R.id.listView1); and make sure that

How to add xml layout as a child item in Expandable ListView

I want to add XML layout (For performing Form submission) as a child item in Expandable listview. I am not able to add using arraylist. Please help me. Thanks in advance .. This is my main Activity
public class MainActivity extends ExpandableListActivity implements
OnChildClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ExpandableListView expandbleLis = getExpandableListView();
expandbleLis.setDividerHeight(2);
expandbleLis.setGroupIndicator(null);
expandbleLis.setClickable(true);
setGroupData();
setChildGroupData();
NewAdapter mNewAdapter = new NewAdapter(groupItem, childItem);
mNewAdapter
.setInflater(
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
this);
getExpandableListView().setAdapter(mNewAdapter);
expandbleLis.setOnChildClickListener(this);
}
public void setGroupData() {
groupItem.add("abcd");
groupItem.add("efgh");
}
ArrayList<String> groupItem = new ArrayList<String>();
ArrayList<Object> childItem = new ArrayList<Object>();
public void setChildGroupData() {
/**
* Add first child items
*/
ArrayList<String> child = new ArrayList<String>();
child.add("Java");
child.add("Drupal");
childItem.add(child);
/**
* Add second child items (here I want to add a xml Layout as a child item for form submission purpose)
*/
child = new ArrayList<String>();
child.add("Android");
child.add("Blackberry");
childItem.add(child);
}
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this, "Clicked On Child",
Toast.LENGTH_SHORT).show();
return true;
}
}

How to use ImageViews with Expandable Lists in Android?

I have an expandable list in android and I can fill it with string easily but now I want to add a drawable on each row of the list (a different one on each row), what's the easiest way to do this with the code I already have? Thanks.
public class Physical extends ExpandableListActivity {
public static final int GROUPS = 1;
public SimpleExpandableListAdapter expListAdapter;
private boolean expanded = true;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.phys_sub);
String [] cats = { "phy1", "phys2" };
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < GROUPS; i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put( "group", "Categories" );
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
for( int n = 0 ; n < cats.length ; n++ ) {
Map<String, String> curChildMap = new HashMap<String, String>();
curChildMap.put( "child", cats[n] );
children.add(curChildMap);
}
childData.add(children);
}
expListAdapter =
new SimpleExpandableListAdapter(
/* context */
this,
/* creates Group list */
groupData,
/*Group item layout XML */
R.layout.group_row,
/* the key of each group element (not child's) */
new String[] { "group" },
/* data under the key goes into this TextView */
new int[] { R.id.group_text },
/* creates Child List (sub-level entries) */
childData,
/* layout for children in list */
R.layout.child_sub,
/* the key of each child element */
new String[] { "child" },
/* data under the child keys go into this textView */
new int[] { R.id.child_text }
);
/* sets up and initializes the adapter for the list */
setListAdapter(expListAdapter);
getExpandableListView().setOnGroupExpandListener( new OnGroupExpandListener() {
public void onGroupExpand(int groupPosition) {
expanded = true;
}
});
getExpandableListView().setOnGroupCollapseListener( new OnGroupCollapseListener() {
public void onGroupCollapse(int groupPosition) {
expanded = false;
}
});
if (expanded != false ) {
getExpandableListView().expandGroup(0);
}
}
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, final int childPosition, long id) {
switch (childPosition) {
case 0:
Intent spine_plus_intent = new Intent(Physical.this, SpinePlus.class);
startActivity(spine_plus_intent);
finish();
break;
}
return true;
}
}
you'd define a custom expandable list adapter and redefine the getchildview / getgroupview.
From there you have 2 routes to follow: one, you can programmatically add the imageviews into the child/group view (i'd discourage you from this approach), or you define an xml layout file for the child/group view and inflate it (and possibly edit the content dynamically)
Simple adapters aren't flexible enough to achieve good results. Expecially when you want custom views for their elements. (Not to mention scalability: always project and code keeping in mind that your requirements may enlarge in the future and a flexible base allows more freedom and prevents you from scratching too much code)

Android - Add item to custom listview on button click

I currently have a custom listview where each item on the list contains two rows of text. What I would like to do is each time a user clicks on the button, it creates a new item on the list with the text that the user inputted. While I know how to get the text, I am having trouble adding a new item to the list view as I simply don't know where to begin.
Here is my code:
public class MainFeedActivity extends ListActivity implements OnClickListener {
View sendButton;
EditText postTextField;
TextView currentTimeTextView, mainPostTextView;
ListView feedListView;
String[] test;
ArrayList<HashMap<String, String>> list;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_feed);
//create button and implement on click listener
sendButton = this.findViewById(R.id.sendPostButton);
sendButton.setOnClickListener(this);
list = new ArrayList<HashMap<String, String>>();
//create the adapter for the list view
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.post_layout,
new String[]{"time", "post"},
new int[]{R.id.postTimeTextView, R.id.postTextView});
//fill the list view with something - TEST
fillData();
//set list adapter
setListAdapter(adapter);
}
public void fillData() {
//long timeTest = System.currentTimeMillis();
HashMap<String, String> temp = new HashMap<String, String>();
temp.put("time", "current time");
temp.put("post", "USER TEXT GOES HERE");
list.add(temp);
}
#Override
public void onClick(View button) {
switch (button.getId()) {
case R.id.sendPostButton:
break;
}
}
}
It's as simple as adding a new element to your ArrayList (like you do in fillData), then calling adapter.notifyDataSetChanged().
After calling fillData(), just make a call on adapter.notifyDataSetChanged().
NotifyDataSetChanged() - Notifies the attached View that the underlying data has been changed and it should refresh itself.

Categories

Resources