Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中使用gridview显示动态生成的字符串?_Android_String_Gridview_Dynamic - Fatal编程技术网

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

如何在android中使用gridview显示动态生成的字符串?,android,string,gridview,dynamic,Android,String,Gridview,Dynamic,我有一个数字数组,我想用以下格式显示最近添加的数字:数组索引以冒号折叠,数字和数字取决于它是哪个数字,还有一个感叹号,如下所示: 10:35,09:41!,08:17, 07:5!... 我将如何实现这一目标 它就像一个列表视图 SDK中的Hello Gridview示例就是您所需要的: 只需将ImageAdapter替换为TextAdapter即可 公共类TextAdapter扩展了BaseAdapter{ 私有上下文 public TextAdapter(Context c) {

我有一个数字数组,我想用以下格式显示最近添加的数字:数组索引以冒号折叠,数字和数字取决于它是哪个数字,还有一个感叹号,如下所示:

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


我将如何实现这一目标

它就像一个列表视图

SDK中的Hello Gridview示例就是您所需要的:

只需将ImageAdapter替换为TextAdapter即可

公共类TextAdapter扩展了BaseAdapter{ 私有上下文

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

}

可能是重复的谢谢我已经开始工作了。但是,生成字符串数据的代码位于主活动中,而gridview位于由第一个活动启动的第二个活动中。因此,我在MainActivity中实例化了一个静态TextAdapter,以便可以从这两个活动中访问它。然后,我使用在适配器中创建的public addString()和remString()方法更新适配器的私有字符串数组。静态对象是实现这一点的最佳方式吗?在第一个或第二个活动中使用静态对象是最佳方式吗?