如何在android中使用gridview显示动态生成的字符串?

时间:2012-12-29 09:01:45

标签: android string gridview dynamic

我有一个数字数组,我想用最近添加的数据显示它们,格式如下:冒号后面的数组索引,数字和取决于它的数字,也是一个惊叹号这样:

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

我将如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

它就像一个ListView ......

SDK中的Hello Gridview示例是您所需要的: http://developer.android.com/guide/topics/ui/layout/gridview.html

只需用TextAdapter替换ImageAdapter即可!

公共类TextAdapter扩展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!",...
};

}