![](/img/trans.png)
[英]How to remove specific space between parent and child in an ExpandableListView
[英]how to remove the child from the list in expandablelistview after clicked?
所以我有一個ExpandableListView。 單擊該子項后,它將從mysql表中刪除數據。 從表中刪除數據后,我希望將其從列表中刪除。
我嘗試使用remove(),但仍然無法正常工作。 這是我的代碼
exKonsumsi = (ExpandableListView) findViewById(R.id.EVkonsumsi_user);
exKonsumsi.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
prepareListMenuPagi();
exKonsumsiAdapter = new com.ta.helper.ExpandableListAdapterKonsumsi(
this, listDataHeaderDaftarAwal, listDataChildDaftarAwal);
exKonsumsi.setAdapter(exKonsumsiAdapter);
exKonsumsi.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
group = groupPosition;
child = childPosition;
String namaMakanan = listDataChildDaftarAwal.get(
listDataHeaderDaftarAwal.get(groupPosition)).get(
childPosition);
Toast.makeText(getApplicationContext(), namaMakanan + " " + "sudah dihapus" , Toast.LENGTH_LONG) .show();
try {
String nama = URLEncoder.encode(username, "utf-8");
System.out.println(username);
String namatoEn = URLEncoder.encode(namaMakanan, "utf-8");
url += "?" + "&nama=" + nama + "&namatoEn=" + namatoEn ;
getRequest(url);
}
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
listDataChildDaftarAwal.remove(namaMakanan);
return true;
}
});
適配器:
public class ExpandableListAdapterKonsumsi extends BaseExpandableListAdapter {
public static int takaran = 0;
private Context context;
private List<String> listDataHeader; // judul header
private HashMap<String, List<String>> listDataChild; // child data dengan
// format judul
// header, judul
// child
public ExpandableListAdapterKonsumsi(Context context,
List<String> listDataHeader,
HashMap<String, List<String>> listDataChild) {
super();
this.context = context;
this.listDataHeader = listDataHeader;
this.listDataChild = listDataChild;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return this.listDataChild.get(this.listDataHeader.get(groupPosition))
.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
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.konsumsi_user_expand_listchild, null);
}
CheckedTextView listChild = (CheckedTextView) convertView
.findViewById(R.id.lblListItemKonsumsi);
listChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return this.listDataChild.get(this.listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return this.listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return this.listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(
R.layout.daftar_akun_expand_listgroup, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
任何幫助,將不勝感激。 謝謝
您必須刪除與listview項相關聯的數據,例如刪除listDataChild
中的一項,然后調用adapter.notifydatasetchanged
通知listview數據已更改,然后listview將刷新視圖以與數據保持同步。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.