EditText text is copying itself to another EditText field - java

Now when I change fields and open another Expandable List it change fields
My Expandable list Adapter
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private static final String LOG_TAG = "EXPANDABLE_LIST_ADAPTER_LOG";
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
Log.i(LOG_TAG,"Child:"+listChildData+" header:"+listDataHeader);
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
Object obj = this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
Log.i(LOG_TAG,"GroupPos:"+groupPosition+" childPos:"+childPosititon+" EdTex:"+obj);
return obj;
}
#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.list_item, null);
}
EditText txtListChild = (EditText) convertView
.findViewById(R.id.lblListItem);
// txtListChild.setText(childText);
txtListChild.setHint(childText);
Log.i(LOG_TAG,"GroupPos:"+groupPosition+" childPos:"+childPosition+" text:"+txtListChild.getText());
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
String groupS = this._listDataHeader.get(groupPosition);
int size =this._listDataChild.get(groupS)
.size();
Log.i(LOG_TAG,"Group:"+groupS+" grouppos:"+groupPosition+" size:"+size );
return 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.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Also I realize that when keyboard is on, it deletes fields, or make it disappear like if it was creating another instance of EditTexts.
The first problem got fixed by setting stable ids to true, I found that might be useful if anyone willing to associate the problems
#Override
public boolean hasStableIds() {
return true;
}

Related

Problem with an ExpandableList with 3 level

