我使用自定义适配器显示包含文本视图和图像视图的列表视图。
这是我的输出屏幕。现在我想从listview中删除一个项目,并通过使用以下代码
来实现 holder.img_delete.setTag(tempValues.getCompanyName());
holder.img_delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
eventDB.open();
eventDB.DeleteByText(view.getTag().toString());
eventDB.close();
Toast.makeText(ctx, "Deleted", 10).show();
notifyDataSetChanged();
}
});
现在问题是listView在执行删除后没有更新项目。请给我一些建议。
@Dante
public class CustomAdapter extends BaseAdapter implements OnClickListener {
private ArrayList data;
private static LayoutInflater inflater=null;
public Resources res;
ListModel tempValues=null;
int i=0;
Context ctx;
EventCate_DBAdapter eventDB;
public CustomAdapter(Context c, ArrayList d,Resources resLocal) {
data=d;
res = resLocal;
ctx=c;
inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
eventDB = new EventCate_DBAdapter(ctx);
}
public int getCount() {
if(data.size()<=0)
return 1;
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView text;
public static ImageView img_edit,img_delete;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.tabitem, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.tv_event_cat);
holder.img_edit=(ImageView)vi.findViewById(R.id.iv_edit);
holder.img_delete=(ImageView)vi.findViewById(R.id.iv_delete);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
if(data.size()<=0)
{
holder.text.setText("No Data");
}
else
{
tempValues=null;
tempValues = (ListModel) data.get(position);
holder.text.setText(tempValues.getCompanyName());
vi.setOnClickListener(new OnItemClickListener(position));
}
holder.img_edit.setTag(tempValues.getCompanyName());
holder.img_edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(ctx, "Edit = "+view.getTag().toString(), Toast.LENGTH_SHORT).show();
}
});
holder.img_delete.setTag(tempValues.getCompanyName());
holder.img_delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
eventDB.open();
eventDB.DeleteByText(view.getTag().toString());
eventDB.close();
Toast.makeText(ctx, "Deleted", 10).show();
notifyDataSetChanged();
notifyDataSetInvalidated();
}
});
return vi;
}
@Override
public void onClick(View v) {
Log.v("CustomAdapter", "=====Row button clicked");
}
private class OnItemClickListener implements OnClickListener{
private int mPosition;
OnItemClickListener(int position){
mPosition = position;
}
@Override
public void onClick(View arg0) {
SettingsActivity sct = (SettingsActivity)ctx;
sct.onItemClick(mPosition);
}
}
}
删除项目工作正常。我需要另一个更新列表项的解决方案。
这是我的编码
eventDB.open();
eventDB.update(Id.get(0), etEventCategory.getText().toString());
eventDB.close();
data.set(pos,etEventCategory.getText().toString());
dialog.dismiss();
notifyDataSetChanged();
notifyDataSetInvalidated();
答案 0 :(得分:1)
也从tempValues中删除相同的索引。你不是从那里删除它所以如何反映。
tempValues.remove(position);
notifyDatsetChanged();
答案 1 :(得分:1)
在调用notifyDataSetChanged()
之前,您还应该从data
数组中删除对象
更改为此代码
holder.img_delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
eventDB.open();
eventDB.DeleteByText(view.getTag().toString());
data.remove(position); // change position to final
eventDB.close();
Toast.makeText(ctx, "Deleted", 10).show();
notifyDataSetChanged();
//notifyDataSetInvalidated();
}
});