我是Android的菜鸟,我在使用自定义适配器实现listview时遇到了麻烦。我的适配器行的布局包括imageview,文本和两个按钮。这两个按钮对应于填充该特定行的数据。当listview最初加载时,一切都很好,因为按钮对应于它们的特定行。但是,滚动后,特定按钮不再对应于该行中列出的数据。例如,在滚动之前如果我单击第5行中的按钮它将返回第5行,但是在滚动之后如果我单击第20行中的按钮则返回第3行。非常感谢任何解决此问题的帮助。
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;
}
}
答案 0 :(得分: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);
答案 1 :(得分: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;
}
}