2

グリッドビューとレイアウトインフレータを使用して、テキスト付きのアイコンを表示しています。

私は次のコードを参照しています:http: //mytelcoit.com/2010/02/programming-android-create-icon-with-text-using-gridview-and-layout-inflater/テキストはSDカードに保存されます。次のようになります。

ここに画像の説明を入力してください

私のテキストファイルには次のデータが含まれています。

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でcolumns値を使用する方法と、columns配列が正しいかどうかがわかりません。

これで私を助けてください

ありがとう

パンカイ

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

次のコードを試して、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();
    }

ありがとうDeepak

于 2011-05-30T10:16:25.860 に答える
0

各ファイル行の内容をcolumns-arrayに格納します。このデータは後で使用するために保存するか、ループ内で一度にテキストを設定する必要があります。

于 2011-05-30T10:03:48.450 に答える
0

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"
  />

ありがとうDeepak

于 2011-05-30T10:24:03.393 に答える