gpt4 book ai didi

java - ListView 项目未在 BaseExpandableListAdapter 中删除

转载 作者:行者123 更新时间:2023-11-29 04:24:18 24 4
gpt4 key购买 nike

我有一个 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

enter image description here

最佳答案

首先:您需要更改hasStableIds,因为对列表的删除和添加操作会更改列表的索引,所以true意味着,可扩展 ListView 可以重用对应的 View ID,它可能会导致不必要地重用脏(已删除) View 的问题

public boolean hasStableIds() {
return false;
}

第二:正如您提到的,您需要使用 int 而不是 long 因为这样做

 laptops.remove(position);

long 位置将被AutoBoxed 放入Long 因此它将调用覆盖版本的remove 对象ArrayList#remove(Object)这导致

 laptops.remove(LongObject); 

因此它将尝试从 ArrayList 中搜索并删除一个 Long 对象(其值是位​​置),尽管没有 Long对象存在于列表中。

为什么是拳击?

按照规则

Widening Primitive Conversion

int will be promoted to long but inverse is not automatically allowed (result in lost of data)

因此 long 将被装箱Long 以调用更具体的删除方法 ArrayList#remove(Object)

解决方案:使用 int 或在使用 remove as

时将转换应用于 int
 laptops.remove((int)position);

关于java - ListView 项目未在 BaseExpandableListAdapter 中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47206166/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com