开发者

Duplicate image in case of gridview while scrolling the grid?

I am using the grid view with layout inflator example.

I am selecting the image from the sdcard and the name of the image and text is retrieved from string.txt file.

When i test on the emulator(wide screen) it works fine but when i check it on the emulator with the screen equivalent to real device (Resolution: 240 x 320 QVGA) it comes with the scrolling when i go to the last row the first image repeats on the last row as well.

My string file contains

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

My code is as follows:

    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;    开发者_如何学JAVA                   
                        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;
    }

I tried to manually check the value of name ie.(01.png upto 12.png) by Toast.makeText method it runs fine upto 9 elements (elements available without scroll) it displays 01.png upto 09.png but then it displays 01.png again instead of 10.png then displays 11.png then 12.png.

I am unable to understand why 01.png appears twice? I am completely stuck at this.


close the if statement

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;


You could try this:

 public View getView(int position, View convertView, ViewGroup parent) {
         View MyView = convertView;
         if (convertView == null) {  // if it's not recycled, initialize some attributes
             //Inflate the layout
             LayoutInflater li = getLayoutInflater();
             MyView = li.inflate(R.layout.grid_item, null);
        } else {
             MyView = convertView;
            }
             // Add The Image!!!           
             ImageView iv = (ImageView)MyView.findViewById(R.id.grid_item_image);
             iv.setImageResource(mThumbIds[position]);

             // Add The Text!!!
             TextView tv = (TextView)MyView.findViewById(R.id.grid_item_text);
             tv.setText(names[position] );
             return MyView;
     }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