2013-06-27 69 views
0

我的代碼發生了一些奇怪的事情我不知道它爲什麼發生。在我的XML文件,我把一個複選框和圖像視圖,但在我的課somethings發生錯誤複選框和圖像視圖得到顛倒我投的imageview來自複選框的ID和複選框來自圖像視圖的id,當我施放imageview在我的xml中的imageview的ID相同與複選框它有錯誤。 這裏是我的xml創建自定義網格視圖有些不可思議

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:padding="5dp" > 

<CheckBox 
    android:id="@+id/cbo_CheckImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="5px" /> 

<ImageView 
    android:id="@+id/img_thumbImage" 
    android:layout_width="90dp" 
    android:layout_height="90dp" 
    android:layout_marginRight="10px" /> 

</LinearLayout> 

這裏是我的類

public class ImageAdapter extends BaseAdapter { 
    private LayoutInflater mInflater; 

    public ImageAdapter() { 
     mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public int getCount() { 
     return count; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 
      holder = new ViewHolder(); 
      convertView = mInflater.inflate(
        R.layout.gallery, null); 

      //LOOK AT HERE I DON'T KNOW WHY IT'S HAPPENING BUT IT WORKS. BUT WHEN I SWITCH THE IMAGEVIEW CAST TO THE ID OF MY IMAGEVIEW IN MY XML FILE IT GOT ERROR 
      holder.imageview = (ImageView) convertView.findViewById(R.id.cbo_CheckImage); 
      holder.checkbox = (CheckBox) convertView.findViewById(R.id.img_thumbImage); 


      convertView.setTag(holder); 
     } 
     else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     holder.checkbox.setId(position); 
     holder.imageview.setId(position); 
     holder.checkbox.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       CheckBox cb = (CheckBox) v; 
       int id = cb.getId(); 
       if (thumbnailsselection[id]){ 
        cb.setChecked(false); 
        thumbnailsselection[id] = false; 
       } else { 
        cb.setChecked(true); 
        thumbnailsselection[id] = true; 
       } 
      } 
     }); 
     holder.imageview.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       int id = v.getId(); 
       Intent intent = new Intent(); 
       intent.setAction(Intent.ACTION_VIEW); 
       intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*"); 
       startActivity(intent); 
      } 
     }); 
     holder.imageview.setImageBitmap(thumbnails[position]); 
     holder.checkbox.setChecked(thumbnailsselection[position]); 
     holder.id = position; 
     return convertView; 
    } 
} 
class ViewHolder { 
    CheckBox checkbox; 
    ImageView imageview; 

    int id; 
} 
+0

你試圖手動刪除生成的R文件,做一個項目,然後''clean' rebuild'? – chancea

回答

0

在你的代碼
holder.imageview =(ImageView的)convertView.findViewById(R.id.cbo_CheckImage); holder.checkbox =(CheckBox)convertView.findViewById(R.id.img_thumbImage);

這樣,請改變像

holder.imageview =(ImageView的)convertView.findViewById(R.id.img_thumbImage); holder.checkbox =(CheckBox)convertView.findViewById(R.id.cbo_CheckImage);

它的工作

這是有益的,你ggive我1+

+0

我已經這樣做了。這裏的東西是我的代碼沒有錯誤我想知道爲什麼我的代碼運行,即使我投我的複選框和複選框到我的圖像視圖..當我做你的建議代碼它給了我一個錯誤。 – NewDroidDev

+0

replace holder.checkbox.setId(position); int id = cb.getId(); holder.checkbox.setTag(position); int id = cb.getTag(); –