2

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

我指的是以下代码: 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 中使用列值以及列数组是否正确。

请帮助我

谢谢

潘卡伊

4

4 回答 4

1

查看您引用的代码

    @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]);
于 2011-05-30T10:16:18.113 回答
0

您将每个文件行的内容存储在列数组中。您应该存储这些数据以备后用,或者在循环中立即设置文本。

于 2011-05-30T10:03:48.450 回答
0

请尝试以下代码设置 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();
    }

谢谢迪帕克

于 2011-05-30T10:16:25.860 回答
0

您可以在不创建 TextView 和 ImageVIew 的情况下创建上述视图。只需创建一个按钮并使用 drawableTop。如果您想根据您的图像设置配置文件名称。button.settext("Profile1")如果 button1 然后像这样创建条件语句。

<Button 
  android:id="@+id/openpdfbutton"
  android:layout_width="100dip"
  android:layout_height="60dip"
  android:text="Click"

  android:tag="@drawable/cancelfocus"
  android:drawableTop="@drawable/cancelfocus"
  />

谢谢迪帕克

于 2011-05-30T10:24:03.393 回答