2013-05-02 60 views
0

我需要在3个文本视图中显示每个网格项内的数据,但我也希望每个网格项都是正方形。此外,cursoradapter全部基于搜索。自定义ViewGroup,GridView和SimpleCursorAdapter

根据我的需求,我发现我应该为每个网格项目创建自己的视图组,并将高度设置为与宽度相同(在onMeasurement中),从而生成适当的正方形。但是现在我的cursoradapter数据不会显示在视图中。

我可以告诉cursoradapter被引用,因为GridView中的白色框的数量根据我搜索的内容而变化。

我哪里错了?

<ubiquia.sqbxsync.main.GridViewGroup 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:background="#ffffff"> 

<RelativeLayout 
    android:id="@+id/gridLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#ffffff"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:ellipsize="end" 
     android:singleLine="true" 
     android:text="TextView" 
     android:textSize="15dp" 
     android:textColor="#ff000000" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textView1" 
     android:layout_marginLeft="10dp" 
     android:ellipsize="end" 
     android:singleLine="true" 
     android:text="TextView" 
     android:textSize="11dp" 
     android:textColor="#ff000000" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView2" 
     android:layout_marginLeft="10dp" 
     android:ellipsize="end" 
     android:singleLine="true" 
     android:text="TextView" 
     android:textSize="11dp" 
     android:textColor="#ff000000" /> 

</RelativeLayout> 

package ubiquia.sqbxsync.main; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.ViewGroup; 

public class GridViewGroup extends ViewGroup { 

    public GridViewGroup(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) { 
    // Do nothing 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, widthMeasureSpec); 
    } 
} 

我的CursorAdapter

public class GridCursorAdapter extends CursorAdapter { 

    public GridCursorAdapter(Context context, Cursor c) { 
     super(context, c); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View gridView = inflater.inflate(R.layout.grid_item, null, true); 
     return gridView; 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     TextView tv1 = (TextView) view.findViewById(R.id.textView1); 
     TextView tv2 = (TextView) view.findViewById(R.id.textView2); 
     TextView tv3 = (TextView) view.findViewById(R.id.textView3); 
     tv1.setText(cursor.getString(cursor.getColumnIndexOrThrow("trackingnumber"))); 
     tv2.setText(cursor.getString(cursor.getColumnIndexOrThrow("recipient_name"))); 
     tv3.setText(cursor.getString(cursor.getColumnIndexOrThrow("carrier_name"))); 
    } 
} 
+0

'onLayout()'方法负责将孩子放在父母的区域,因为你没有实现它... – Luksprog 2013-05-02 21:01:15

+0

我所说的是,孩子被正确放置..只是内容是没有被安置在孩子身上。从android文档说,onLayout用于定位和大小的孩子,我已经在onMeasure中调整大小。 – 2013-05-02 21:09:59

+0

我很困惑如何将我的cursoradapter应用于ViewGroup @Luksprog中的文字视图 – 2013-05-03 17:38:15

回答

0

我发现我的变化延伸到的LinearLayout,写在super.onLayout(改变,L,T,R,B后);它现在在我想要的广场上显示我的布局。