0

我是android的noob,我無法用自定義適配器實現listview。我的適配器行的佈局由一個imageview,文本和兩個按鈕組成。這兩個按鈕對應於填充該特定行的數據。當列表視圖最初加載時,一切都很好,因爲這些按鈕對應於它們的特定行。但是,滾動後,特定按鈕不再與該行中列出的數據相對應。例如,在滾動之前,如果我單擊第5行中的某個按鈕,它將返回第5行,但在滾動後,如果我單擊第20行中的某個按鈕,它將返回第3行。任何解決此問題的幫助都將不勝感激。爲什麼ListView適配器在滾動後失去位置?

MY適配器類

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; 

    } 

} 

回答

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); 
+0

感謝響應。我如何以及在哪裏實施?我是否在convertView之外編寫另一種方法? – DollaBill

+0

更新了我的答案 – user2729200

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; 

} 


} 
相關問題