0

I'm testing a simple grid adapter with a custom object, the app runs on the device without a problem but stays blank instead of inflating the activity with the specified grid items. This is my code.

Main Activity

package com.example.nobodyme.errorrepository;

  import android.app.Activity;
  import android.os.Bundle;
  import android.support.v7.app.ActionBarActivity;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.GridView;
  import android.widget.TextView;
  import android.widget.Toast;
  import android.view.View;
  import android.widget.AdapterView.OnItemClickListener;

  import java.util.ArrayList;

  public class MainActivity extends ActionBarActivity {

  GridView gridView;


  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  ArrayList<GridItems> gridItems = new ArrayList<>();
  gridItems.add(new GridItems("Android", 2));
  gridItems.add(new GridItems("Java", 3));

  gridView = (GridView) findViewById(R.id.gridView1);

  gridView.setAdapter(new GridViewAdapter(this, gridItems));



  }

  }

GridViewAdapter

    package com.example.nobodyme.errorrepository;

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.TextView;

    import java.util.List;



    public class GridViewAdapter extends ArrayAdapter<GridItems> {

        private Context context;
        int b;

        public GridViewAdapter(Context context, List<GridItems> gridItems) {
            super(context, 0, gridItems);
            b=gridItems.size();
        }

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

            View gridView = convertView;

            if(gridView == null)
            {

                gridView = LayoutInflater.from(getContext()).inflate(
                                           R.layout.grid_view, parent, false);

            }

            GridItems currentgriditem = getItem(position);

            TextView mMaintextView = (TextView) gridView.findViewById(R.id.grid_item_main);
            mMaintextView.setText(currentgriditem.getTitle());


            TextView mUnansweredtextView = (TextView) gridView.findViewById(R.id.grid_item_unanswered);
            mUnansweredtextView.setText(currentgriditem.getUnansweredNo());


            return gridView;

        }

        @Override
        public int getCount() {
            return b;
        }


        @Override
        public long getItemId(int position) {
            return 0;
        }

    }

GridItems

package com.example.nobodyme.errorrepository;

/**
 * Created by nobodyme on 15/1/17.
 */

public class GridItems {

    /** Grid title*/
    private String mTitle;

    /** NO of unanswered items in the gird **/
    private Integer mUnansweredNo;

    public GridItems(String Title, Integer UnansweredNo) {
        mTitle = Title;
        mUnansweredNo = UnansweredNo;
    }



    public String getTitle() {
        return mTitle;
    }


    public Integer getUnansweredNo() {
        return mUnansweredNo;
    }


}

I have edited the code as per the comments and the app still crashes.

Nobody
  • 397
  • 1
  • 5
  • 25

4 Answers4

3

You're overriding getCount and returning zero in the adapter. Don't override this, or return the correct number of items.

brindy
  • 4,585
  • 2
  • 24
  • 27
  • Can you telmme how to do that? I did as per the other answer and it doesn't work and not overriding crashes the app. – Nobody Jan 16 '17 at 14:28
2

Please check below code :

@Override
public int getCount() {
    return gridItems.size();
}

Instead of 0 returning, You should return your list size.

samsad
  • 1,241
  • 1
  • 10
  • 15
1

You need define gridItems List on your arrayAdapter and set it on the constructor and override getCount to return the gridItems (adapterGridItems) size

    private List<GridItems> adapterGridItems;

    public GridViewAdapter(Context context, List<GridItems> gridItems) {
            super(context, 0, gridItems);
            //set adapterGridItems
            this.adapterGridItems=gridItems;
     }
     @Override
     public int getCount() {
         return adapterGridItems.size();
     }

please, check

mMaintextView.setText(currentgriditem.getUnansweredNo());

you are passing a integer so mMainTextView.setText will find a resource with currentgriditem.getUnansweredNo() id, i think you are trying to set currentgriditem.getUnansweredNo() as string, so this will work

mMaintextView.setText(currentgriditem.getUnansweredNo()+"");

are you sure that is mMaintextView.setText(currentgriditem.getUnansweredNo()+"");

and not

mUnansweredtextView.setText(currentgriditem.getUnansweredNo()+""); 

?

Tito
  • 138
  • 1
  • 7
  • Yea I have made those changes actually idk why you only able to see the unchanged version of the question – Nobody Jan 16 '17 at 16:05
  • Actually I made all the changes except the `+""` for string conversion. And that did the trick. Thanks – Nobody Jan 16 '17 at 16:59
0

In order to tell the adapter how many GridItems to show we need to override the getCount method as shown above by @samsad.

The reason the compiler complains that it can't recognize symbol griditems is because you haven't created a field to store a reference to the GridItems in your adapter.

Try creating a field for the ArrayList of GridItems in your adapter to fix the issue.

FrankR
  • 196
  • 10
  • `public GridViewAdapter(Context context, List gridItems) { super(context, 0, gridItems); b= gridItems.size(); }` – Nobody Jan 16 '17 at 14:54
  • Yes i added a reference to girditems and returned b in getcount and still the app crashes – Nobody Jan 16 '17 at 14:55