我有一个自定义listView,可以为每行加载TextView,ImageView和复选框。
管理我使用的单个项目
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
long x = MyClass.this.adapter.getItemId(position);
......
}
});
这允许我获取单击行的id x,我可以使用它来检索与单行关联的数据。
现在:
如何获取所有点击的行的各种位置索引?
我的ListAdapter是
public class MyAdapter extends BaseAdapter{
private Activity activity;
private String[] dataCar;
private String[] dataImm;
private static LayoutInflater inflater=null;
public TitleLoader titleLoader;
public ImageLoader imageLoader;
public TextLoader textLoader;
public MyAdapter(Activity a, String[] dCar, String[] dImm) {
activity = a;
dataCar=dCar;
dataImm=dImm;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
titleLoader=new TitleLoader(activity.getApplicationContext());
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return dataImm.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.elemento_lista_car, null);
TextView car=(TextView)vi.findViewById(R.id.car);
ImageView image=(ImageView)vi.findViewById(R.id.image);
CheckBox cb=(CheckBox)vi.findViewById(R.id.checkBox);
titleLoader.DisplayTitle(dataCar[position], car);
imageLoader.DisplayImage(dataImm[position], image);
return vi;
}
}
答案 0 :(得分:0)
我认为你应该使用onListItemClick
我给你举个例子
public void onListItemClick(ListView parent,View v,int position,long id){
try{
TextView t4=(TextView)v.findViewById(R.id.Name);
TextView t5=(TextView)v.findViewById(R.id.Desc);
if(t4!=null){
EEActionListBean mSelected; // bean ur set get class
int idx=position;//
mSelected=m_adapter.getItem(idx); // this will give you position clicked
String getvalueoflist=mSelected.getID();
Intent intent=new Intent(this,EEActionListDetail.class);
intent.putExtra("sID",ActionID);
startActivity(intent);
}
}
catch(Exception e){
//MsgBox(this,e.toString());
}
}
答案 1 :(得分:0)
正如Luksprog所建议的那样,为了解决这个问题,我使用了一个布尔数组。 当我单击各个条目时,各个位置设置为true。使用此数组中的true元素索引,我可以访问列表中所有已检查的行
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
long x = MezzoStampa.this.adapterListaTestate.getItemId(position);
CheckBox cb=(CheckBox)view.findViewById(R.id.checkBox);
cb.setChecked(true);
if(cb.isChecked())
checked[(int)x]=true;
}
});