我知道我的问题解决了很多Stack Overflow问题。我尝试了大部分解决方案,但没有一个成功。
我正在尝试实现listview的listrow删除。如果有两个选项delete和cancel,则会弹出listrow alert的longclick。当按下delete时,该行将被删除自定义适配器以及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;
}
}
任何人都可以帮助我???
答案 0 :(得分:1)
您没有从countryList
中的MyCustomAdapter
删除任何内容。
我看到了一个添加内容的功能,但我没有看到删除内容。
添加一项功能以从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();
}
答案 1 :(得分:0)
确定我可以帮助您,您将在自定义适配器上再添加一项功能
public void removeElementAtPosition(int position){
this.countryList.remove(position);
notifyDataSetChanged();
}
并删除长按功能中的适配器项目,如
dataAdapter.removeElementAtPosition(position);
您不想再次更新我们的适配器。您将检查并避免空指针异常
所有适配器都可以使用此功能
public int getCount(){
this.countryList.size();
}
因此,如果您使用此getcount函数 BASE ADAPTER
,您将影响此适配器中的列表,您的列表项将减少http://developer.android.com/reference/android/widget/BaseAdapter.html
一切顺利......