Reputation: 1342
I am using CustomGridAdapter which extends BAseAdapter to display the values of array in a listview. but in the getView of the CustomGridAdapter the position of the array values is not in a flow.
public class CustomGridAdapter extends BaseAdapter {
private Context context;
private final String[] gridValues;
private final String[] gridImage;
//Constructor to initialize values
public CustomGridAdapter(Context context, String[] gridValues, String[] gridImage) {
this.context = context;
this.gridValues = gridValues;
this.gridImage=gridImage;
}
@Override
public int getCount() {
// Number of times getView method call depends upon gridValues.length
return gridValues.length;
}
@Override
public Object getItem(int position) {
return gridValues[position];
}
@Override
public long getItemId(int position) {
return 0;
}
// Number of times getView method call depends upon gridValues.length
public View getView(int position, View convertView, ViewGroup parent) {
//LayoutInflator to call external grid_item.xml file
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from grid_item.xml
gridView = inflater.inflate(R.layout.grid_item, null);
// set value into textview
TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label);
textView.setText(gridValues[position]);
Log.i(new Integer(position).toString(),gridValues[position] );
ImageView grp_icon=(ImageView)gridView.findViewById(R.id.grid_item_image);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
String path="/sdcard/Letsmeet/letsmeet_media/group_images/"+gridImage[position];
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
grp_icon.setImageBitmap(bitmap);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
String mobile = gridValues[position];
} else {
gridView = (View) convertView;
}
return gridView;
}
}
In the getView method of the customGridAdapter the postion is in order of 0,1,2,0,4 Why I am getting 0 after 2 instead of 3?
Upvotes: 2
Views: 2729
Reputation: 6792
@Override
public long getItemId(int position) {
return position; //return position here
}
Make your getView as,
public View getView(int position, View convertView, ViewGroup parent) {
//LayoutInflator to call external grid_item.xml file
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from grid_item.xml
gridView = inflater.inflate(R.layout.grid_item, null);
// set value into textview
TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label);
textView.setText(gridValues[position]);
Log.i(new Integer(position).toString(),gridValues[position] );
ImageView grp_icon=(ImageView)gridView.findViewById(R.id.grid_item_image);
} else {
gridView = (View) convertView;
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
String path="/sdcard/Letsmeet/letsmeet_media/group_images/"+gridImage[position];
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
grp_icon.setImageBitmap(bitmap);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
String mobile = gridValues[position];
return gridView;
}
Your converView is null only for the number of Views shown initially on the screen. Once you scroll them converView != null
and they are recycled. So move your code outside it.
Upvotes: 3
Reputation: 2506
This is related to how ListView and classes inherited from ListView display results based on scroll position and other factors for GC/performance reasons. It is specified in the documentation that there is no guarantee the order or positions. To counter this it is recommended that you use the ID rather than position of items for referencing.
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
For more information see: https://www.youtube.com/watch?v=wDBM6wVEO70
Upvotes: 0
Reputation: 1442
You just use like this and you will take it as a example and edit your custom adapter like this and you can get the Out put I think you have done the mistake in convert view
You just delete the condition of convetview ... and use like this
public class CustomAdapter extends ArrayAdapter<Sample> {
public ArrayList<Sample> mlist;
public Context context;
public LayoutInflater inflater;
public CustomAdapter(Context context, int resource, ArrayList<Sample> mlist) {
super(context, resource);
this.mlist = mlist;
this.context = context;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getPosition(Sample item) {
return super.getPosition(item);
}
@Override
public Sample getItem(int position) {
return mlist.get(position);
}
@Override
public int getCount() {
return mlist.size();
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.listitem, null);
TextView text1 = (TextView) convertView.findViewById(R.id.item1);
TextView text2 = (TextView) convertView.findViewById(R.id.item2);
text1.setText(mlist.get(position).getListitem1());
text2.setText(mlist.get(position).getListitem2());
text2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// you just put your Logic here And use this custom adapter to
// load your Data By using this particular custom adapter to
// your listview
}
});
return convertView;
}
}
Upvotes: 0