[英]How to handle duplicated Item in custom Listview extends BaseAdapter?
[英]How can I associate checkbox for each items of custom listview which extends BaseAdapter
我有自定義列表顯示圖像,其名稱如下...
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private String[] data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, String[] d) {
activity = a;
data=d;
inflater =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView text;
public ImageView image,yesimage;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.item, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.text);;
holder.image=(ImageView)vi.findViewById(R.id.image);
holder.yesimage=(ImageView)vi.findViewById(R.id.selectedyes);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.text.setText("item "+position);
holder.image.setTag(data[position]);
holder.yesimage.setTag(data[position]);
imageLoader.DisplayImage(data[position], activity, holder.image,holder.yesimage);
return vi;
}
}
對於這個列表我想設置,
list=(ListView)findViewById(R.id.list);
adapter=new LazyAdapter(this, mStrings);//mstrings is array
list.setAdapter(adapter);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
在哪里我可以使用,所有檢查列表項目,
list.setOnItemClickListener(new OnItemClickListener(){
TextView selected=(TextView)findViewById(R.id.selected);
final ArrayList<String> years = new ArrayList<String>();
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
SparseBooleanArray checked = list.getCheckedItemPositions();
if(checked.get(arg2))
{
years.add(""+mStrings[arg2]+"\n");
}
else
{
years.remove(" "+mStrings[arg2]+"\n");
}
if(years.size()!=0)
selected.setText(years.toString());
else
selected.setText("You Have Nothing in Cart");
}});
但沒有任何內容在列表中表明選中的項目已被選中,它只是將點擊的項目視為已選中,此處我無法使用,
list.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, mStrings));
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
因為我的列表擴展了BaseAdapter,
我想為列表中的每一行添加復選框,以確保在檢查該列表項后檢查該項目,我該怎么做...?
在ViewHolder中選擇CheckBox並為每個復選框實現setOnCheckedChangeListener 。
例如:
holder.checkbox = ( CheckBox ) vi.findViewById( R.id.checkbox );
holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
}
}
});
在checkBox上設置setOnCheckedChangeListener並檢查其狀態。 根據代碼,我認為你將不會遇到任何問題。 你想點擊chackBox做一些特定的功能嗎? 如果是,請分享。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.