android - 单击 ListView 上的删除按钮后删除 ListView 上的项目

标签 android android-listview android-adapter

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

适配器类:

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 Activity类

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,如下所示:

 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;
}
}

关于android - 单击 ListView 上的删除按钮后删除 ListView 上的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19142630/

相关文章:

android - 用于数据访问代码生成的 sqlitegen

android - 键盘出现时保持 ListView 的底部元素可见

android - Listview多次调用getView方法

android - RecyclerView 和保存+检索每个适配器项的临时状态

android - 单击按钮从自定义 ListView 中删除项目

android - Gradle导入外部android库

java - Android - 调整可缩放 ImageView 顶部的 ImageView 位置

java - Android - 使用 GridLayout 垂直滚动

Android ListView 无法正确缩放

android - 从 fragment 类主 Activity 调用自定义 ListView 适配器