1

我正在使用自定义适配器来显示带有文本视图和图像视图的列表视图。

在此处输入图像描述

这是我的输出屏幕。现在我想从列表视图中删除一个项目,我通过使用下面的代码来实现

 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 在执行删除后没有更新项目。请给我建议。

@但丁

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();
4

2 回答 2

1

也从您的 tempValues 中删除相同的索引。你没有从那里移除它,所以你怎么能反映。

tempValues.remove(position);
notifyDatsetChanged();
于 2013-10-11T07:26:17.830 回答
1

data在调用 notifyDataSetChanged() 之前,您还应该从数组中删除对象

更改此代码

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();


    }
});
于 2013-10-11T07:28:00.163 回答