我已经有几天这个问题了,这似乎是我从" getItemViewType"得到的返回值。没有传递给" getView"正确。
当我调试它时,它首先返回" final_value"据说应该去" getView"但它没有,只进入" getView"在第二次返回最终值后,我不知道为什么,也许我的设计是错误的,我不确定。请帮忙,下面是我的代码:
public class myListAdapter extends ArrayAdapter<chatMessageObject> {
private static final String TAG = "TESTING";
private static final int TYPE1= 1;
private static final int TYPE2= 2;
private static final int TYPE3= 3;
private static final int TYPE4= 4;
private static final int TYPE5= 5;
private static List<String> date_list = new ArrayList<>();
private int mLayoutResource, type;
private Context mContext;
private String id, name;
public myListAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<myObject> objects) {
super(context, resource, objects);
mContext = context;
mLayoutResource = resource;
}
public static class ViewHolder{
TextView name,id;
}
@Override
public int getViewTypeCount() {
return 5;
}
@Override
public int getItemViewType(int position) {
int final_value = 0;
id = getItem(position).get_id();
name = getItem(position).get_name();
if (!Objects.equals(id, name)) {
if (Objects.equals(getItem(position).getName().trim(), "Test")) {
final_value = TYPE1;
}
else if (!date_list.contains(getItem(position).getDate().substring(0,10))) {
date_list.add(getItem(position).getDate().substring(0,10));
final_value = TYPE2;
}
else {
Log.d(TAG,"VIEW_TYPE_MESSAGE_RECEIVED...");
final_value = TYPE3;
}
} else {
if (Objects.equals(getItem(position).getName().trim(), "Test")) {
final_value = TYPE1;
}
else if (!date_list.contains(getItem(position).getDate().substring(0,10))) {
date_list.add(getItem(position).getDate().substring(0,10));
final_value = TYPE4;
}
else {
Log.d(TAG,"VIEW_TYPE_MESSAGE_SENT...");
final_value = TYPE5;
}
}
return final_value;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder holder;
int type = getItemViewType(position);
Log.d(TAG,"type = " + type);
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
holder = new ViewHolder();
switch (type) {
case TYPE1;
convertView = inflater.inflate(R.layout.test1, parent, false);
holder.name = convertView.findViewById(R.id.name_type1);
holder.id = convertView.findViewById(R.id.id_type1);
break;
case TYPE2;:
Log.d(TAG,"TYPE2");
convertView = inflater.inflate(R.layout.test2, parent, false);
holder.name = convertView.findViewById(R.id.name_type2);
holder.id = convertView.findViewById(R.id.id_type2);
break;
case TYPE3;
Log.d(TAG,"TYPE3");
convertView = inflater.inflate(R.layout.test3, parent, false);
holder.name = convertView.findViewById(R.id.name_type3);
holder.id = convertView.findViewById(R.id.id_type3);
break;
case TYPE4;
Log.d(TAG,"TYPE4");
convertView = inflater.inflate(R.layout.test4, parent, false);
holder.name = convertView.findViewById(R.id.name_type4);
holder.id = convertView.findViewById(R.id.id_type4);
break;
case TYPE5;
Log.d(TAG,"TYPE5");
convertView = inflater.inflate(R.layout.test5, parent, false);
holder.name = convertView.findViewById(R.id.name_type5);
holder.id = convertView.findViewById(R.id.id_type5);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText("TEST NAME");
holder.id.setText("ID");
return convertView;
}