我有一个ListView,每个项目都有一个删除按钮。我想按下按钮时要从数据中删除的项目(HashMaps的一个ArrayList)和从ListView中删除。
这是我的代码:
BaseAdapter:
public class MyCustomAdapter extends BaseAdapter{
private ArrayList<HashMap<String, Object>> list;
private Context context;
public MyCustomAdapter(ArrayList<HashMap<String, Object>> list,Context context){
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.addresslist_item, null);
TextView address = (TextView) view.findViewById(R.id.listtxtaddress);
String add = String.valueOf(list.get(position).get("address"));
address.setText(add);
Button btndelete = (Button) view.findViewById(R.id.listbtndelete);
btndelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("check", "item_positiona:"+position);
Log.d("check", "lista:"+list.toString());
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Τιτλος επιχειρησησης");
builder.setMessage("Θέλετε να διαγάρψετε αυτή τη διεύθυνση;");
builder.setNegativeButton("NAI", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Log.d("check", "item_positionb:"+position);
list.remove(position);
notifyDataSetChanged();
}
});
builder.setPositiveButton("AKYPO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog abc = builder.create();
abc.show();
}
});
}
return view;
}
}
我的问题是该项目已从数据中删除,但当ListView更新时,只删除了最后一项,而不是我选择的项目。任何想法为什么会发生这种情况?在此先感谢。
答案 0 :(得分:1)
ArrayList
发生更改后,您必须致电notifyDataSetChanged()
更新ListView
并进行更改。这可以在适配器内部或外部完成。所以,例如:
public class MyCustomBaseAdapter extends BaseAdapter {
//TIP: Don't make this static, that's just a bad idea
private ArrayList<Contact> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<Contact> initialResults) {
searchArrayList = initialResults;
mInflater = LayoutInflater.from(context);
}
public void updateResults(ArrayList<Contact> results) {
searchArrayList = results;
//Triggers the list update
notifyDataSetChanged();
}
}
答案 1 :(得分:0)
修改数据后,在适配器对象上调用notifyDataSetChanged()
应该刷新列表视图。
你可以试试这个 -
在适配器中创建一个私有类ViewHolder。此类应包含列表项所具有的所有视图。
private static class ViewHolder {
TextView address;
String add;
Button btndelete;
}
然后在getView()里面:
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(R.layout.addresslist_item, null);
viewHolder.address = (TextView) view.findViewById(R.id.listtxtaddress);
viewHolder.add = String.valueOf(list.get(position).get("address"));
viewHolder.address.setText(add);
viewHolder.btndelete = (Button) view.findViewById(R.id.listbtndelete);
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.btndelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("check", "item_positiona:"+position);
Log.d("check", "lista:"+list.toString());
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Τιτλος επιχειρησησης");
builder.setMessage("Θέλετε να διαγάρψετε αυτή τη διεύθυνση;");
builder.setNegativeButton("NAI", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Log.d("check", "item_positionb:"+position);
list.remove(position);
notifyDataSetChanged();
}
});
builder.setPositiveButton("AKYPO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog abc = builder.create();
abc.show();
}
});
}
}
return convertView;
}
答案 2 :(得分:0)
您也可以尝试这种方式.. 创建一个Listener接口
package xyz.listeners;
public interface DeleteClickListener {
public void onDeleteClick(int position);
}
比你的具有listview的行为/片段 -
private DeleteClickListener mDeleteClickListener;
mDeleteClickListener = new DeleteClickListener() {
@Override
public void onDeleteClick(int position) {
list.remove(position);
adapter.notifyDataSetChanged();
}
};
在适配器类中,构造函数传递该侦听器。
public MyCustomAdapter(ArrayList<HashMap<String, Object>> list,Context context,DeleteClickListene mDeleteClickListener){
this.list = list;
this.context = context;
deleteClickListener=mDeleteClickListener;
}
然后单击“删除”按钮
builder.setNegativeButton("NAI", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
deleteClickListener.onDeleteClick(position);
}
});