我正在使用擴展BAseAdapter以在列表視圖中顯示數組值的CustomGridAdapter。但是在CustomGridAdapter的getView中,數組值的位置不在流中。在getView方法中返回不正確的位置
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;
}
}
在customGridAdapter在現在的位置的getView方法是在0,1,2,0,4順序爲什麼我2而不是3後得到0?
在移動textView.setText(gridValues [position])後工作;對外線 – DharanBro
是的,我錯過了移動。 :)我們應該始終將findViewById()代碼保存在if(convertView == null){}條件中。所有與應用setText()等值相關的代碼應該超出此條件。您還應該考慮ViewHolder模式。例如 - http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html –