Question

I'm a noob to android and I am having trouble implementing a listview with a custom adapter. The layout for my adapter row consists of an imageview, text, and two buttons. The two buttons correspond to data populating that particular row. When the listview initially loads, everything works great, as the buttons correspond to their particular row. However, after I scroll, the buttons in a particular no longer correspond to the data listed in that row. For example, before scrolling if I click a button in row 5 it returns row 5, but after scrolling if I click a button in row 20 it returns row 3. Any help resolving this is greatly appreciated.

MY ADAPTER CLASS

public class MyAppointments_ListAdapter extends BaseAdapter implements OnClickListener {

private ArrayList<HashMap<String, String>> listData;     
private LayoutInflater layoutInflater;
Context c;
AlertDialog alert;
int selection;
String responce_for_push = null;
URL push_message = null;
public static String adapter_email;

public MyAppointments_ListAdapter(Context context, ArrayList listData) {
    this.listData = listData;
    layoutInflater = LayoutInflater.from(context);
    c = context;
}

@Override
public int getCount() {
    return listData.size();
}

@Override
public Object getItem(int position) {
    return listData.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.layout_row_myappointments, null);
        holder = new ViewHolder();
        holder.userAvatar = (ImageView) convertView.findViewById(R.id.client_image);
        holder.userName = (TextView) convertView.findViewById(R.id.username);
        holder.userStatus = (TextView) convertView.findViewById(R.id.status);
        holder.userTime = (TextView) convertView.findViewById(R.id.time);
        holder.profile = (ImageButton) convertView.findViewById(R.id.profile);
        holder.profile.setOnClickListener(MyAppointments_ListAdapter.this);
        holder.profile.setTag(position);
        holder.cancel = (ImageButton) convertView.findViewById(R.id.cancel);
        holder.cancel.setOnClickListener(MyAppointments_ListAdapter.this);
        holder.cancel.setTag(position);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    if(LandingActivity.user_isClient){
        holder.userName.setText((listData.get(position)).get("Barber_Username"));
    }else{
        holder.userName.setText((listData.get(position)).get("Username"));
    }

    if((listData.get(position)).get("Status").equals("PENDING")){ 
        holder.userStatus.setText( "Appointment Pending.");
    }else{
        holder.userStatus.setText( "Appointment Confirmed.");
    }
    holder.userTime.setText( (listData.get(position)).get("Time"));

    int loader = R.drawable.app_icon;       
    if(LandingActivity.user_isClient){
        String profilepicURL = "http://184.107.149.234/KutTime/clients/"+( listData.get(position)).get("Email")+"/profile.jpg";       
        // ImageLoader class instance
        ImageLoader imgLoader = new ImageLoader(c);
        imgLoader.DisplayImage(profilepicURL, loader, holder.userAvatar);
    }else{
        String profilepicURL = "http://184.107.149.234/KutTime/clients/"+( listData.get(position)).get("Client_Email")+"/profile.jpg";
        // ImageLoader class instance
        ImageLoader imgLoader = new ImageLoader(c);
        imgLoader.DisplayImage(profilepicURL, loader, holder.userAvatar);
    }



    return convertView;
}

static class ViewHolder {
    ImageView userAvatar;
    TextView userName;
    TextView userStatus;
    TextView userTime;
    ImageButton profile;
    ImageButton cancel;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.profile:      

        selection = (Integer)v.getTag();
        Log.e("Selection", String.valueOf(selection));

        if(LandingActivity.user_isClient){
            adapter_email = (listData.get(selection)).get("Email");
            Log.e("Adapter Email", adapter_email);
            Intent myIntent = new Intent("com.tpssquared.kuttime.PROFILE_BARBER_VIEW");
            myIntent.putExtra("ADAPTER_EMAIL", adapter_email);
            c.startActivity(myIntent);              
        }else{
            adapter_email = (listData.get(selection)).get("Client_Email");
            Log.e("Adapter Email", adapter_email);
            Intent myIntent = new Intent("com.tpssquared.kuttime.PROFILE_CLIENT_VIEW");
            myIntent.putExtra("ADAPTER_EMAIL", adapter_email);
            c.startActivity(myIntent);  
        }

        break;

    case R.id.cancel:       

        selection = (Integer)v.getTag();
        AlertDialog.Builder builder = new AlertDialog.Builder(c);
        builder.setTitle("Cancel Appointment");
        if(LandingActivity.user_isClient){
            builder.setMessage("Cancel appointment with " + (listData.get((Integer)v.getTag())).get("Username") + " at " + (listData.get((Integer)v.getTag())).get("Time")  );
        }else{
            builder.setMessage("Cancel appointment with " + (listData.get((Integer)v.getTag())).get("Barber_Username") + " at " + (listData.get((Integer)v.getTag())).get("Time")  );
        }
        builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {

                alert.dismiss();

            }
        });
        builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {

                cancelAppointment();

            }
        });

        alert = builder.create();
        alert.show();

        break;

    }

}
Was it helpful?

Solution

Update buttons tags each time when getView called. It should work fine.

if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.layout_row_myappointments, null);
        holder = new ViewHolder();
        holder.userAvatar = (ImageView) convertView.findViewById(R.id.client_image);
        holder.userName = (TextView) convertView.findViewById(R.id.username);
        holder.userStatus = (TextView) convertView.findViewById(R.id.status);
        holder.userTime = (TextView) convertView.findViewById(R.id.time);
        holder.profile = (ImageButton) convertView.findViewById(R.id.profile);
        holder.profile.setOnClickListener(MyAppointments_ListAdapter.this);
        holder.cancel = (ImageButton) convertView.findViewById(R.id.cancel);
        holder.cancel.setOnClickListener(MyAppointments_ListAdapter.this);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.cancel.setTag(position);
    holder.profile.setTag(position);

OTHER TIPS

Try this code :

   public class Adptr extends BaseAdapter  {
private  ArrayList<Model> aList;
private Activity activity;
private LayoutInflater layoutinflater;


public Adptr (ArrayList<Model> modelTemps, Activity activity) {
    super();
    this.aList = modelTemps;
    this.activity = activity;
}

@Override
public int getCount() {

    return aList.size();
}

@Override
public Object getItem(int position) {

    return aList.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    layoutinflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewHolder holder = null;
    // ModelTemp employeeLocalDataModel = aListStudent.get(position);
    if (convertView == null || !(convertView.getTag() instanceof ViewHolder)) {
        convertView = layoutinflater.inflate(R.layout.row_, null);
        holder = new ViewHolder();
        holder.txtCName = (TextView) convertView.findViewById(R.id.row_cart_ItemName);


        convertView.setTag(holder);

        convertView.setTag(R.id.R.id.row_cart_ItemName, holder.txtCartProductName);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }


    holder.txtName.setText(aList.get(position).getDesc());

    return convertView;
}

class ViewHolder {
    TextView txtName;

}


}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top