gpt4 book ai didi

android - 从listview android中删除了错误的行

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

我知道有很多 Stack Overflow 问题针对我的问题得到了解决。我尝试了大部分解决方案,但没有一个对我成功。

我正在尝试实现 ListView 的列表行删除。长按列表行警报将弹出,其中有两个选项删除和取消。当按下删除时,将在自定义适配器中删除该行以及使用 mysql 表中的行异步任务。但只有最后一行被删除。

我已将 notifyDataSetChanged() 设置到我的适配器。即便如此,它也无法正常工作。

这是我的代码:

     listView.setOnItemLongClickListener(new OnItemLongClickListener() {

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
System.out.println("postition value::" + position);
removeItemFromList(position);

return true;
}
});


protected void removeItemFromList(int position) {
final int deletePosition = position;
System.out.println("deleting postition::"+deletePosition);
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());

alert.setTitle("Delete");
alert.setMessage("Do you want delete this item?");
alert.setPositiveButton("YES", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TOD O Auto-generated method stub

// main code on after clicking yes
new removefromfav().execute();
courselist.remove(deletePosition);
// dataAdapter.remove(dataAdapter.getItem(deletePosition));
dataAdapter.notifyDataSetChanged();
listView.setAdapter(dataAdapter);
// dataAdapter.notifyDataSetInvalidated();

}
});
alert.setNegativeButton("CANCEL", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});

alert.show();

}

我的适配器类,

   private class MyCustomAdapter extends ArrayAdapter<Course> {

private ArrayList<Course> countryList;

public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<Course> countryList) {
super(context, textViewResourceId, countryList);
this.countryList = new ArrayList<Course>();
this.countryList.addAll(countryList);
}

private class ViewHolder {
TextView code;
TextView name;
ImageView cover;
TextView cost;
ImageView ratingshow;
ImageView promoimage;
TextView enroll;
}

public void add(Course country) {

this.countryList.add(country);
}

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

ViewHolder holder = null;

if (convertView == null) {

LayoutInflater vi = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.course_overview, null);

holder = new ViewHolder();
holder.code = (TextView) convertView
.findViewById(R.id.coursename);
holder.name = (TextView) convertView.findViewById(R.id.author);
holder.cost = (TextView) convertView.findViewById(R.id.cost);
holder.cover = (ImageView) convertView.findViewById(R.id.cover);
holder.ratingshow = (ImageView) convertView
.findViewById(R.id.ratingimage);
holder.promoimage = (ImageView) convertView
.findViewById(R.id.promoimage);
holder.enroll = (TextView) convertView
.findViewById(R.id.enroll);
convertView.setTag(holder);

} else {
holder = (ViewHolder) convertView.getTag();
}

Course country = this.countryList.get(position);
holder.code.setText(country.getCode());
holder.name.setText(country.getName());
holder.cost.setText("$ " + country.getRegion());
holder.cover.setImageBitmap(country.getBitmap());




return convertView;

}

}

谁能帮帮我???

最佳答案

您没有从 MyCustomAdapter 中的 countryList 中删除任何内容。我看到一个功能,您可以在其中添加内容,但我没有看到一个删除内容的功能。

添加一个函数以从您的 countryList 中删除项目:

private class MyCustomAdapter extends ArrayAdapter<Course> {

private ArrayList<Course> countryList;

public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<Course> countryList) {
super(context, textViewResourceId, countryList);
this.countryList = new ArrayList<Course>();
this.countryList.addAll(countryList);
}

private class ViewHolder {
TextView code;
TextView name;
ImageView cover;
TextView cost;
ImageView ratingshow;
ImageView promoimage;
TextView enroll;
}

public void add(Course country) {

this.countryList.add(country);
}

//ADD THIS FUNCTION
public void remove(int index) {
this.countryList.remove(index)
}

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

ViewHolder holder = null;

if (convertView == null) {

LayoutInflater vi = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.course_overview, null);

holder = new ViewHolder();
holder.code = (TextView) convertView
.findViewById(R.id.coursename);
holder.name = (TextView) convertView.findViewById(R.id.author);
holder.cost = (TextView) convertView.findViewById(R.id.cost);
holder.cover = (ImageView) convertView.findViewById(R.id.cover);
holder.ratingshow = (ImageView) convertView
.findViewById(R.id.ratingimage);
holder.promoimage = (ImageView) convertView
.findViewById(R.id.promoimage);
holder.enroll = (TextView) convertView
.findViewById(R.id.enroll);
convertView.setTag(holder);

} else {
holder = (ViewHolder) convertView.getTag();
}

Course country = this.countryList.get(position);
holder.code.setText(country.getCode());
holder.name.setText(country.getName());
holder.cost.setText("$ " + country.getRegion());
holder.cover.setImageBitmap(country.getBitmap());




return convertView;

}
}

然后在对话框的 onClick 回调中调用它

public void onClick(DialogInterface dialog, int which) {
// TOD O Auto-generated method stub

// main code on after clicking yes
new removefromfav().execute();
dataAdapter.remove(which);
dataAdapter.notifyDataSetChanged();

}

关于android - 从listview android中删除了错误的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26353228/

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