linitbuff
linitbuff

Reputation: 359

How do I display dynamically generated strings with a gridview in android?

I have an array of numbers, and I would like to display them from the most recently added, in the following format: The array index folowed by a colon, the number and depending on which number it is, also an exclaimation like this:

10:35, 09:41!, 08:17, 07:5!...

How would I go about achieving this?

Upvotes: 0

Views: 404

Answers (1)

Waza_Be
Waza_Be

Reputation: 39577

It's exactly like a ListView...

The Hello Gridview example in the SDK is what you need: http://developer.android.com/guide/topics/ui/layout/gridview.html

Just replace ImageAdapter by a TextAdapter and that's it!

public class TextAdapter extends BaseAdapter { private Context mContext;

public TextAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

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

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

// create a new TextView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    TextView textView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        textView = new TextView(mContext);
        textView.setLayoutParams(new GridView.LayoutParams(85, 85));
        textView.setPadding(8, 8, 8, 8);
    } else {
        textView = (TextView) convertView;
    }

    textView.setText(strings[position]);
    return textView;
}

// references to our texts
private String[] strings = {
        "10:35","09:41!","08:17","07:5!",...
};

}

Upvotes: 1

Related Questions