ArrayList 适配器在列表视图中获取错误数据
Posted
技术标签:
【中文标题】ArrayList 适配器在列表视图中获取错误数据【英文标题】:ArrayList adapter getting wrong data in listview 【发布时间】:2020-05-12 00:25:38 【问题描述】:任何人都知道为什么我在使用 listview 填充适配器时在我的 AdapterClass 中得到错误的数据这是我正在使用的代码
public class SchoolAdapter extends ArrayAdapter<SchoolClass>
ArrayList<SchoolClass> schoolclass;
private SchoolAdapter schoolAdapter;
public SchoolAdapter(Activity activity1, ArrayList<SchoolClass> schoolClassList1)
super(activity1, R.layout.school_adapter, schoolClassList1);
this.activity = activity1;
this.schoolClassList = schoolClassList1;
@NonNull
@Override
public View getView (int position, View convertView, @NonNull ViewGroup parent)
LayoutInflater inflater = activity.getLayoutInflater();
@SuppressLint("ViewHolder", "InflateParams") View view = inflater.inflate(R.layout.school_adapter, null, false);
school_name = view.findViewById(R.id.school_name);
school_size = view.findViewById(R.id.school_size);
school_btn = view.findViewById(R.id.school_btn);
SchoolClass schoolClass = schoolclass.get(position);
school_name.setText(schoolClass.getSchoolName());
school_size.setText(schoolClass.getSchool_Size());
school_btn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Toast.makeText(context, "Grant"+schoolClass.getSchoolName(), Toast.LENGTH_SHORT).show();
);
return view;
注意:当我使用 listview.setOnItemClickListerner 时,我得到了正确的数据,但是当我使用任何单击适配器中第一个值的项目时,我得到的结果类似于第 5 个项目,当我单击时第二项同样的事情,当我单击第三项时,我得到的结果就像适配器中的第 8 或第 9 项一样,其他一切似乎都很好,我的代码没有显示任何错误,因为结果已显示,但它不是,正确的结果 注意 2:我使用 json 来检索列表,但只有当我单击适配器中的项目时,当我在 Toast 中得到错误结果时,结果才会正确显示
【问题讨论】:
这是什么schoolClass = schoolClass.get(position);
?你能添加更多关于你的适配器的细节吗?
【参考方案1】:
Toast.makeText(context, "Grant "+ schoolclass.get(position).getName, Toast.LENGTH_SHORT).show();
public class SchoolAdapter extends ArrayAdapter<SchoolClass>
ArrayList<SchoolClass> schoolclass;
Context context
public SchoolAdapter(Context context ,ArrayList<SchoolClass> schoolClassList)
super(context, R.layout.school_adapter, schoolClassList);
this.activity = activity1;
this.schoolClassList = schoolClassList1;
@NonNull
@Override
public View getView (int position, View convertView, @NonNull ViewGroup parent)
LayoutInflater inflater = activity.getLayoutInflater();
@SuppressLint("ViewHolder", "InflateParams") View view = inflater.inflate(R.layout.school_adapter, parent, false);
school_name = view.findViewById(R.id.school_name);
school_size = view.findViewById(R.id.school_size);
school_btn = view.findViewById(R.id.school_btn);
schoolClass = schoolclass.get(position);
school_name.setText(schoolClass.getSchoolName());
school_size.setText(schoolClass.getSchool_Size());
school_btn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Toast.makeText(context, "Grant"+ schoolclass.get(position).getSchoolName(), Toast.LENGTH_SHORT).show();
);
return view;
【讨论】:
【参考方案2】:您的实例(学校班级)名称与列表名称几乎相同。
改变
"schoolClass = schoolClass.get(position);" to
"schoolClass = schoolclass.get(position);"
schoolclass
- 是列表;
schoolClass
- 是你的项目,我认为你混淆了上/下
【讨论】:
我已经更正了这一点并添加了我的代码的更多细节以上是关于ArrayList 适配器在列表视图中获取错误数据的主要内容,如果未能解决你的问题,请参考以下文章
两个 ArrayList 一个 RecyclerView 适配器
如何使用自定义列表视图在edittext过滤器中获取空间?可以使用简单的适配器吗?