I have a problem with my ExpendableList. I am trying
to make a 3 level expandablelist. The display on the 3 levels works
fine and stays tidy as it should. But I don't know why, the
subcategories are duplicated. Here is the whole code with the adapter
and the creation of my HashMap. If you have an idea where this comes
from you would help me a lot!
public HashMap<String, HashMap<String, ArrayList<String>>>typeItemList;
public HashMap<String, ArrayList<String>>materiauItemList;
public HashMap<String, HashMap<String, HashMap<String, ArrayList<String>>>> familleItemList;
private ExpandableListView expandableListView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> famille = new ArrayList<>();
ArrayList<String> type= new ArrayList<>();
ArrayList<String> materiaux = new ArrayList<>();
familleItemList = new HashMap<>();
for(int g = 1; g<=5; g++) {
famille.add("famille "+g);
HashMap<String, HashMap<String, ArrayList<String>>> typeItemList1 = new HashMap<>();
for (int h = 1; h <= 4; h++) {
type.add("type " + g+h);
HashMap<String, ArrayList<String>> matItemList = new HashMap<>();
for (int i = 1; i <= 4; i++) {
materiaux.add("materiau " + g+h+i);
ArrayList<String> outils = new ArrayList<>();
for (int j = 1; j <= 10; j++) {
outils.add("outils " + g+h+i+j);
}
matItemList.put("materiau " + i, outils);
}
typeItemList1.put("type " + h, matItemList);
}
familleItemList.put("famille "+g, typeItemList1);
}
expandableListView = (ExpandableListView) findViewById(R.id.mainList);
expandableListView.setAdapter(new ParentLevel(this,familleItemList, famille ));
}
public class ParentLevel extends BaseExpandableListAdapter {
private Context context;
ArrayList<String> famille;
HashMap<String, HashMap<String, HashMap<String, ArrayList<String>>>> familleItemList;
public ParentLevel(Context context, HashMap<String, HashMap<String, HashMap<String, ArrayList<String>>>> familleItemList, ArrayList<String> famille ) {
this.context = context;
this.familleItemList = familleItemList;
this.famille=famille;
}
//
#Override
public int getGroupCount() {
return famille.size();
}
#Override
public int getChildrenCount(int i) {
return familleItemList.get(famille.get(i)).size();
}
#Override
public Object getGroup(int i) {
return famille.get(i);
}
#Override
public Object getChild(int i, int i1) {
return familleItemList.get(famille.get(i)).get(i1);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
SecondLevelExpandableListView secondLevelELV = new SecondLevelExpandableListView(MainActivity.this);
ArrayList<String> list = new ArrayList<>();
for (String i : Objects.requireNonNull(familleItemList.get(famille.get(groupPosition))).keySet()) {
list.add(i);}
typeItemList = new HashMap<>();
typeItemList=familleItemList.get(getGroup(groupPosition));
secondLevelELV.setAdapter(new SecondLevelAdapter(context, typeItemList,list ));
secondLevelELV.setGroupIndicator(null);
return secondLevelELV;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_first, null);
TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText);
String sGroup = String.valueOf(getGroup(groupPosition));
text.setText(sGroup);
}
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
public static class SecondLevelExpandableListView extends ExpandableListView {
public SecondLevelExpandableListView(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//999999 is a size in pixels. ExpandableListView requires a maximum height in order to do measurement calculations.
heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public class SecondLevelAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<String> type;
private HashMap<String, HashMap<String, ArrayList<String>>> typeItemList;
public SecondLevelAdapter(Context context,HashMap<String, HashMap<String, ArrayList<String>>> typeItemList, ArrayList<String> type) {
this.context = context;
this.typeItemList = typeItemList;
this.type=type;
}
#Override
public int getGroupCount() {
return type.size();
}
#Override
public int getChildrenCount(int i) {
return typeItemList.get(type.get(i)).size();
}
#Override
public Object getGroup(int i) {
return type.get(i);
}
#Override
public Object getChild(int i, int i1) {
return typeItemList.get(type.get(1)).get(i1);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_third, null);
TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText);
String sGroup = String.valueOf(getGroup(groupPosition));
text.setText(sGroup);
}
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ThirdLevelExpandableListView thridLevelELV = new ThirdLevelExpandableListView(MainActivity.this);
ArrayList<String> list = new ArrayList<>();
for (String i : typeItemList.get(getGroup(groupPosition)).keySet() ){
list.add(i);}
materiauItemList=new HashMap<>();
materiauItemList = typeItemList.get(getGroup(groupPosition));
thridLevelELV.setAdapter(new ThirdLevelAdapter(context,materiauItemList,list));
thridLevelELV.setGroupIndicator(null);
return thridLevelELV;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
public static class ThirdLevelExpandableListView extends ExpandableListView {
public ThirdLevelExpandableListView(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//999999 is a size in pixels. ExpandableListView requires a maximum height in order to do measurement calculations.
heightMeasureSpec = MeasureSpec.makeMeasureSpec(99999, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public static class ThirdLevelAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<String> materiau;
private HashMap<String, ArrayList<String>> materiauItemList;
public ThirdLevelAdapter(Context context,HashMap<String, ArrayList<String>> materiauItemList,ArrayList<String> materiau) {
this.context = context;
this.materiauItemList = materiauItemList;
this.materiau = materiau;
}
#Override
public Object getGroup(int groupPosition) {
return materiau.get(groupPosition);
}
#Override
public int getGroupCount() {
return materiau.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_four, null);
TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText);
text.setText(String.valueOf(getGroup(groupPosition)));
}
return convertView;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return materiauItemList.get(materiau.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) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_second, null);
TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText);
String outils = String.valueOf(getChild(groupPosition, childPosition));
text.setText(outils);
}
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return materiauItemList.get(materiau.get(groupPosition)).size();
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
[family photo displayed correctly][1] [photo of repeating types][2]
[photo of tools that are also repeated][3]
Thank you for your feedback.
[1]: https://i.stack.imgur.com/8TiQa.png [2]:
https://i.stack.imgur.com/LOMol.png [3]:
https://i.stack.imgur.com/hdFAE.png

How can I get specific view from ExpandableListView?

