2011-08-07 33 views
0

我有一個帶有9個項目的GridView。我試圖做的是當我點擊一個項目時,它的圖標發生了變化。我在做它的方式返回單擊GridView中的項目時更改圖像

java.lang.ClassCastException: android.widget.LinearLayout 

我不得不類:

MainLayout.java

public class MainLayout extends Activity implements OnItemClickListener 
{ 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     requestWindowFeature(Window.FEATURE_NO_TITLE); 

     setContentView(R.layout.mainlayout); 

     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);  

     final ImageButton btn=(ImageButton)findViewById(R.id.logoimagebutton); 
     btn.setBackgroundColor(Color.TRANSPARENT); 

     btn.setOnTouchListener(new OnTouchListener() { 

       public boolean onTouch(View v, MotionEvent event) { 
        if (event.getAction() == MotionEvent.ACTION_DOWN) { 
         btn.setImageResource(R.drawable.logoselected); 
         Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show(); 
         //return true; 
        }else if (event.getAction() == MotionEvent.ACTION_UP) { 
         btn.setImageResource(R.drawable.logo); 
        } 

        return false; 
       } 
      }); 

     final GridView gridview = (GridView) findViewById(R.id.gridview); 
     gridview.setAdapter(new ImageAdapter(this));    

     gridview.setOnItemClickListener(new OnItemClickListener() 
     { 
      public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
      {     
       ImageView imageView = (ImageView) v; 
       imageView.setImageResource(ImageAdapter.mThumbSelected[position]); 
      } 
     }); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
    { 
     // TODO Auto-generated method stub 

    } 

} 

ImageAdapter.java

public class ImageAdapter extends BaseAdapter 
{ 

    private Context mContext; 

    public static Integer[] mThumbIds = 
    { 
      //a collection of images 
    }; 

    public static Integer[] mThumbSelected = 
    { 
      //a collection of images 
    }; 

    private String[] mLabelsIds = {//a collection of strings}; 

    public ImageAdapter(Context c) 
    { 
     // TODO Auto-generated constructor stub 
     mContext = c; 
    } 

    @Override 
    public int getCount() 
    { 
     // TODO Auto-generated method stub 
     return mThumbIds.length; 
    } 

    @Override 
    public Object getItem(int arg0) 
    { 
     // TODO Auto-generated method stub 
     return mThumbIds[arg0]; 
    } 

    @Override 
    public long getItemId(int arg0) 
    { 
     // TODO Auto-generated method stub 
     return arg0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    {  
     View grid; 

     if (convertView == null) 
     { 
      grid = new View(mContext); 
      LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      grid = inflater.inflate(R.layout.menugrid, parent, false); 
     } else 
     { 
      grid = (View) convertView; 
     } 

     ImageView imageView = (ImageView) grid.findViewById(R.id.imageicon); 
     TextView textView = (TextView) grid.findViewById(R.id.imagelabel); 
     imageView.setImageResource(mThumbIds[position]); 
     textView.setText(mLabelsIds[position]); 

     return grid; 
    } 
} 

這裏我的兩個xml文件:

menugrid.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="vertical"> 

    <ImageView 
     android:id="@+id/imageicon" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     /> 
    <TextView 
     android:id="@+id/imagelabel" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"  
     android:textColor="#000000" 
     android:textSize="16px" 
     android:gravity="center_horizontal"/> 
</LinearLayout> 

mainlayout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainlayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="@drawable/background"> 

    <LinearLayout 
     android:id="@+id/holdlogo" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"  
     android:orientation="horizontal"      
     android:background="@drawable/title_background"> 

     <ImageButton 
      android:id="@+id/logoimagebutton" 
      android:scaleType="center" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"  
      android:layout_gravity="center_vertical"  
      android:src="@drawable/logo" 
      > 
     </ImageButton> 

    </LinearLayout> 


    <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:numColumns="auto_fit" 
    android:layout_marginTop="10px" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:columnWidth="90dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" 
/> 
</LinearLayout> 

誰能告訴我什麼是錯在我的代碼?或者在按下時如何更改項目的圖像?我認爲onTouchListener更高效,因爲它有事件發生,所以當我顯示event.getAction() == MotionEvent.ACTION_DOWN圖像和event.getAction() == MotionEvent.ACTION_UP時,我顯示原始圖標。

在此先感謝。

回答

4

我猜你reveice這裏的ClassCastException異常:

public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
     {     
      ImageView imageView = (ImageView) v; 
      imageView.setImageResource(ImageAdapter.mThumbSelected[position]); 
     } 

發送的觀點實際上是其中的ImageView駐留在您需要使用int position拿到項目,然後操縱它到你需要的LinearLayout,或者在LinearLayout中獲取ImageView,這是您的Grid Item Layout。

4

如果您使用ViewHolder類,則不僅可以優化網格視圖,還可以從OnItemClickListener更容易地訪問imageview。你需要在你ImageAdapter是這樣的:

class ViewHolder { 
    ImageView img; 
    TextView lbl; 
} 

然後修改getView是這樣的:

public View getView(int position, View convertView, ViewGroup parent) 
{  

    ViewHolder holder; 
    if (convertView == null) 
    { 

     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.menugrid, parent, false); 
     holder = new ViewHolder(); 
     holder.img = (ImageView) convertView.findViewById(R.id.imageicon); 
     holder.lbl = (TextView) convertView.findViewById(R.id.imagelaberl); 
     convertView.setTag(holder); 
    } else 
    { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    ImageView imageView = holder.img; 
    TextView textView = holder.lbl; 
    imageView.setImageResource(mThumbIds[position]); 
    textView.setText(mLabelsIds[position]); 

    return convertView; 
} 

現在您的OnItemClickListener你可以訪問你這樣的圖像視圖:

public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
    {     
     ImageView imageView = ((ViewHolder) v.getTag()).img; 
     imageView.setImageResource(ImageAdapter.mThumbSelected[position]); 
    } 

這樣可以防止您經常撥打findViewById,這樣可以提高性能,因爲這是一項昂貴的操作。

+0

ClassCastException發生此問題的任何解決方案? – ABI

+0

你得到ClassCastException的哪一行?我從來沒有遇到過這個問題。 – Sleepybear

+0

需要將其轉換爲ImageView。 'ImageView imageView =(ImageView)((ViewHolder)v.getTag())。img;' –

相關問題