2011-06-10 59 views
2

我正在使用網格視圖和佈局充氣器示例。在滾動網格的情況下gridview的情況下的重複圖像?

我從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; 
    } 

我試圖手動檢查名稱即價值(01.png高達12.png)土司。 .makeText方法運行良好,最多9個元素(可用的元素不滾動),它顯示01.png upto 09.png,但它再次顯示01.png而不是10.png然後顯示11.png然後12.png。

我無法理解爲什麼01.png會出現兩次?我完全停留在此。

回答

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; 
0

你可以試試這個:

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; 
    }