I have challenges to access my childItems in ExpandableListView.
I am able to access my child items when I have one single view(TextView) in the list, but when I add another view(CheckBox), then in my MainActivity, setOnChildClickListener not invoked .
Can someone please help me where I am doing wrong?
filter_layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ExpandableListView
android:id="@+id/lvExp"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ExpandableListView>
</RelativeLayout>
filter_group.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/laptop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="25dp"/>
</LinearLayout>
filter_list_item.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp" >
<TextView
android:id="@+id/laptop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:paddingLeft="25dp" />
<CheckBox
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
Following is my FilterAdapter:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Activity context;
private Map<String, List<String>> laptopCollections;
private List<String> laptops;
public ExpandableListAdapter(Activity context, List<String> laptops,
Map<String, List<String>> laptopCollections) {
this.context = context;
this.laptopCollections = laptopCollections;
this.laptops = laptops;
}
public Object getChild(int groupPosition, int childPosition) {
return laptopCollections.get(laptops.get(groupPosition)).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String laptop = (String) getChild(groupPosition, childPosition);
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.filter_list_item, null);
}
TextView item = (TextView) convertView.findViewById(R.id.laptop);
/*CheckBox delete = (CheckBox) convertView.findViewById(R.id.delete);
delete.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Do you want to remove?");
builder.setCancelable(false);
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
List<String> child =
laptopCollections.get(laptops.get(groupPosition));
child.remove(childPosition);
notifyDataSetChanged();
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});*/
item.setText(laptop);
return convertView;
}
public int getChildrenCount(int groupPosition) {
return laptopCollections.get(laptops.get(groupPosition)).size();
}
public Object getGroup(int groupPosition) {
return laptops.get(groupPosition);
}
public int getGroupCount() {
return laptops.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String laptopName = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.filter_list_group,
null);
}
TextView item = (TextView) convertView.findViewById(R.id.laptop);
item.setTypeface(null, Typeface.BOLD);
item.setText(laptopName);
return convertView;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Following is my mainactivity. Below you can observe expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() is not getting invoked at all. Can someone help me please where I am going wrong?
public class FilterActivity extends AppCompatActivity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filter_layout);
// get the listview
expListView = (ExpandableListView) 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 ExpandableListView.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 ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Log.d("FilterActivity","Hello world");
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Status");
listDataHeader.add("Hospital");
listDataHeader.add("Treatment Type");
// Adding child data
//Adding Status
List<String> status = new ArrayList<String>();
DBHelper dbHelper=new DBHelper(getApplicationContext());
Cursor statusCursor=dbHelper.getTreatmentStatusTypes();
while(statusCursor.moveToNext())
status.add(statusCursor.getString(0));
List<String> hospital = new ArrayList<String>();
Cursor hospitalCursor=dbHelper.getListOfHospitNames();
while(hospitalCursor.moveToNext())
hospital.add(hospitalCursor.getString(0));
List<String> treatmentTypes = new ArrayList<String>();
Cursor treatmentTypeCursor=dbHelper.getTreatmentTypes();
while(treatmentTypeCursor.moveToNext())
treatmentTypes.add(treatmentTypeCursor.getString(0));
dbHelper.close();
listDataChild.put(listDataHeader.get(0), status); // Header, Child data
listDataChild.put(listDataHeader.get(1), hospital);
listDataChild.put(listDataHeader.get(2), treatmentTypes);
}
}
You need to return true in all override function which you want to implement.
// Listview Group click listener
expListView.setOnGroupClickListener(new ExpandableListView.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 true;
}
});
also return true in this function.
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Log.d("FilterActivity","Hello world");
return true;
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.