Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Simple GridView that displays text in the grids

Tags:

java

android

I'm following the example on the android tutorial about the GridView, but instead of showing image, i want to just simple show some text using a TextView. it turns out seems to be harder than i thought. it might seems like this is totally unnecessary and it doesn't have a valid use case, but i'm trying this out to just get myself familiar with the sdk.

so my code is pretty much the same as the GridView example in http://developer.android.com/guide/tutorials/views/hello-gridview.html, but instead of using a ImageAdapter, i created a dummy adapter like following:

public class MyAdapter extends BaseAdapter {

    private Context context;
    private String[] texts = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "eee", "hhh", "iii"};

    public MyAdapter(Context context) {
        this.context = context;
    }

    public int getCount() {
        return 9;
    }

    public Object getItem(int position) {
        return null;
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        TextView tv;
        if (convertView == null) {
            tv = new TextView(context);
            tv.setLayoutParams(new GridView.LayoutParams(85, 85));
        }
        else {
            tv = (TextView) convertView;
        }

            tv.setText(texts[position]);
        return tv;
    }
}

it all seems valid to me, but running this gives me nothing on the screen. and there's no error message. there are some selectable/clickable (invisible) blocks if i click them, but the text is obvious not shown. i wonder is my layout doesn't have the android:text causing this problem? or anything else?

any feedback will be appreciated and thanks for your help!

like image 829
fei Avatar asked Jun 11 '09 17:06

fei


1 Answers

I am not sure what could be causing your problem. I followed the step by step instructions on the page that you linked to to set up "Hello, GridView", and used your code and was able to see the text.

The only things I changed was rather than creating a class for ImageAdapter I used your MyAdapter. In the activity HelloGridView.java onCreate I used "MyAdapter" rather than "ImageAdapter". I didn't change the layout at all.

alt text

Here is a Screenshot of what I get when running your code.

like image 84
snctln Avatar answered Oct 02 '22 23:10

snctln