ma3*_*a34 2 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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个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);
Run Code Online (Sandbox Code Playgroud)
这该怎么做?
小智 8
您可以尝试使用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;
}
}
Run Code Online (Sandbox Code Playgroud)