我的listView有問題。我使用自定義適配器類來顯示列表項目。 我的問題是,當我點擊位置1上的列表視圖按鈕時,位置10上的按鈕也被點擊。我如何克服這個問題?當我們在位置1點擊ListView按鈕時,第10個位置的按鈕也被點擊
這裏是我的代碼:
public static class Clockin_Group extends BaseAdapter implements Filterable {
private LayoutInflater mInflater;
private Context context;
public Clockin_Group(Context context) {
mInflater = LayoutInflater.from(context);
this.context = context;
}
@Override
public int getCount() {
if(employeeList==null){
return 0;
}
else{
return employeeList.length;
}
}
@Override
public Object getItem(int position) {
return employeeList[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null){
convertView = mInflater.inflate(R.layout.group_clkin_row, null);
holder = new ViewHolder();
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
holder.tv = (TextView) convertView.findViewById(R.id.group_name);
holder.tv.setText(employeeList[position]);
holder.tv1 = (TextView) convertView.findViewById(R.id.loc_id_tv);
holder.tv1.setText("["+empNo_Array[position]+"]");
holder.check = (ImageView) convertView.findViewById(R.id.checkmark);
holder.check.setVisibility(View.GONE);
holder.time = (TextView) convertView.findViewById(R.id.time);
holder.time.setText("Last Clock " +punchType_array[position]+" "+"at"+" "+punchTime_array[position]);
holder.clkin_tv = (TextView) convertView.findViewById(R.id.tv_clockin);
holder.clkin_tv.setVisibility(View.GONE);
holder.button = (Button) convertView.findViewById(R.id.group_button);
holder.button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
employee_id = Integer.parseInt(emp_idList[position]);
emp_selected = employeeList[position];
System.out.println("selected Emp.."+emp_selected);
boolean Status = false;
String type = "In";
SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd, yyyy HH:mm:ss");
Date date=new Date();
String s=sdf.format(date);
System.out.println("GMT: "+s);
try {
Status = sendDetails(corpId, user_name, password,employee_id,location_id_str, task_id_str, "0", type);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
if(Status){
holder.button.setVisibility(View.INVISIBLE);
holder.clkin_tv.setVisibility(View.VISIBLE);
holder.time.setTextColor(Color.parseColor("#088A08"));
holder.check.setVisibility(View.VISIBLE);
holder.time.setText("Last Clock IN at "+sdf.format(new Date()).toString());
System.out.println("Status..");
}
else{
Toast.makeText(context, "Clock In Failed", Toast.LENGTH_SHORT).show();
holder.button.setVisibility(View.VISIBLE);
holder.clkin_tv.setVisibility(View.INVISIBLE);
}
}
});
/*convertView.setOnClickListener(new OnClickListener() {
//private int pos = position;
@Override
public void onClick(View v) {
//Toast.makeText(context, "Click-" + String.valueOf(pos), Toast.LENGTH_SHORT).show();
}
});*/
convertView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(context, employeeList[position]+"["+empNo_Array[position]+"]", Toast.LENGTH_SHORT).show();
return false;
}
});
// convertView.setTag(holder);
return convertView;
}
http://stackoverflow.com/questions/8162835/button-events-in-listview – RobinHood 2012-07-27 06:39:56
你是如何解決這個問題的?標籤? – Sunkas 2013-03-20 08:57:38
無法關注您的代碼,電視,tv1,clkin_tv縮寫使其無法跟隨。 – Siddharth 2014-01-17 08:43:31