I have created a expandable list view and two radio buttons in it.
I want to get the value of the radio button which is selected when some button is pressed in my main Activity. My ExpandableListView is working properly but i am not able to get the radio button which is selected in main activity.
public class ExpandableMenuListAdapter extends BaseExpandableListAdapter
{
private Context context;
private List<String> listDataHeader;
private HashMap<String,List<List<String>>> listHashMap;
public ExpandableMenuListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<List<String>>> listHashMap) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listHashMap = listHashMap;
}
#Override
public int getGroupCount() {
return listDataHeader.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return Objects.requireNonNull(listHashMap.get(listDataHeader.get(groupPosition))).size();
}
#Override
public Object getGroup(int groupPosition) {
return listDataHeader.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return Objects.requireNonNull(listHashMap.get(listDataHeader.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) {
String headerTitle = (String)(getGroup(groupPosition)).toString();
if (convertView==null)
{
LayoutInflater inflater =(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_menu_days_group,null);
}
TextView lblListHeader = convertView.findViewById(R.id.list_days);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
List<String> childText = (List<String>) getChild(groupPosition,childPosition);
if (convertView==null)
{
LayoutInflater inflater =(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_menu_item,null);
}
RadioButton txtListChild = convertView.findViewById(R.id.menu_list_daily1);
RadioButton txtListChild2 = convertView.findViewById(R.id.menu_list_daily2);
txtListChild.setText(childText.get(0));
txtListChild2.setText(childText.get(1));
ViewsList.menu.add(convertView);
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
Use onChildClick method in your code.This method is automatically called when you click on child item
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
//get your radio here from the view v and do whatever you want
return true;
}

ExpandableListAdapter methods are not working properly in my case. Any alternate to ExpandableListView

I am using an ExpandableListAdapter for the first time. I have some textviews in the groupView and some buttons in childView. I've used the following code from the internet and tried to convert it according to my preference. But there is a problem in the getGroupCount() method. I know in my code _listDataHeader and _listDataChild are null, but what should I use instead? Here are the pictures of my header view
and Child View layout
.
On the click of header this child View should be shown on screen.. Am i using the correct android View for this design? Please help
public class ExpandableListAdapter extends BaseExpandableListAdapter {
Activity context;
ArrayList<String> s_date,c_number,d_ration,s_time,download_path,a_number,a_name;
String id[];
String mUrl;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Activity context, ArrayList<String> start_date, ArrayList<String> caller_number, ArrayList<String> duration,ArrayList<String> start_time, ArrayList<String> download_path, ArrayList<String> agent_number, ArrayList<String> agent_name) {
this.context=context;
this.s_date=start_date;
this.c_number=caller_number;
this.d_ration=duration;
this.s_time=start_time;
this.download_path=download_path;
this.a_number=agent_number;
this.a_name=agent_name;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(final 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.list_item, null);
}
Button play_button=(Button) convertView.findViewById(R.id.play);
play_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("play child is",childText);
}
});
Button download_button=(Button) convertView.findViewById(R.id.download);
download_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("download child is",childText);
}
});
}
#Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this.s_date.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.list_group, null);
}
TextView start_date = (TextView) convertView.findViewById(R.id.start_date);
start_date.setText(s_date.get(groupPosition));
TextView caller_number = (TextView) convertView.findViewById(R.id.caller_number);
caller_number.setText(c_number.get(groupPosition));
TextView duration = (TextView) convertView.findViewById(R.id.duration);
duration.setText(d_ration.get(groupPosition));
TextView start_time = (TextView) convertView.findViewById(R.id.start_time);
start_time.setText(s_time.get(groupPosition));
TextView agent_name = (TextView) convertView.findViewById(R.id.agent_name);
agent_name.setText(a_name.get(groupPosition));
TextView agent_number = (TextView) convertView.findViewById(R.id.agent_number);
agent_number.setText(a_number.get(groupPosition));
// start_date.setTypeface(null, Typeface.BOLD);
// start_date.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
and this is how i'm setting the adpater
ExpandableListAdapter a=new ExpandableListAdapter(MainScreen.this,start_date,caller_number,duration,start_time,download_path,agent_number,agent_name);
data_list.setAdapter(a);
As Suggested by #PratikDasa I have used Custom ListView and in that i have used Visible gone method. Below is the solution.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/sub_layout"
android:background="#FFC7FFFE"
android:visibility="gone">
I have set all the contents(Buttons in my case) under this sub_layout and set layout visibilty as gone.
And then on the click of any of the list item(Headers), I have shown the sub_layout that i have set as gone.
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View row = inflater.inflate(R.layout.list_item, g, false);
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Toast.makeText(context,"i'm Clicked",Toast.LENGTH_LONG).show();
RelativeLayout sub_list=(RelativeLayout)row.findViewById(R.id.sub_layout);
sub_list.setVisibility(View.VISIBLE);
}
});
Now it works like an ExpandableListView.

how to get all the child id in an ExpandableListView without depending on the parent group

