Reputation: 308
I have a problem with my listView. I displayed the list item using a custom adapter class. My problem is, when I click on the listview button at the position 1, the button at position 10 is also clicked. How do I overcome this problem?
Here is my code:
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;
}
Upvotes: 1
Views: 2336
Reputation: 312
// Write one set function inside adapter
public void setList(List<Employee> list) {
mEmployeeList = list;
notifyDataSetChanged();
}
//Create a member variable.
private List<Employee> mEmployeeList = new ArrayList<Employee>();
//Now in getView()
public View getView(int position, View convertView, ViewGroup parent) {
/Use like this
ViewHolder holder = (ViewHolder)convertView.getTag();
final Employee employee= mEmployeeList.get(position);
//Use employee to set all variable.
holder.button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
Log.i("Adapter", employee.id);
}});
}
// Call setList function from your main class pass employeeList and let me know the result.
Upvotes: 0
Reputation: 234
Check by setting your listView height and width wrap_content or fill_parent.
Upvotes: 2
Reputation: 625
Set the tag with position key not with holder.
Change
convertView.setTag(holder);
to
convertView.setTag(position);
chk..
Upvotes: 0
Reputation: 7527
actually convertView
concept is used to reuse the view and there are 10 views are being created initially and when you scroll the list onclick
listener is not being update.
you can use this tutorial to assign different onClickListener
to each of your items
Upvotes: 1
Reputation: 2415
I think you need to use tags.
ie
convertView.setTag(holder);
then inside your onClick get the tag and action from there
String currentPos = arg0.getTag().toString();
Integer.parseInt(currentPos)
Hope that makes sense
Upvotes: 1
Reputation: 146
The problem here is, every button in list is going to have same id.
Instead of writing onClik()
in base adapter you can write onItemClickListener()
in your activity which will give you position of the item in ListView
.
Upvotes: 1