gpt4 book ai didi

Android - 通过 ArrayAdapter 从 ListView 中删除行时 ListItem id 发生变化

转载 作者:行者123 更新时间:2023-11-29 20:03:54 26 4
gpt4 key购买 nike

Android 开发的新手,我有一个问题。

我有一个带有 TextView 的 ListView。我有一个 ArrayAdapter,我要在单击按钮时删除一个 ListItem。在我的 ListActivity 中,我将 id 分配给 ListItems,问题是当我删除 ListItem 时,以下 ListItem 继承了它的 id。

即:

假设我有 2 个 ListItems:ListItem0 的 id 0ListItem 1 的 id 1。 ==> 如果我删除 ListItem0 那么 ListItem1 将获得 id 0

基本上有什么方法可以保留 ListItem id,即使我删除了其中的一些?

后期编辑:通过从 idList 中删除 id 解决:

idList.remove(position)

public void displayL() {
Cursor c = mDbAdapter.fetchAllReminders();
numberList.clear();
idList.clear();
startManagingCursor(c);
c.moveToFirst();
if (c .moveToFirst()) {
while (c.isAfterLast() == false) {
id= c.getString(c.getColumnIndex(RemindersDbAdapter.KEY_ROWID));
String name = c.getString(c.getColumnIndex(RemindersDbAdapter.KEY_TITLE));
numberList.add(name);
idList.add(id);
c.moveToNext();
}
}

adapter = new ItemAdapter(this, R.layout.row, numberList, idList);
setListAdapter(adapter);
adapter.notifyDataSetChanged();
}

适配器类:

public class ItemAdapter extends ArrayAdapter<String>  {
private ArrayList<String> idList = null;
private ArrayList<String> numberList;
private ImageView delete_entry;
public RemindersDbAdapter mDbAdapter;
public View v;
public ArrayList<String> clickId;
private long positionId;


public ItemAdapter(Context context, int textViewResourceId, ArrayList<String> numberList,
ArrayList<String> clickPosition) {
super(context, textViewResourceId, numberList);
this.numberList = numberList;
clickId = clickPosition;
}

@Override
public int getPosition(String item) {
return super.getPosition(item);
}

@Override
public long getItemId(int position) {
return super.getItemId(position);
}

public View getView(final int position, View convertView, ViewGroup parent){
v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.row, null);
}

mDbAdapter= new RemindersDbAdapter(v.getContext());
final String number = numberList.get(position);
mDbAdapter.open();

if (number != null) {
final TextView tt = (TextView) v.findViewById(R.id.text_number);
tt.setTextColor(Color.parseColor("#ffffff"));
delete_entry = (ImageView) v.findViewById(R.id.delete_entry);
tt.setText(number);
delete_entry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
positionId = Integer.parseInt(clickId.get(position));

if(position>=0) {
Log.d("Delete--", "DELETING ID #" + positionId);
Boolean b = mDbAdapter.deleteReminder(positionId); //delete from database
numberList.remove(number); //remove listItem
notifyDataSetChanged();

}
mDbAdapter.close();
}
});
}
return v;

最佳答案

当您从 numberList 中删除数字时,您似乎并没有从 idList 中删除 id,因此看起来它是被继承的。试试这个。

    public void onClick(View v) {
positionId = Integer.parseInt(clickId.get(position));

if(position>=0) {
Log.d("Delete--", "DELETING ID #" + positionId);
Boolean b = mDbAdapter.deleteReminder(positionId); //delete from database
numberList.remove(number); //remove listItem
idList.remove(position);
notifyDataSetChanged();

}
mDbAdapter.close();
}

关于Android - 通过 ArrayAdapter 从 ListView 中删除行时 ListItem id 发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35901310/

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