我有expandablelistView
。单击列表时,它将显示两个按钮。单击删除按钮时,它会删除子项和父项。不幸的是,没有任何东西被删除。
ArrayList<ListObj> groupList= new ArrayList<ListObj>();
listview = (ExpandableListView) findViewById(R.id.exlistView);
expListAdapter = new ExpandableListAdapter(AddMonthlyExpenses.this,getApplication(), groupList);
listview.setAdapter(expListAdapter);
retrieveList(name);
public void retrieveList(String name) {
groupList.clear();
database = mdb.getReadableDatabase();
Cursor cursor = database.rawQuery("SELECT * FROM " + MyDatabaseHelper.TABLE__TASK + " WHERE Name = ? ", new String[]{name}, null);
if (cursor != null && cursor.getCount() > 0) {
groupList = new ArrayList<ListObj>();
while (cursor.moveToNext()) {
int iD = cursor.getInt(cursor.getColumnIndex("ID"));
String month = cursor.getString(cursor.getColumnIndex("Month"));
double budget = cursor.getDouble(cursor.getColumnIndex("Budget"));
groupList.add(new ListObj(iD,month,budget));
if (expListAdapter != null) {
expListAdapter.add(iD, month, budget,"edit");
listview.deferNotifyDataSetChanged();
}
}
}
}
适配器类
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<ListObj> laptops;
Activity parentActivity;
private LayoutInflater mInflater;
SQLiteDatabase database;
MyDatabaseHelper mdb;
public ExpandableListAdapter(Activity parentActivity, Context context, ArrayList<ListObj> laptops) {
this.parentActivity= parentActivity;
this.context = context;
this.laptops = laptops;
mInflater = LayoutInflater.from(context);
mdb= new MyDatabaseHelper(context);
}
public Object getChild(int groupPosition, int childPosition) {
ArrayList<ItemDetails> productList =
laptops.get(groupPosition).getProductList();
return productList.get(childPosition);
}
public void add(int id, String month, double budget) {
String[] splited = month.split("\\s+");
ListObj obj = new ListObj(id, month, budget);
obj.setYear(splited[1]);
obj.setMonth(splited[0]);
obj.setBudget(budget);
obj.setID(id);
// mySection.put(obj);
laptops.add(obj);
this.notifyDataSetChanged();
ArrayList<ItemDetails> productList = obj.getProductList();
ItemDetails detailInfo = new ItemDetails();
productList.add(detailInfo);
obj.setProductList(productList);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getCount() {
return laptops.size();
}
public void removeItem(long position) {
laptops.remove(position);
notifyDataSetChanged();
}
public ListObj getItem(int position) {
return laptops.get(position);
}
public View getChildView(final int groupPosition, final 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.child_item, null);
}
Button editButton = (Button)convertView.findViewById(R.id.editButton);
Button deleteButton = (Button)convertView.findViewById(R.id.deleteButton);
deleteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(parentActivity);
builder.setMessage("Do you want to remove?");
builder.setCancelable(false);
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Toast.makeText(context,"button clicked "+ groupPosition,Toast.LENGTH_LONG).show();
ListObj headerInfo = laptops.get(groupPosition);
deleteData(headerInfo.getID());
removeItem(groupPosition);
Toast.makeText(context,"list deleted",Toast.LENGTH_LONG).show();
notifyDataSetChanged();
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
return convertView;
}
public void deleteData(long id)
{
database = mdb.getWritableDatabase();
database.delete(MyDatabaseHelper.TABLE_EXPENSES, MyDatabaseHelper.ID2 + "=?", new String[]{id + ""});
}
public int getChildrenCount(int groupPosition) {
ArrayList<ItemDetails>itemDetails=laptops.get(groupPosition).getProductList();
return itemDetails.size();
}
public Object getGroup(int groupPosition) {
return laptops.get(groupPosition);
}
public int getGroupCount() {
return this.laptops.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
convertView = mInflater.inflate(R.layout.expenses_adapter, null);
TextView month = (TextView) convertView.findViewById(R.id.textMonth);
TextView budget = (TextView) convertView.findViewById(R.id.textAmount);
TextView year = (TextView) convertView.findViewById(R.id.textYear);
month.setText(laptops.get(groupPosition).getMonth());
budget.setText(laptops.get(groupPosition).getBudget()+"");
year.setText(laptops.get(groupPosition).getYear());
return convertView;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
我的适配器类正在扩展 BaseExpandableListAdapter
答案 0 :(得分:2)
首先:您需要更改hasStableIds
,因为列表中的删除和添加操作将更改列表的索引,因此true
意味着,可扩展列表视图可以重复使用针对相应ID的视图和它可能导致不必要的重复使用脏(删除)视图的问题
public boolean hasStableIds() {
return false;
}
第二:正如你所提到的,你需要使用int
代替long
,因为这样做
laptops.remove(position);
long
位置将AutoBoxed
转换为Long
,因此它会调用remove
的被覆盖版本,其中Object
即ArrayList#remove(Object)
结果
laptops.remove(LongObject);
所以虽然列表中没有Long
对象,但它会尝试从ArrayList
搜索并删除Long
对象(其值是位置)。
为什么拳击?
按照
的规则int将被提升为long但不会自动允许inverse(导致数据丢失)
所以long
将boxed
Long
调用更具体的移除方法ArrayList#remove(Object)
解决方案:使用int
或使用int
将remove
应用为<{1}}
laptops.remove((int)position);
答案 1 :(得分:1)
将long
更改为int
并将hasStableIds
设置为false后,它就像魅力一样。
public void removeItem(int position) {
laptops.remove(position);
notifyDataSetChanged();
}
我传递了错误的数据类型。但很奇怪,我没有收到任何错误。