0

我是 android 的菜鸟,我在使用自定义适配器实现 listview 时遇到了麻烦。我的适配器行的布局由一个图像视图、文本和两个按钮组成。这两个按钮对应于填充该特定行的数据。当列表视图最初加载时,一切正常,因为按钮对应于它们的特定行。但是,在我滚动之后,特定的按钮不再对应于该行中列出的数据。例如,在滚动之前,如果我单击第 5 行中的按钮,它会返回第 5 行,但在滚动后,如果我单击第 20 行中的按钮,它会返回第 3 行。非常感谢任何解决此问题的帮助。

我的适配器类

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;

    }

}
4

2 回答 2

2

每次调用 getView 时更新按钮标签。它应该可以正常工作。

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);
于 2013-09-26T16:03:03.123 回答
0

试试这个代码:

   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;

}


}
于 2013-09-26T16:31:32.213 回答