gpt4 book ai didi

java - Android - 删除了 ArrayList 中的多个对象,但适配器无法正确更新

转载 作者:行者123 更新时间:2023-11-30 02:05:08 25 4
gpt4 key购买 nike

我有一个对象的 ArrayList,用作 ListView 的一行。我有一种方法,可以在长按 ListView 中删除并相应更新选中的项目。但是,当该应用程序运行时,所发生的只是那些选中的项目现在变为未选中状态——该项目根本没有被删除。

这是我的 onClick 代码。行对象是 checklistItems。

button.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
ArrayList<Checklist> temp = new ArrayList<Checklist>();
for (int i = checklistItems.size()-1; i >= 0; i--){
if (checklistItems.get(i).getValue() != 1)
temp.add(checklistItems.get(i));
}

checklistItems = temp;
myAdapter.notifyDataSetChanged();

return true;
}
});

我确信我过滤掉不需要的项目的程序是好的......除非它不是哈哈。我只是不明白为什么它会这样。任何帮助表示赞赏!

public class CustomAdapter extends ArrayAdapter<Checklist> {
ArrayList<Checklist> checklistItems;
Context context;

public CustomAdapter(Context context, ArrayList<Checklist> resource){
super(context, R.layout.list_view_row_item, resource);
this.context = context;
this.checklistItems = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(R.layout.list_view_row_item, parent, false);
TextView name = (TextView) convertView.findViewById(R.id.itemText);
CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox);

name.setText(checklistItems.get(position).getName());
if (checklistItems.get(position).getValue() == 1)
cb.setChecked(true);
else
cb.setChecked(false);
return convertView;
}

编辑:初始化适配器的 onCreate 方法:

public class MainActivity extends Activity {
private ListView lv;
ArrayList<Checklist> checklistItems;
CustomAdapter myAdapter;
Button button;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//读取项目();

    button = (Button) findViewById(R.id.addItem);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
shortclick();
}
});

button.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
ArrayList<Checklist> temp = new ArrayList<Checklist>();
for (int i = checklistItems.size()-1; i >= 0; i--){
if (checklistItems.get(i).getValue() == 1) // changed to == 1
temp.add(checklistItems.get(i));
}

//checklistItems = temp; //Don't change reference of Adapter data source

checklistItems.clear(); //added
checklistItems.addAll(temp); //added

myAdapter.notifyDataSetChanged();

return true;
}
});


lv = (ListView) findViewById(R.id.listView);
checklistItems = new ArrayList<Checklist>();
checklistItems.add(new Checklist(0,"When you add a new item it will show here"));
checklistItems.add(new Checklist(0,"Check the items that you've finished"));

myAdapter = new CustomAdapter(this,checklistItems);
lv.setAdapter(myAdapter);


}

shortClick 方法:

    public void shortclick()
{
EditText taskET = (EditText) findViewById(R.id.taskET);
String itemText = taskET.getText().toString();
Checklist newTask = new Checklist(0,itemText);
myAdapter.add(newTask);
taskET.setText("");

}

list 类:

public class Checklist {
int value;
String text;
boolean isChecked;

public Checklist(int value, String text){
this.value = value;
this.text = text;
}

public String getName(){
return this.text;
}

public int getValue(){
return this.value;
}

public void setValue(boolean isChecked){
if (value == 1)
isChecked = true;
else
isChecked = false;
}

最佳答案

一旦 Adapter 被设置为数据源,您就无法更改其数据源的引用。

获得所需结果的最简单方法是在数据源上调用 clear(),然后调用 addAll() 将新数据添加到原始数据来源。

button.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
ArrayList<Checklist> temp = new ArrayList<Checklist>();
for (int i = 0; i < checklistItems.size(); i++){
if (checklistItems.get(i).getValue() == 0)
temp.add(checklistItems.get(i));
}

//checklistItems = temp; //Don't change reference of Adapter data source

checklistItems.clear(); //added
checklistItems.addAll(temp); //added

myAdapter.notifyDataSetChanged();

return true;
}
});

编辑,您还需要使用复选框的监听器更新数据,如下所示:

    @Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(R.layout.list_view_row_item, parent, false);
TextView name = (TextView) convertView.findViewById(R.id.itemText);
CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox);

cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checklistItems.get(position).setValue(isChecked);
}
});

name.setText(checklistItems.get(position).getName());
if (checklistItems.get(position).getValue() == 1)
cb.setChecked(true);
else
cb.setChecked(false);
return convertView;
}

关于java - Android - 删除了 ArrayList 中的多个对象,但适配器无法正确更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30792982/

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