2012-01-31 67 views
0

我想通過擴展ListView在android中創建自定義listview。自定義列表視圖需要有多個列。我正在擴展BaseAdapter,並在getView()中擴充自定義行,但在擴展ListView的onMeasure()中出現錯誤。android擴展listview和baseadapter而不使用ListActivity

public class ImageListView extends ListView{ 
    private boolean isFirstMeasure; 
    private Paint paint=new Paint(); 
    private NoteActivity pActivity; 
    private Cursor cursor; 
    private int columnIndex; 
    private int parentWidth; 
    private int parentHeight; 
    private int itemWidth; 
    private boolean isImageSelected; 
    private int indexSelected; 
    private Bitmap selectedBitMap; 

    public Bitmap getSelectedBitMap() { 
     return selectedBitMap; 
    } 

    public void setSelectedBitMap(Bitmap selectedBitMap) { 
     this.selectedBitMap = selectedBitMap; 
    } 

    public int getIndexSelected(){ 
     return indexSelected; 
    } 

    public void setIndexSelected(int indexSelected) { 
     this.indexSelected = indexSelected; 
    } 

    static class ViewHolder 
    { 
     ImageView iView; 
     CheckBox cbListCheck ; 
     ImageDraggerView dragView; 
    } 

    private class ImageAdapter extends BaseAdapter{ 
     private Context mContext; 

     public ImageAdapter(Context localContext) { 
      super(); 
      mContext = localContext; 

     } 

     public int getCount() { 
      return cursor.getCount(); 
     } 
     public Object getItem(int position) { 
      return position; 
     } 
     public long getItemId(int position) { 
      return position; 
     } 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      ViewHolder holder = new ViewHolder(); 
      if (convertView == null) { 
       LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = (LinearLayout)inflater.inflate(R.layout.imagelistrow, null); 
       holder.iView = (ImageView)convertView.findViewById(R.id.imgBitmap); 
       holder.cbListCheck = (CheckBox)convertView.findViewById(R.id.multiplechecker); 
       holder.dragView = (ImageDraggerView)convertView.findViewById(R.id.singledragger); 
       convertView.setTag(holder); 
      } 
      else 
      { 
       holder = (ViewHolder)convertView.getTag(); 
      } 

      holder.cbListCheck.setChecked(false); 

      //ImageView iView= new ImageView(mContext,null); 
      cursor.moveToPosition(position); 
      int imageID = cursor.getInt(columnIndex); 
      Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID)); 
      String url = uri.toString(); 
      int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length())); 
      Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(pActivity.getContentResolver(),originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null); 
      holder.iView.setImageBitmap(b); 
      holder.iView.setLayoutParams(new ListView.LayoutParams(parentWidth/5, itemWidth)); 
      holder.iView.setScaleType(ImageView.ScaleType.FIT_XY); 
      //return holder.iView; 
      return convertView; 

     } 


    } 

    public ImageListView(Context context) { 
     super(context); 
     pActivity=(NoteActivity)context; 
     initLayout(); 
    } 

    public ImageListView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     pActivity=(NoteActivity)context; 
     initLayout(); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ 
     if(isFirstMeasure){ 
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
      parentWidth = MeasureSpec.getSize(widthMeasureSpec); 
      parentHeight = MeasureSpec.getSize(heightMeasureSpec); 
      itemWidth=parentHeight/12; 
      isFirstMeasure=false; 
     } 
     this.setMeasuredDimension(parentWidth/5,parentHeight/4); 

    } 

    public void initLayout(){ 
     isFirstMeasure=true; 
     paint.setAntiAlias(true); 
     paint.setColor(Color.argb(128, 256, 256, 256)); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setDither(true); 
     paint.setStrokeJoin(Paint.Join.ROUND); 
     paint.setStrokeCap(Paint.Cap.ROUND); 
     this.setWillNotDraw(false); 
     isImageSelected=false; 
     String[] projection = {MediaStore.Images.Media._ID}; 
     cursor = pActivity.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,projection, 
       MediaStore.Images.Media.DATA + " like ? ", new String[] {"%Images%"},null); 
     columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID); 
     this.setAdapter(new ImageAdapter(pActivity)); 
    } 
    } 

以下是我試圖膨脹的行。

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <CheckBox android:id="@+id/multiplechecker" 
     android:gravity="center_vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentBottom="true" 
     android:checked="false"/> 
    <ImageView android:id="@+id/imgBitmap" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:gravity="center_vertical" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentBottom="true"/> 
    <com.ui.ImageDraggerView android:id="@+id/singledragger" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:gravity="center_vertical" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentBottom="true"> 
    </com.ui.ImageDraggerView>  
</LinearLayout 

>

,但我得到了下面的錯誤。

E/AndroidRuntime(775):致命異常:主要 E/AndroidRuntime(775):java.lang.ClassCastException:android.widget.AbsListView $的LayoutParams不能轉換到android.widget.LinearLayout $的LayoutParams E/AndroidRuntime(775):at android.widget.LinearLayout.measureVertical(LinearLayout.java:587) E/AndroidRuntime(775):at android.widget.LinearLayout.onMeasure(LinearLayout.java:519) E/AndroidRuntime(775) :android.view.java:10577 E/AndroidRuntime(775):在android.widget.ListView.measureScrapChild(ListView.java:1165) E/AndroidRuntime(775):在android。 widget.ListView.measureHeightOfChildren(ListView.java:1230) E/AndroidRuntime(7 75):at android.widget.ListView.onMeasure(ListView.java:1139) E/AndroidRuntime(775):at com.ui.ImageListView.onMeasure(ImageListView.java:139)

回答

3

trace中的錯誤解釋問題清楚。
你在做
holder.iView.setLayoutParams(new ListView.LayoutParams....);
但它是一個LinearLayout的子視圖。
所以,你需要做
holder.iView.setLayoutParams(new LinearLayout.LayoutParams....);
事情會好起來的。

+0

謝謝......現在能膨脹。 – user1179815 2012-01-31 09:17:31

相關問題