gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:38:38 24 4
gpt4 key购买 nike

我写了一段代码来在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com