I am wriring an Android app, and I decided to use expandable list view. I have everything I need, list is working fine, but when I populate it, groups appear to be in different order than they should, here's an example:
As you can see, child values are fine, only parent values appear in random order. Here's the full code I have:
My fragment code:
public class BuildingsFragment extends Fragment {
public BuildingsFragment() {
// Required empty public constructor
}
public static BuildingsFragment newInstance(Context context) {
BuildingsFragment fragment = new BuildingsFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_buildings, container, false);
final ExpandableListView expandableListView = (ExpandableListView) view.findViewById(R.id.expandableListViewBuildings);
HashMap<String, List<String>> expandableListDetail = ExpandableListDataPump.getData();
List<String> expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
ExpandableListAdapter expandableListAdapter = new CustomExpandableListAdapter(getContext(), expandableListTitle, expandableListDetail);
expandableListView.setAdapter(expandableListAdapter);
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Fragment fragment = null;
Class fragmentClass = BuildingDetailsFragment.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentTransaction trans = getFragmentManager().beginTransaction();
Bundle args = new Bundle();
args.putString("BUILDING_NAME", "test");
fragment.setArguments(args);
trans.replace(R.id.frameLayoutForFragments, fragment);
trans.addToBackStack(null);
trans.commit();
return false;
}
});
return view;
}
}
My custom adapter:
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
public CustomExpandableListAdapter(Context context, List<String> expandableListTitle, HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
#Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
#Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
#Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_view_buildings_row_layout, null);
}
TextView expandedListTextView = (TextView) convertView
.findViewById(R.id.expandedListItem);
expandedListTextView.setText(expandedListText);
return convertView;
}
#Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}
#Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
#Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
#Override
public long getGroupId(int listPosition) {
return listPosition;
}
#Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_view_buildings_group_layout, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.listGroup);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}
The ExpandableListDataPump I am using:
public class ExpandableListDataPump {
public static HashMap<String, List<String>> getData() {
HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
List<String> abuildings = new ArrayList<String>();
abuildings.add("A-1");
abuildings.add("A-2");
abuildings.add("A-3");
abuildings.add("A-5");
abuildings.add("A-7");
expandableListDetail.put("A", abuildings);
List<String> bbuildings = new ArrayList<String>();
bbuildings.add("B-1");
bbuildings.add("B-2");
bbuildings.add("B-3");
bbuildings.add("B-5");
bbuildings.add("B-6");
expandableListDetail.put("B", bbuildings);
List<String> cbuildings = new ArrayList<String>();
cbuildings.add("C-1");
cbuildings.add("C-3");
cbuildings.add("C-4");
cbuildings.add("C-5");
cbuildings.add("C-6");
expandableListDetail.put("C", cbuildings);
List<String> dbuildings = new ArrayList<String>();
dbuildings.add("D-1");
dbuildings.add("D-2");
dbuildings.add("D-3");
dbuildings.add("D-5");
dbuildings.add("D-7");
expandableListDetail.put("D", dbuildings);
return expandableListDetail;
}
}
I think that's everything related to ExpandableListView. I would really like to leave HashMap, but if there is no other way, can I just change it to TreeMap and use it's sorting function (as a last resort)? Thanks for help.
P.S. While doing my ExpandableListView I was following this tutorial (I don't know if that matters): Tutorial
This is because HashMap doesn't maintain the order of the items as those are inserted. So everywhere in your code (Activity & Adapter) change HashMap to LinkedHashmap. Like
private LinkedHashMap<String, List<String>> expandableListDetail;
According to the Java Specs for HashMap, the order is not guaranteed:
...This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time...
So, if you want to guarantee the order of your groups, you'll have to use a different data structure.
See this answer for a similar discussion, and the suggested usage of LinkedHashMap, which maintains insertion order.
Related
I have Expandable List View in my app with is hardcoded. After getting to know Firebase database i want to add inside child item information from my database. I copyed code and node from here android expandable listview retrieve data from firebase and it worked perfectly fine. Here is the result:
My main Firebase node look like this:
How can i display data from my node? All information stored inside "campus", building and faculties are should be parent item. Building1 and Building2, same goes to faculty item, should be child items. How can i achieve my goal? Here is my Java code:
public class campus extends AppCompatActivity {
CampusAdapter campusAdapter;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
FirebaseDatabase database;
DatabaseReference myRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_campus);
database = FirebaseDatabase.getInstance();
myRef = database.getReference("campus");
expandableListView = findViewById(R.id.campusExpandableListView);
SetStandardGroups();
campusAdapter = new CampusAdapter(this, listDataHeader, listDataChild);
expandableListView.setAdapter(campusAdapter);
}
public void SetStandardGroups() {
listDataHeader = new ArrayList<>();
listDataChild = new HashMap<>();
myRef.addChildEventListener(new ChildEventListener() {
int counter = 0;
List<String> childItem;
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
listDataHeader.add(dataSnapshot.getKey());
Log.e("TAG", listDataHeader.get(counter));
childItem = new ArrayList<>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String childNames = (String) ds.getValue();
Log.e("TAG", "childNames :" + childNames);
childItem.add(childNames);
}
listDataChild.put(listDataHeader.get(counter), childItem);
counter++;
Log.e("TAG", "counter :" + counter);
campusAdapter.notifyDataSetChanged();
}
});
}
Here is my Adapter java file:
public class CampusAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> headerItem;
private HashMap<String, List<String>> childItem;
public CampusAdapter(Context context, List<String> headerItem, HashMap<String, List<String>> childItem) {
this.context = context;
this.headerItem = headerItem;
this.childItem = childItem;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return childItem.get(headerItem.get(groupPosition)).get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_item, null);
}
TextView tv = convertView.findViewById(R.id.campusName);
tv.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return childItem.get(headerItem.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return headerItem.get(groupPosition);
}
#Override
public int getGroupCount() {
return headerItem.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.group_item, null);
}
TextView tv = convertView.findViewById(R.id.groupName);
tv.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
p.s i use Glide to display images
This goes in your onCreate() of the main class (the one with the listView)
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("(campus/building");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for(DataSnapshot ds:snapshot.getChildren()){
String title = ds.child("title").getValue(String.class);
String image = ds.child("image").getValue(String.class);
//TODO: add it to the expandable view with the link above who explains it
ExpandableListView myExpList= (ExpandableListView) this.view.findViewById(R.id.yourExpListView);
ExpandableListAdapter adapter =
(ExpandableListAdapter) myExpList.getExpandableListAdapter();
( (ExpandableListParentClass)adapter.getMParent().get(0) ).getParentChildren().add(title);
( (ExpandableListParentClass)adapter.getMParent().get(0) ).getParentChildren().add(image);
//(change to get(0) which you parent want to get )
adapter.notifyDataSetChanged();
adapter.notifyDataSetInvalidated();
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
})
Then create a new java class called "ExpandableListAdapter" with following code:
public class ExpandableListAdapter extends BaseExpandableListAdapter implements Filterable{
private LayoutInflater inflater;
private ArrayList<ExpandableListParentClass<Object>> mParent;
private View view;
public ArrayList<Object> getMParent() {
return mParent;
}
public ExpandableListAdapter(Context context,
ArrayList<ExpandableListParentClass<Object>> parentList ) {
this.mParent = parentList;
this.inflater = LayoutInflater.from(context);
}
// counts the number of group/parent items so the list knows how many
// times calls getGroupView() method
public int getGroupCount() {
return mParent.size();
}
// counts the number of children items so the list knows how many times
// calls getChildView() method
public int getChildrenCount(int parentPosition) {
int size =0;
if(mParent.get(parentPosition).getParentChildren() != null){
size = mParent.get(parentPosition).getParentChildren().size();
}
return size;
}
// gets the title of each parent/group
public Object getGroup(int i) {
return mParent.get(i).getParent();
}
// gets the name of each item
public Object getChild(int parentPosition, int childPosition) {
return mParent.get(parentPosition).getParentChildren().get(childPosition);
}
public long getGroupId(int parentPosition) {
return parentPosition;
}
public long getChildId(int i, int childPosition) {
return childPosition;
}
public boolean hasStableIds() {
return true;
}
// in this method you must set the text to see the parent/group on the list
public View getGroupView(int parentPosition, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.layout_listview, viewGroup, false);
}
return view;
}
// in this method you must set the text to see the children on the list
public View getChildView(int parentPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.layout_listview, viewGroup, false);
}
// return the entire view
return view;
}
public boolean isChildSelectable(int i, int i1) {
return true;
}
}
List<ExpandableListParentClass> arrayParents = new ArrayList<ExpandableListParentClass>();
ExpandableListParentClass item = new ExpandableListParentClass();
item.setParent(yourParentItem);
item.setParentChildren( yourChildList);
Then create a new java class called "ExpandableListParentClass" with the following code:
public class ExpandableListParentClass{
private Object parent;
private ArrayList<Object> parentChildren;
public ExpandableListParentClass() {
}
public ExpandableListParentClass(Object parent, ArrayList<Object> parentChildren) {
this.parent = parent;
this.parentChildren = parentChildren;
}
public Object getParent() {
return parent;
}
public void setParent(Object parent) {
this.parent = parent;
}
public ArrayList<Object> getParentChildren() {
return parentChildren;
}
public void setParentChildren(ArrayList<Object> parentChildren) {
this.parentChildren = parentChildren;
}
}
I am using Exapandable listview inside a fragment,i am passing 8 values but it is showing only 6 values and that too the exapnadable listview is not scrolling.the same if i put it in activity..everything works fine.but when i use within fragment it is showing error.can anyone tell me how to solve it...here is my code
public class GroupsFragment extends Fragment {
View groupview;
List<String> tdp, congress, ysrcp, bjp;
ExpandableListView expandableListView;
HashMap<String, ArrayList<GroupModel>> expandableListDetail;
ArrayList<GroupModel> contacts;
List<String> expandableListTitle;
ExpandableListAdapter expandableListAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the dailog_layout for this fragment
groupview = inflater.inflate(R.layout.contacts_expandable_list, container, false);
expandableListView = groupview.findViewById(R.id.expanded_list_view_contacts);
contacts = new ArrayList<>();
return groupview;
}
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
expandableListDetail = new HashMap<String, ArrayList<GroupModel>>();
showContactsList();
}
#SuppressLint("ClickableViewAccessibility")
private void showContactsList() {
getExpandableListItems();
expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
expandableListAdapter = new CustomContactsExapandableListAdapter(getActivity(), expandableListTitle, expandableListDetail);
expandableListView.setAdapter(expandableListAdapter);
}
private HashMap<String, ArrayList<GroupModel>> getExpandableListItems() {
contacts.add(new GroupModel("gopi", "Officer", "9999999999", "abcd0"));
expandableListDetail.put("TDP", contacts);
expandableListDetail.put("YSRCP", contacts);
expandableListDetail.put("Congress", contacts);
expandableListDetail.put("BJP", contacts);
expandableListDetail.put("ABC", contacts);
expandableListDetail.put("CDE", contacts);
expandableListDetail.put("DEF", contacts);
expandableListDetail.put("FED", contacts);
return expandableListDetail;
}
}
and here is the adapter
public class CustomContactsExapandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, ArrayList<GroupModel>> expandableListDetail;
public CustomContactsExapandableListAdapter(Context context, List<String> expandableListTitle,
HashMap<String, ArrayList<GroupModel>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(groupPosition))
.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
Log.i("test", "the value in exapandable is " + getChild(groupPosition, childPosition));
final GroupModel expandedListText = (GroupModel) getChild(groupPosition, childPosition);
Log.i("test", "the text is " + expandedListText.getName()+ " "+ expandedListText.getDesignation() + " " +expandedListText.getMobile() );
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.single_contact, null);
}
TextView expandedListTextViewName = (TextView) convertView
.findViewById(R.id.contact_name);
TextView expandedListTextViewMobile = (TextView) convertView
.findViewById(R.id.contact_mobile_num);
expandedListTextViewMobile.setText(expandedListText.getDesignation());
expandedListTextViewName.setText(expandedListText.getName());
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this.expandableListTitle.get(groupPosition);
}
#Override
public int getGroupCount() {
Log.i("test", "the value in exapandable group is " + expandableListTitle.size());
return expandableListTitle.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.single_party, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.party_name);
ImageView imageView=convertView.findViewById(R.id.party_expandable_image);
if(isExpanded){
imageView.setImageResource(R.drawable.minus_icon_contacts);
}
else {
imageView.setImageResource(R.drawable.plus_icon_contacts);
}
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
After reading data from a CSV file, I have it stored in List.
List <String>listDataHeader = new ArrayList<String>();
Map<String,String>listDataChild = new HashMap<String, String>();
List<String[]> termDefinition = csv.read();
Then I itirate over the list and want to store the data. As it is written now, I take the correct keys, but not the values:
for (String[] term : termDefinition) {
listDataHeader.add(term[0]);
listDataChild.put(term[0], term[1]);
}
May you help me? Thanks!
EDIT:
I need to fill data in ExpandavbleListVew. The listdataHeader is the main topic, the listdataChild is the subitem. There are only 2 columns in the CSV, in the format term-definition. At the moment, main topics (from the listDataheader) are stored correctly. After correcting the loop (see above), I have for each main item the definition appearing as many times as the size of the loop (10 in the case).
Here is the expandableListView class:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader; // header titles
// child data in format of header title, child title
private Map<String, String> listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
Map<String, String> listChildData) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listDataChild = listChildData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this.listDataChild.get(this.listDataHeader.get(groupPosition));
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.itemList);
txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this.listDataChild.size();
}
#Override
public Object getGroup(int groupPosition) {
return this.listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this.listDataHeader.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.header, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.headerList);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
I don't see the point of the listDataHeader List. Using it causes you to put the same key (listDataHeader.get(0)) over and over again in the Map.
Since you only have 2 columns in each row of the CSV, all you need is :
for (String[] term : termDefinition) {
listDataChild.put(term[0], term[1]);
}
My Fragment didn't show anything for my expandable view. there's not error as well. and i don't know where went wrong so i need help Can someone let me know my error? There's no error on the page/didn't have any crashes as well
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listGroup;
private HashMap<String, List<String>> listChild;
public MyExpandableListAdapter(Context context, List<String> listGroup,
HashMap<String, List<String>> listChild) {
this.context = context;
this.listGroup = listGroup;
this.listChild = listChild;
}
#Override
public int getGroupCount() {
return listGroup.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return listChild.get(listGroup.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return listGroup.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return listChild.get(listGroup.get(groupPosition)).get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_layout, null);
}
String textGroup = (String) getGroup(groupPosition);
TextView textViewGroup = (TextView) convertView
.findViewById(R.id.group);
textViewGroup.setText(textGroup);
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.item_layout, null);
}
TextView textViewItem = (TextView) convertView.findViewById(R.id.item);
String text = (String) getChild(groupPosition, childPosition);
textViewItem.setText(text);
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
public class Hotel extends Fragment {
ExpandableListView expandableListView;
MyExpandableListAdapter myExpandableListAdapter;
List<String> groupList;
HashMap<String, List<String>> childMap;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.hotel_frag, container, false);
init();
expandableListView = (ExpandableListView) v.findViewById(R.id.mylist);
expandableListView.setAdapter(myExpandableListAdapter);
myExpandableListAdapter = new MyExpandableListAdapter(Hotel.this.getActivity().getApplicationContext(), groupList, childMap);
return v;
}
private void init() {
groupList = new ArrayList<String>();
childMap = new HashMap<String, List<String>>();
List<String> groupList0 = new ArrayList<String>();
groupList0.add("groupList0 - 1");
groupList0.add("groupList0 - 2");
groupList0.add("groupList0 - 3");
List<String> groupList1 = new ArrayList<String>();
groupList1.add("groupList1 - 1");
groupList1.add("groupList1 - 2");
groupList1.add("groupList1 - 3");
List<String> groupList2 = new ArrayList<String>();
groupList2.add("groupList2 - 1");
groupList2.add("groupList2 - 2");
groupList2.add("groupList2 - 3");
List<String> groupList3 = new ArrayList<String>();
groupList3.add("groupList3 - 1");
groupList3.add("groupList3 - 2");
groupList3.add("groupList3 - 3");
groupList.add("Group List 0");
groupList.add("Group List 1");
groupList.add("Group List 2");
groupList.add("Group List 3");
childMap.put(groupList.get(0), groupList0);
childMap.put(groupList.get(1), groupList1);
childMap.put(groupList.get(2), groupList2);
childMap.put(groupList.get(3), groupList3);
}
}
http://i.stack.imgur.com/dnM7Q.png
http://i.stack.imgur.com/eXJOz.png
http://i.stack.imgur.com/VmaUj.png
You have to construct the adapter before you assign it to the ListView:
init();
myExpandableListAdapter = new MyExpandableListAdapter(Hotel.this.getActivity().getApplicationContext(), groupList, childMap);
expandableListView = (ExpandableListView) v.findViewById(R.id.mylist);
expandableListView.setAdapter(myExpandableListAdapter);
I have two custom listview in the same activity. Then, second listview can get the itemValue and itemID selected from the first listview. Then now, how do I check if the itemID is already exists in the second listview? Thanks in advance.
This is my second listview
private void listOrder(String itemValue, String itemID)
{
HashMap<String, String> map = new HashMap<String, String>();
String quantity = "1";
map.put(FOODID3, itemID);
map.put(FOODNAME3, itemValue);
map.put(FOODQUANTITY3, quantity);
LIST3.add(map);
LISTORDER = (ListView) findViewById(R.id.listOrder);
List3Adapter adapter = new List3Adapter(MainActivity.this, LIST3);
LISTORDER.setAdapter(adapter);
adapter.notifyDataSetChanged();
LISTORDER.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
}
});
}
And this is the class of the listview
public class List3Adapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public List3Adapter(Activity a, ArrayList<HashMap<String, String>> d)
{
activity = a;
data = d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View vi = convertView;
if (convertView == null)
{
vi = inflater.inflate(R.layout.row, null);
}
TextView foodID3 = (TextView)vi.findViewById(R.id.foodID3);
TextView foodname3 = (TextView)vi.findViewById(R.id.foodName3);
TextView foodquantity3 = (TextView)vi.findViewById(R.id.foodQuantity3);
HashMap<String,String> food = new HashMap<String, String>();
food = data.get(position);
foodID3.setText(food.get(MainActivity.FOODID3));
foodname3.setText(food.get(MainActivity.FOODNAME3));
foodquantity3.setText(food.get(MainActivity.FOODQUANTITY3));
return vi;
}
}
You can check for the key in hash map to check if the itemID is already exists or not
if(data.containsKey(itemID))
{
// your code
}
where data is a HashMap<String, String> object