我有一个带有自定义适配器的GridView,如下所示。我还设置了onClick侦听器,其中适配器设置为gridView。 onClick函数显示正确的位置和数据,但我想在GridView中显示的数据不正确。我在数组列表中有10个项目。当我向下滚动时,倒数第二个网格项显示第一个网格项的值。我在Google上搜索了很多,但是找不到解决方案。需要帮忙。谢谢。
public class CustomGridAdapter extends BaseAdapter {
List<SupplicationMain> supplications;
Context context;
public CustomGridAdapter(List<SupplicationMain> supplications, Context c) {
this.supplications = supplications;
this.context = c;
}
@Override
public int getCount() {
return supplications.size();
}
@Override
public Object getItem(int i) {
return supplications.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.single_item, parent,false);
TextView textView = (TextView) convertView.findViewById(R.id.grid_text);
TextView textView2 = (TextView) convertView.findViewById(R.id.grid_text2);
textView.setText(supplications.get(position).getName());
textView2.setText(""+supplications.get(position).getSubCategories().size()+" Groups");
}
convertView.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, 300));
return convertView;
}
}
设置适配器:-
List<SupplicationMain> supplications = DataGenerator.getSupplications();
CustomGridAdapter adapter = new CustomGridAdapter(supplications,getContext());
gv.setAdapter(adapter);
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getActivity(), "You Clicked "+supplications.get(i).getName(), Toast.LENGTH_SHORT).show();
}
});