0

私はAndroidの初心者で、カスタムアダプターを使用してリストビューを実装するのに問題があります。私のアダプター行のレイアウトは、イメージビュー、テキスト、および 2 つのボタンで構成されています。2 つのボタンは、その特定の行に入力されるデータに対応しています。リストビューが最初に読み込まれると、ボタンが特定の行に対応するため、すべてがうまく機能します。ただし、スクロールすると、特定のボタンがその行にリストされているデータに対応しなくなります。たとえば、スクロールする前に行 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

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 に答える