0

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;
    }
}

}

Ravi
  • 34,851
  • 21
  • 122
  • 183
A Osman
  • 171
  • 2
  • 8
  • I can understand what the idea is there but I have done it in the past according to how Google did it with SimpleAdapter where getView() returns the args and mResource then create the list with the view(s) using createViewFromResource() where it gets returned and displayed. That I know works. The way your doing it, idk. Maybe all roads lead to Rome. – CmosBattery Jan 12 '16 at 04:35
  • I believe the difference between Google's code and my code is the fact that you cannot add pictures to the resource folder at runtime. Since my program gets the pictures using the camera *i.e. at runtime) and must save them for future, I believe that method is not an option for me, or doing it that way would be hacky. I hope I'm not wrong though. – A Osman Jan 12 '16 at 04:49
  • Its just a method with a name. variables, camera pictures, cursors will all work there. As both have view args though the getView() method should theoretically work. – CmosBattery Jan 12 '16 at 04:54
  • You are returning imageview in getView(). Thats wrong. You need to assign convertview and return convertview. You havent understood the concept of this. Please do not copy paste. – Keshav Jan 12 '16 at 05:00

2 Answers2

0

Code seems to correct, except one missing line which is very important. In your getView() function, in else condition you are assigning convertView to imageView instance but actually convertView is not inflated or not initialized.

You need to change your code as below:

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);
            convertView = imageView;
        }
        else{
            imageView = (ImageView) convertView;
        }

I hope by changing your code, it should work.

Geeky Singh
  • 1,003
  • 9
  • 20
0

you did not set convertView so it gets null. first you have to set convertView. You should try this..

    public View getView(int position, View convertView, ViewGroup parent){
      ViewHolder viewHolder;


            if(convertView == null){
LayoutInflater inflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.your_view, null);    // you need one xml file which has parent layout to hold imageview
viewHolder = new ViewHolder;
viewHolder.linearLayout = converView.findViewById(R.id.linearLayout);   //your parent layout id which is defined in xml file
                viewHolder.imageView = new ImageView(mContext);
                viewHolder.imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
                viewHolder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                viewHolder.imageView.setPadding(8,8,8,8);
    viewHolder.linearLayout.addView(viewHolder.imageView);   // add imageview on layout
convertView.setTag(viewHolder);
            }
            else{
                viewHolder = (ViewHolder) convertView.getTag();
            }

            FileInputStream fis = null;
            try {
                fis = new FileInputStream(mPhotoPaths.get(position));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            Bitmap bm = BitmapFactory.decodeStream(fis);
            viewHolder.imageView.setImageBitmap(bm);

            return convertView;
        }

private static class ViewHolder
{
     LinearLayout linearLayout;
    ImageView imageView;
}

this is xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"'
    android:id="@+id/linearLayout""
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

</LinearLayout>

hope it helps you

Mangesh Sambare
  • 594
  • 3
  • 23
  • Thanks for you reply. I'm trying to implement this code now (working on some errors it gives/xml). However, I wanted to just add that I set a breakpoint at the first line in getView, which is never triggered. Shouldn't at least the first line of getView() be called for this solution to work? – A Osman Jan 12 '16 at 05:02
  • you have to define one `xml` file see edited code. and comments in `getView()` methods.. you have to read this [link](http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works). hope this will clear concepts about `convertview` – Mangesh Sambare Jan 12 '16 at 05:23