0

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?

1

1 Answer 1

1

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!",...
};

}

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I have it working. However, the code that generates the string data is in the main activity, and the gridview is in a second activity which is started by the first. So I have instantiated a static TextAdapter in the MainActivity so I can access it from both activities. I then update the adapter's private strings array with public addString() and remString() methods which I have created within it. Is a static object the best way to do this, and is it best to have in the first or second activity?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.