2

レイアウト インフレーターの例でグリッド ビューを使用しています。

SD カードから画像を選択しており、画像とテキストの名前は string.txt ファイルから取得されます。

エミュレーター(ワイドスクリーン)でテストすると正常に動作しますが、実際のデバイス(解像度:240 x 320 QVGA)と同等の画面でエミュレーターでチェックすると、最初の最後の行に移動するとスクロールします画像は最後の行でも繰り返されます。

私の文字列ファイルには

01.png;http://abc.com/01.png;Text1|02.png;http://abc.com/02.png;Text2|03.png;http://abc.com/03.png;Text3|04.png;http://abc.com/04.png;Text4|05.png;http://abc.com/05png;Text5|06.png;http://abc.com/06.png;Text6|07.png;http://abc.com/07.png;Text7|08.png;http://abc.com/08.png;Text8|09.png;http://abc.com/09.png;Text9|10.png;http://abc.com/10.png;Text10|11.png;http://abc.com/11.png;Text11|12.png;http://abc.com/12.png;Text12

私のコードは次のとおりです。

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


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

                while ((line = br.readLine()) != null) {
                    columns = line.split("\\|");

                }
            }
            catch (IOException e) {
                //You'll need to add proper error handling here
            }
            newtext=columns[position].split("\\;");

            tv.setText(newtext[2]);
            tv.setTextSize(12);
            tv.setTextColor(Color.BLACK);

            mUrl = path+"s/"+newtext[0];

            name=newtext[0];

            url=newtext[1];

            final Bitmap mBitmap = BitmapFactory.decodeFile(mUrl);
            picturesView=(ImageView)v.findViewById(R.id.icon_image);
            picturesView.setImageBitmap(mBitmap);
            picturesView.setTag(R.id.icon_image,name);
            picturesView.setTag(R.id.icon_text,url);
            picturesView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    imgname=(String) v.getTag(R.id.icon_image);
                    imgurl=(String) v.getTag(R.id.icon_text);
                    // TODO Auto-generated method stub
                    String cimagename;
                    String oimagename;
                    String cUrl;
                    String oUrl;

                    if (selectedImage != null) {
                        oimagename=selectedImage;                       
                        oUrl = path+"s/"+oimagename;        
                        Bitmap oBitmap = BitmapFactory.decodeFile(oUrl);
                        selectedView.setImageBitmap(oBitmap);
                    }
                    selectedImage = imgname;
                    selectedView = (ImageView) v;
                    cimagename=imgname;
                    cUrl = path+"c/"+cimagename;  
                    Bitmap cBitmap = BitmapFactory.decodeFile(cUrl);
                    picturesView.setImageBitmap(cBitmap);

                }
            });


        }


        return v;
    }

Toast.makeText メソッドで名前 ie.(01.png から 12.png まで) の値を手動で確認しようとしました。最大 9 つの要素 (スクロールせずに利用可能な要素) まで正常に実行され、01.png から 09.png までが表示されますが、その後表示されます10.png の代わりに 01.png をもう一度表示すると、11.png が表示され、次に 12.png が表示されます。

01.png が 2 回表示される理由がわかりません。私はこれで完全に立ち往生しています。

4

2 に答える 2

0

ifステートメントを閉じます

if (convertView == null) {            
      LayoutInflater li = getLayoutInflater();        
      v = li.inflate(R.layout.icon, null);
 }  //<--add this and remove the closing brace you currently have at the above the return v;
于 2011-06-10T09:44:23.537 に答える