如何使用自定义baseadapter从listView中删除项目

时间:2015-05-22 14:33:18

标签: android listview

我遇到了一个问题,我有一个列表视图,我想在按钮点击时从中删除一个项目。我可以在互联网上找到例子,但我不能让他们工作。我怎样才能做到这一点?

这是我的代码:

public class GroupRequestCustomAdapter extends BaseAdapter {
    String [] result;
    Context context;
    String [] imageId;
    String [] groupId;

private static LayoutInflater inflater=null;

public GroupRequestCustomAdapter(GroupRequests ListActivity, String[] prgmNameList, String[] prgmImages, String [] GroupId) {

    // TODO Auto-generated constructor stub

    result=prgmNameList;
    context=ListActivity;
    imageId=prgmImages;
    groupId = GroupId;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return result.length;

}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public class Holder
{
    TextView tv;
    ImageView img;
    Button accept;
    Button decline;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;
    //Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), imageId[position]);
    //Bitmap bitmap = getBitmapFromURL(imageId[position]);
    rowView = inflater.inflate(R.layout.group_request_list, null);
    holder.tv=(TextView) rowView.findViewById(R.id.textView1);
    holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
    holder.tv.setText(result[position]);
    holder.accept=(Button) rowView.findViewById(R.id.buttonAccept);
    holder.decline=(Button) rowView.findViewById(R.id.buttonDecline);

    if (holder.img != null) {
        new GetImageFromUrl(holder.img,context).execute(imageId[position]);
    }

    holder.accept.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.v("Button click", "accept");
        }
    });
    holder.decline.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.v("Button click", "Decline");

        }
    });
    return rowView;

}
}

1 个答案:

答案 0 :(得分:1)

您可以在适配器中添加方法来为您执行此操作:

public void deleteItem (int position) {
    result.remove(position);
    // remove(int) does not exist for arrays, you would have to write that method yourself or use a List instead of an array
    notifyDataSetChanged();
}

public void deleteItem (String itemToDelete) {
    result.remove(itemToDelete);
    // remove(int) does not exist for arrays, you would have to write that method yourself or use a List instead of an array
    notifyDataSetChanged();
}

顺便说一句,您的getItem(int position)实施错误,应该返回result.get(position)getItemId也应该返回一个有意义的ID;在你的情况下,我认为返回的位置很好。

编辑

我注意到你的构造函数有3个不同的列表。因此,您很难处理数据集中的更改,因为如果从结果中删除项目,则还必须从其他列表中删除相应的项目。我强烈建议您重新定义适配器,如下所示:

public GroupRequestCustomAdapter(GroupRequests ListActivity, List<MyObject>)

MyObject将是一个非常简单的类,包含ListView中一个项目的数据,例如:

public class MyObject {
    private String prgmName;
    private String prgmImage;
    // other required fields.

    // required getters/setters
}

这样,您只需要在适配器内部管理一个列表,而不是像现在这样管理resultimageIdgroupId。可以在List上调用remove(int position)。

最后,您还可以考虑延长ArrayAdapter而不是BaseAdapter,它有一些BaseAdapter没有用的有用方法,包括......您猜对了: remove方法;)