I have a GridView with custom adapter as shown below. I also have set onClick Listener where adapter is set to gridView. onClick function shows correct position and data but the data I wanted to display in GridView is incorrent. I have 10 items in array list. When I scroll down, the second last grid item shows the values of first grid item. I googled a lot but unable to find a solution. Need Help. Thanks.
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;
}
}
Setting the adapter :-
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();
}
});