ankush981
ankush981

Reputation: 5417

Android GridView is empty

EDIT: This problem has been resolved, so the code in my original post does not apply anymore. Thanks to everyone who contributed.

First off, I have checked other similar questions but they either don't match mine or use the BaseAdapter. What I'm trying to do is display a text-image combination as part of a GridView. The actual app is larger, but my problem is that the GridView is blank in the very first activity!

I'd appreciate if someone can take a quick look at it and tell me what's wrong.

Code for layout file used for GridView elements:

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

    <ImageView
        android:id="@+id/iv_grid"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/tv_grid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

Layout file of first Activity:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".CategoriesActivity" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="54dp"
        android:text="Select a category to learn more" />

    <TextView
        android:id="@+id/tv_grid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="26dp"
        android:text="Lessons from History"
        android:textSize="18sp" />

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="25dp"
        android:numColumns="3" >
    </GridView>

</RelativeLayout>

And finally, the Java file:

package com.example.historicpersonalities;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class CategoriesActivity extends Activity {

    GridView gv_categories;
    public int selected; //which category is selected [0-4]?
    String[] data; //holds the data for GridView

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_categories);

        populate();
        selected = -1;

        gv_categories = (GridView) findViewById(R.id.gridView1);
        gv_categories.setAdapter(new MyAdapter(this, 
                android.R.layout.simple_expandable_list_item_1));

        gv_categories.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int index,
                    long arg3) {

            }
        });
    }

    class MyAdapter extends ArrayAdapter<String> {

        public MyAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View v = inflater.inflate(R.layout.layout_grid_scheme, null);

            TextView item = (TextView) findViewById(R.id.tv_grid);
            item.setText(data[position]);

            ImageView img = (ImageView) findViewById(R.id.iv_grid);
            img.setImageResource(R.drawable.dummy);

            Log.d("ankush", ""+position);

            return v;
        }

    }

    private void populate() {
        data = new String[5];
        data[0] = "Writers";
        data[1] = "Painters";
        data[2] = "Conquerors";
        data[3] = "Chemists";
        data[4] = "Actors";
    }
}

Upvotes: 2

Views: 4092

Answers (3)

Opiatefuchs
Opiatefuchs

Reputation: 9870

Don´t know if this causes the problem, but You have to set Your TextView and ImageView in Your adapter like this:

    TextView item = (TextView) v.findViewById(R.id.tv_grid);
        item.setText(data[position]);

        ImageView img = (ImageView) v.findViewById(R.id.iv_grid);
        img.setImageResource(R.drawable.dummy);

EDIT

First of all, the best way is to combine all three solutions. BUT, I have seen another issue in Your xml layout: You put the same id to the TextView inside Your GridView Element xml and Your Activity layout xml. Both TextViews got id="@+id/tv_grid" . This could never work, change the id´s to different ones

Upvotes: 1

hcelaloner
hcelaloner

Reputation: 306

If I'm not wrong, it seems that in your constructor you don't pass any data to the adapter.

    public MyAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

In this code snippet, your are using ArrayAdapter (Context context, int textViewResourceId) which initialize an empty arraylist as the objects to represent in the GridView.

public ArrayAdapter(Context context, int textViewResourceId) {
    init(context, textViewResourceId, 0, new ArrayList<T>());
}

Therefore your GridView should be empty. In addition to that, it seem you didn't properly implement the getView.(findViewById() parts should be handled as shown in the Opiatefuchs's answer)

If you want to extend ArrayAdapter, you should be calling a constructor which takes an array of objects as a parameter. (Of course there are many ways to make a custom adapter you can extend BaseAdapter or while extending ArrayAdapter you can override some other methods)

This way it should work:

class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context context, int textViewResourceId, String[] objects) {
        super(context, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            view = inflater.inflate(R.layout.layout_grid_scheme, null);
        } else {
            view = convertView;
        }

        TextView item = (TextView) view.findViewById(R.id.tv_grid);
        item.setText(getItem(position));

        ImageView img = (ImageView) view.findViewById(R.id.iv_grid);
        img.setImageResource(R.drawable.dummy);
    }

}

Upvotes: 1

Alexander Mikhaylov
Alexander Mikhaylov

Reputation: 1800

I think the problem is the height of your gridView. GridView is a scrollable container and can contain any amount of content in any height. Try to set background color to your gridView and you will see, that there is no gridview on the screnn. It's because you use wrap_content for gridview, but it's incorrect.

Upvotes: 2

Related Questions