单击ListView上的删除按钮后,删除ListView上的项目 [英] Delete item on ListView after click delete button on ListView

查看:74
本文介绍了单击ListView上的删除按钮后,删除ListView上的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个代码来显示图像,按钮和在ListView中动态显示文本. 所以ListView加载按钮.我要通过单击此按钮删除ListView中的选定项目.

I wrote a code to display images, button & texts dynamically in a ListView. So ListView load the button. I want to delete the selected item in ListView by clicking this button.

适配器类:

public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private String[] data;
    private static LayoutInflater inflater=null;
    public ImageLoaderLogoUnder imageLoader; 

    public LazyAdapter(Activity a, String[] d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoaderLogoUnder(activity.getApplicationContext());
    }

    public int getCount() {
        return data.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.inflatelistview, null);

        TextView text=(TextView)vi.findViewById(R.id.textView1);
        ImageView image=(ImageView)vi.findViewById(R.id.imageView1);
        Button btn=(Button)vi.findViewById(R.id.button1);
        btn.setTag(position);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Integer index = (Integer) v.getTag();
                //items.remove(index.intValue());  
                notifyDataSetChanged();

            }
        });
        text.setText("item "+position);
        imageLoader.DisplayImage(data[position], image);
        return vi;
    }
}

这是一个ListView活动类

This is a ListView Activity class

ShowData show = new ShowData();

        String s[] = show.getData();
        adapter = new LazyAdapter(this, s);

        inflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        list = (ListView) findViewById(R.id.listView1);
        list.setAdapter(adapter);

该怎么做?

推荐答案

您可以尝试使用ArrayList代替,如下所示:

You can try using ArrayList instead as below:

 public class LazyAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<String> data;
private static LayoutInflater inflater=null;
public ImageLoaderLogoUnder imageLoader; 

public LazyAdapter(Activity a, ArrayList<String> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoaderLogoUnder(activity.getApplicationContext());
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.inflatelistview, null);

    TextView text=(TextView)vi.findViewById(R.id.textView1);
    ImageView image=(ImageView)vi.findViewById(R.id.imageView1);
    Button btn=(Button)vi.findViewById(R.id.button1);
    btn.setTag(position);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Integer index = (Integer) v.getTag();
            //items.remove(index.intValue());  
            data.remove(position);
            notifyDataSetChanged();

        }
    });
    text.setText("item "+position);
    imageLoader.DisplayImage(data.get(position), image);
    return vi;
}
}

这篇关于单击ListView上的删除按钮后,删除ListView上的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