gpt4 book ai didi

android - 如何从自定义 ListView 数组适配器类中的 ListView 中删除行并刷新 ListView 中的剩余项目?

转载 作者:行者123 更新时间:2023-11-29 19:03:07 27 4
gpt4 key购买 nike

我是 Android 的新手,正在努力提高编程技能。

在这里,我有自定义 ListView 适配器类,我曾在其中显示 ListView 项目,如 TextView 、图像等。

我需要做的是,我在 ListView 中有删除按钮,如果我点击删除按钮, ListView 中的特定行必须被删除,并且需要刷新 ListView 中剩余的行项目。

代码在这里:

public class CustomListViewAdapter1 extends ArrayAdapter<ListView_Model> {
private Activity activity;
List<ListView_Model> books;


public CustomListViewAdapter1(Activity activity, int resource, List<ListView_Model> books) {
super(activity, resource, books);
this.activity = activity;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// If holder not exist then locate all view from UI file.
if (convertView == null) {
// inflate UI from XML file
convertView = inflater.inflate(R.layout.item_listview, parent, false);
// get all UI view
holder = new ViewHolder(convertView);
// set tag for holder
convertView.setTag(holder);

} else {
// if holder created, get tag from view
holder = (ViewHolder) convertView.getTag();
}

ListView_Model book = getItem(position);

holder.name.setText(book.getName());
holder.authorName.setText(book.getAuthorName());
holder.mobileNo.setText(book.getPhNo());
holder.imgView_lv_delete_btn2= (ImageView)
convertView.findViewById(R.id.listview_delete_btn2);
holder.imgView_lv_delete_btn2.setTag(position);


holder.imgView_lv_delete_btn2.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
//I have commented this line here which is not working

/*Integer index = (Integer)v.getTag();
System.out.println("Index is:"+index);

books.remove(index.intValue());
notifyDataSetChanged();*/
}
});
return convertView;
}

private static class ViewHolder {
private ImageView images,imgView_lv_edit_btn,imgView_lv_view_btn1,imgView_lv_delete_btn2;

private TextView name;
private TextView authorName,mobileNo;

public ViewHolder(View v) {
name = (TextView) v.findViewById(R.id.title);
images = (ImageView) v.findViewById(R.id.thumbnail);
authorName = (TextView) v.findViewById(R.id.author);
mobileNo = (TextView) v.findViewById(R.id.MobileNo);

imgView_lv_edit_btn = (ImageView) v.findViewById(R.id.listview_edit_btn);
imgView_lv_view_btn1 = (ImageView) v.findViewById(R.id.listview_view_btn1);
}
}}

我在上面的代码中注释了这一行,因为它不起作用。

          /*Integer index = (Integer)v.getTag();
System.out.println("Index is:"+index);

books.remove(index.intValue());
notifyDataSetChanged();*/

我在这里犯了什么错误,为什么会在这里抛出这个错误?

抛出的错误是:

java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.remove(int)' on a null object reference at CustomListViewAdapter1$1.onClick(CustomListViewAdapter1.java:67)

最佳答案

您没有在 constructor 中分配变量 books

public CustomListViewAdapter1(Activity activity, int resource, List<ListView_Model> books) {
super(activity, resource, books);
this.activity = activity;
this.books = books; // The missing line
}

现在您的注释代码应该可以工作了!

关于android - 如何从自定义 ListView 数组适配器类中的 ListView 中删除行并刷新 ListView 中的剩余项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48149744/

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