Reputation: 23
ExpandableListAdapter Hi i need to get onclick event to the button in List_item.xml whein i click on the button. im beginning with java and android development. im searching for solution for 3 days
public class ExpandableListAdapter 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 ExpandableListAdapter(Context context, List < String > listDataHeader,
HashMap < String, List < 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))
.get(childPosititon);
}
@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);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
Button btnListChild = (Button) convertView.findViewById(R.id.btn);
txtListChild.setText(childText);
return convertView;
}
@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._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);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
list_item.xml - there is the button
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:orientation="vertical"
android:background="@color/colorACD_white"
>
<TextView
android:id="@+id/lblListItem"
android:layout_width="144dp"
android:layout_height="42dp"
android:layout_marginStart="0dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:layout_marginBottom="0dp"
android:gravity="center"
android:paddingLeft="25dp"
android:background="@color/colorACD_white"
android:textColor="@color/colorACD_black"
android:textSize="18sp"
android:fontFamily="@font/sourcesansproregular"/>
<Button
android:id="@+id/btn"
android:layout_width="115dp"
android:layout_height="45dp"
android:layout_marginLeft="250dp"
android:fontFamily="@font/robotoregular"
android:text="Edit">
</Button>
<LinearLayout
android:id="@+id/line"
android:layout_width="391dp"
android:layout_height="10dp"
android:orientation="horizontal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="popup"
android:orientation="horizontal" />
</RelativeLayout>
Dialog.xml - the window i want to open when i click on the button from list_item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="156dp"
android:layout_height="122dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="133dp"
android:layout_marginTop="297dp"
android:layout_marginEnd="121dp"
android:layout_marginBottom="312dp"
android:background="@color/colorACD_orange" />
</RelativeLayout>
**My Dialog Java class**
package com.example.application.example;
public class ExampleDialog extends AppCompatDialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Information")
.setMessage("This is a Dialog")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
return builder.create();
}
}
Upvotes: 1
Views: 73
Reputation: 2056
I have added an interface to your adapter. Now from where you are creating this adapter you need to implement the interface ExpandableListAdapterInterFace
and the buttonClicked
method, you can change the arguments and their types as per your need.
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private final ExpandableListAdapterInterFace _expandableListAdapterInterFace;
interface ExpandableListAdapterInterFace{
void buttonClicked(String dataHeader,int position);
}
//........ your code............
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData, ExpandableListAdapterInterFace expandableListAdapterInterFace) {
this._context = context;
this._expandableListAdapterInterFace=expandableListAdapterInterFace;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
//..............your code .........
@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);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
Button btnListChild = (Button) convertView.findViewById(R.id.btn);
btnListChild.setOnClickListener(v->{
_expandableListAdapterInterFace.buttonClicked(childText,childPosition);
});
txtListChild.setText(childText);
return convertView;
}
//..................your code............
}
Upvotes: 1