android - 如何从 SD 卡中检索文本并相应地设置 TextView ?

标签 android android-gridview

我正在使用 gridview 和布局 inflater 来显示带有文本的图标。

它应该是这样的:

enter image description here

我的文本文件包含以下数据:

Profile 0|Profile 1|Profile 2

我正在使用以下代码:

    public View getView(final int position, View convertView, ViewGroup parent) {
        View v;
        //String text;
        final ImageView picturesView;
        if (convertView == null) {
            LayoutInflater li = getLayoutInflater();
            v = li.inflate(R.layout.icon, null);
            File sdcard = Environment.getExternalStorageDirectory();
            File file = new File(sdcard,"string.txt");
            //StringBuilder stext = new StringBuilder();
            TextView tv = (TextView)v.findViewById(R.id.icon_text);
            try {
                BufferedReader br = new BufferedReader(new FileReader(file));
                String line;

                while ((line = br.readLine()) != null) {
                    String[] columns = line.split("\\|");
                    for (String name : columns){ 

                       tv.setText(name);
                    }

                    //stext.append(line);
                    //stext.append('\n');
                }
            }
            catch (IOException e) {
                //You'll need to add proper error handling here
            }

            
            tv.setTextSize(12);
            tv.setTextColor(Color.BLACK);              
            
        }
        else {
            v = convertView;
        }

}

我想用字符串值设置 TextView

TextView tv = (TextView)v.findViewById(R.id.icon_text);
tv.setText();

我试图通过 Toast.makeText(SDCardImagesActivity.this, ""+columns.length, Toast.LENGTH_LONG).show(); 检查列长度,但它显示 1(5 次)

我很困惑如何在 tv.setText 中使用列值以及列数组是否正确。

最佳答案

查看您引用的代码

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v;
        if(convertView==null){
            LayoutInflater li = getLayoutInflater();
            v = li.inflate(R.layout.icon, null);
            TextView tv = (TextView)v.findViewById(R.id.icon_text);
            tv.setText("Profile "+position);
            ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
            iv.setImageResource(R.drawable.icon);
        }
        else
        {
            v = convertView;
        }
        return v;
    }

代替这一行...

tv.setText("Profile "+position);

...使用以下...

tv.setText(columns[position]);

关于android - 如何从 SD 卡中检索文本并相应地设置 TextView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6174746/

相关文章:

android - 保持服务在后台运行,即使用户清除内存

android - 集成 Google Play Games + Google Maps 扩展 BaseGamesActivity 和 FragmentActivity

android - 无限项的无限滚动

Android GridView适应屏幕高度

Android使用 Intent 从 Activity 中打开 fragment

java - 找到多个文件,操作系统独立路径为 'com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_WF'

android - 在共享元素转换中淡出英雄 View 的 child

android - 使用按钮更改 int 值

android - 错误 : Error parsing xml: Unbound Prefix when using StaggeredGridView

android - 强制 gridview 绘制所有图 block