当我向下滚动列表视图时,它显示错误的项目位置。 原始列表视图项目位置在屏幕中是正确的 但是当我向下滚动列表视图时,它出错了 然后我向上滚动列表视图所有项目位置将更改不是原始 position.my的问题是当我滚动列表视图时如何使getVeiw()位置正确的顺序。
这是我的代码:
MainActivity.java
公共类MainActivity扩展了AppCompatActivity {
private ImageButton btnExit;
private ListView listView;
ArrayList<String>list_name = new ArrayList<>();
ArrayList<String>list_time = new ArrayList<>();
ArrayList<String>list_message = new ArrayList<>();
ArrayList<String>list_image = new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnExit = findViewById(R.id.btnExit);
btnExit.setOnClickListener(btnExitListener);
listView = findViewById(R.id.listview);
for(int i = 0 ; i < 20 ; i++) {
list_name.add("User" + i);
list_time.add("2018-2-14");
list_message.add("hello!!");
list_image.add("http://a1.att.hudong.com/86/38/300001140423130278388082725_950.jpg");
}
ListAdapter adapter = new com.ncut.user.cloudtest.ListAdapter(MainActivity.this,list_name,list_time,list_message,list_image);
listView.setAdapter(adapter);
}
}
ListAdpter.java
public class ListAdapter extends ArrayAdapter<String> {
private final Activity context;
ArrayList<String>list_name = new ArrayList<>();
ArrayList<String>list_time = new ArrayList<>();
ArrayList<String>list_message = new ArrayList<>();
ArrayList<String>list_image = new ArrayList<>();
static class ViewHolder{
TextView name_text;
TextView time_text;
TextView message_text;
ImageView image;
}
public ListAdapter(Activity context, ArrayList<String>list_name, ArrayList<String>list_time,ArrayList<String>list_message
,ArrayList<String>list_image) {
super(context, R.layout.listview_chat_room_me, list_name);
this.context = context;
this.list_name = list_name;
this.list_time = list_time;
this.list_message = list_message;
this.list_image = list_image;
}
public View getView(int position , View view, ViewGroup parent ){
Log.d(TAG, "position=" + position);
ViewHolder holder = new ViewHolder();
if(view == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.listview_chat_room_me,parent,false);
holder.name_text = view.findViewById(R.id.txtName);
holder.time_text = view.findViewById(R.id.txtTime);
holder.message_text = view.findViewById(R.id.txtMessage);
holder.image = view.findViewById(R.id.imgPersonalPhoto);
}
else{
holder = (ViewHolder) view.getTag();
}
try {
holder.name_text.setText(list_name.get(position));
holder.time_text.setText(list_time.get(position));
holder.message_text.setText(list_message.get(position));
Glide.with(context).load(list_image.get(position)).into(holder.image);
}
catch (Exception e){
}
return view;
}
}
logcat的:
05-24 01:05:29.687 21886-21886/com.ncut.user.cloudtest D/ContentValues: position=0
05-24 01:05:29.688 21886-21886/com.ncut.user.cloudtest D/ContentValues: position=1
05-24 01:05:29.689 21886-21886/com.ncut.user.cloudtest D/ContentValues: position=2
05-24 01:05:29.690 21886-21886/com.ncut.user.cloudtest D/ContentValues: position=3
05-24 01:05:29.691 21886-21886/com.ncut.user.cloudtest D/ContentValues: position=4
05-24 01:05:29.692 21886-21886/com.ncut.user.cloudtest D/ContentValues: position=5
05-24 01:05:29.696 21886-21886/com.ncut.user.cloudtest D/ContentValues: position=6
05-24 01:05:29.697 21886-21886/com.ncut.user.cloudtest D/ContentValues: position=7
05-24 01:05:29.698 21886-21886/com.ncut.user.cloudtest D/ContentValues: position=8
05-24 01:05:31.608 21886-21886/com.ncut.user.cloudtest D/ContentValues: position=8
05-24 01:05:36.790 21886-21886/com.ncut.user.cloudtest D/ContentValues: position=0