I've tried copying other people's code word for word, but I can't seem to get my GridView
to display anything besides a white page. getCount()
gets called and returns the correct number, however, getView()
is not called. Any ideas would be greatly appreciated.
public class drinkSaleList extends Activity {
private ArrayList<String> drinkPhotoPaths = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink_sale_list);
GridView gridview = (GridView) findViewById(R.id.gridview);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
int drinkCount = sharedPref.getInt("Drink", 0);
if(drinkCount > 0)
{
for(int i = 1; i <= drinkCount; i++){
String path = getExternalFilesDir(null) + "/drinkObject" + i + ".ser";
drinkPhotoPaths.add(path);
}
}
gridview.setAdapter(new drinkSaleLayoutAdapter(this, drinkPhotoPaths));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
//Create an Intent to start the ImageViewActivity
Intent intent = new Intent(drinkSaleList.this,
SaleActivity.class);
Toast.makeText(drinkSaleList.this, "You clicked " + position + "and Id is " + id, Toast.LENGTH_SHORT).show();
// Add the ID of the thumbnail to display as an Intent Extra
// intent.putExtra(EXTRA_RES_ID, (int) id);
// Start the ImageViewActivity
// startActivity(intent);
}
});
}
public class drinkSaleLayoutAdapter extends BaseAdapter {
private Context mContext;
private List<String> mPhotoPaths;
public drinkSaleLayoutAdapter(Context c, List<String> photoPaths){
mContext = c;
mPhotoPaths = photoPaths;
}
public int getCount(){
int x = mPhotoPaths.size();
return mPhotoPaths.size();
}
public Object getItem(int position){
return mPhotoPaths.get(position);
}
public long getItemId(int position){
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
ImageView imageView;
if(convertView == null){
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8,8,8,8);
}
else{
imageView = (ImageView) convertView;
}
FileInputStream fis = null;
try {
fis = new FileInputStream(mPhotoPaths.get(position));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeStream(fis);
imageView.setImageBitmap(bm);
return imageView;
}
}
}