I've designed a GUI with an ExpandableListView, please see sample picture below.
I want to know if it is possible to get the IDs of all the child in the list without having to depend on the parent group.
Presently what I'm able to get is 0-2 for the first Parent and the second parent also starts from 0.
So basically I want to get the IDs like:
Fixture ? id(0)
Chillers ? id(1)
Displays ? id(2)
"33" Export (Can) id(3)
etc.
Here my Custom adapter class:
public class MTOExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public MTOExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
#Override
public int getGroupCount() {
return this._listDataHeader.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosition);
}
#Override
public long getGroupId(int childPosition) {
return childPosition;
}
#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) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_mto_group, null);
}
TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
#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 inflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_mto_item, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.lblMTOListItem);
txtListChild.setText(childText);
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Okay, I see what you are asking. You are trying to get a unique ID for every child of every group, in numerical order.
#Override
public long getChildId(int groupPosition, int childPosition) {
int id = 0;
for (int group = 0; group < groupPosition; group++) {
id += getChildrenCount(group);
}
id += childPosition;
return id;
}

How to add different image for each group in expandable Listview

In my Expandable Listview, have three groups. I want to add different three image for that group. i tried many codes, but not working.
My code:
Main Activity:
HashMap<String, List<String>> rightDrawerListDetail = getData();
List<String> rightDrawerListTitle = new ArrayList<String>(rightDrawerListDetail.keySet());
adapterR = new CustomExpandableListAdapter(this, rightDrawerListTitle,rightDrawerListDetail);
mRightDrawerList.setAdapter(adapterR);
private HashMap<String, List<String>> getData(){
HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
ArrayList<String> arrayList = mydb.getAllAddress();
List asRider = new ArrayList();
asRider.add("hello");
List asRidee = new ArrayList();
asRidee.add("hai");
List recent = new ArrayList();
recent.add("Success");
expandableListDetail.put("As a Rider",asRider);
expandableListDetail.put("As a Ridee",asRidee);
expandableListDetail.put("My Recent Activities",recent);
return expandableListDetail;
}
CustomExpandableListAdapter.java:
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private List<String> mGroups;
private LayoutInflater mInflater;
private HashMap<String, List<String>> mexpandableListDetail;
public CustomExpandableListAdapter(Context context, List<String> groups,HashMap<String, List<String>> expandableListDetail) {
mContext = context;
mGroups = groups;
mexpandableListDetail = expandableListDetail;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getGroupCount() {
return mGroups.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return mexpandableListDetail.get(mGroups.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return mGroups.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return mexpandableListDetail.get(mGroups.get(groupPosition)).get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return 0;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.right_drawer_group, null);
}
// Get the group item
String listTitle = (String) getGroup(groupPosition);
// Set group name
TextView textView = (TextView) convertView.findViewById(R.id.textGroup);
textView.setText(listTitle);
ImageView indicator = (ImageView) convertView.findViewById(R.id.groupIndicator);
if (isExpanded) {
indicator.setImageResource(R.drawable.arrowup);
} else {
indicator.setImageResource(R.drawable.arrowdown);
}
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.right_drawer_child, null);
}
// Get child name
String children = (String) getChild(groupPosition, childPosition);
// Set child name
TextView text = (TextView) convertView.findViewById(R.id.textChild);
text.setText(children);
/*convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, children, Toast.LENGTH_SHORT).show();
}
});*/
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Please anyone help to me!
Thanks in advance
In your right_drawer_group.xml add an ImageView where you want your group image to be placed, lets say groupImage.
In your getGroupView:
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.right_drawer_group, null);
}
//Set group image
ImageView groupImage = (ImageView)convertView.findViewById(R.id.groupImage);
switch((String)getGroup(groupPosition))
{
case "As a Rider":
//groupImage set correct image
break;
case "As a Ridee":
//groupImage set correct image
break;
case "My Recent Activities":
//groupImage set correct image
break;
}
// Get the group item
String listTitle = (String) getGroup(groupPosition);
// Set group name
TextView textView = (TextView) convertView.findViewById(R.id.textGroup);
textView.setText(listTitle);
ImageView indicator = (ImageView) convertView.findViewById(R.id.groupIndicator);
if (isExpanded) {
indicator.setImageResource(R.drawable.arrowup);
} else {
indicator.setImageResource(R.drawable.arrowdown);
}
return convertView;
}
It's not the most neat solution as if you change the group name this will break, but it will work for now. Better solution would be is to pass the groups as objects containing getName and getImageId.

Categories