我的 listView 有问题。我使用自定义适配器类显示了列表项。我的问题是,当我单击位置 1 的列表视图按钮时,位置 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;
}