如何从SD卡检索文本并相应地设置文本视图?

| 我正在使用gridview和布局充气器显示带有文本的图标。 我指的是以下网址提供的代码:http://mytelcoit.com/2010/02/programming-android-create-icon-with-text-using-gridview-and-layout-inflater/,但图片和文本存储在sdcard中。它看起来应该像这样: 我的文本文件包含以下数据:
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 tv = (TextView)v.findViewById(R.id.icon_text);
tv.setText();
我试图通过
Toast.makeText(SDCardImagesActivity.this, \"\"+columns.length, Toast.LENGTH_LONG).show();
检查列的长度,但显示为1(5次) 我很困惑如何在tv.setText中使用column值,以及column数组是否正确。 请帮我 谢谢 潘卡伊     
已邀请:
        查看您引用的代码
    @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]);
    
        您将每个文件行的内容存储在columns-array中。 您应该存储此数据以备后用,或在循环中立即设置文本。     
        请尝试以下代码设置TextView
File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard,\"string.txt\");



    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;

        while ((line = br.readLine()) != null) {


            String delimiter = \"\\\\|\";

            String[] columns = line.split(delimiter);
            for (String name : columns){ 

                tv.setText(name);
            }

        }
    }
    catch (Exception e) {
        //You\'ll need to add proper error handling here
        e.printStackTrace();
    }
谢谢 迪帕克     
        您可以创建上面的视图而无需创建TextView和ImageVIew。只需创建一个按钮并使用drawableTop。如果要根据图像设置配置文件名称。如果button1创建条件语句,则像这样创建
button.settext(\"Profile1\")
<Button 
  android:id=\"@+id/openpdfbutton\"
  android:layout_width=\"100dip\"
  android:layout_height=\"60dip\"
  android:text=\"Click\"

  android:tag=\"@drawable/cancelfocus\"
  android:drawableTop=\"@drawable/cancelfocus\"
  />
谢谢 迪帕克     

要回复问题请先登录注册