我想從 listview 子類的 onClick 事件中獲取點擊的 row。我能獲取點擊 item 但是不能在那個事件中點擊 row。如何解決這個問題。
代碼如下:
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Holder holder = new Holder();
pos = position;
if(convertView == null)
{
System.out.println(position);
convertView= inflater.inflate(R.layout.list_layout,(ViewGroup)parent.findViewById(R.layout.list_layout));
holder.form_name = (TextView) convertView.findViewById(R.id.list_text);
holder.form_details = (TextView) convertView.findViewById(R.id.button_text);
holder.form_details.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println(pos);
//Here i am able to get clicked view but pos has always
// last value
System.out.println("button view clicked for "+cipt_list.get(pos).cipt);
}});
System.out.println(holder.form_name);
convertView.setTag(holder);
}
else{
holder = (Holder) convertView.getTag();
}
ciptDetails cipt = cipt_list.get(position);
holder.form_name.setText(cipt.cipt);
return convertView;
}
在點擊監聽器前設置:
holder.form_details.setId(position);
在 onClick 方法中使用 v.getId()
就會給出位置。