0

我有一个 ListView,该列表包含一个布局,其中 View 组件添加到我的 RelativeLayout :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView 
    android:id="@+id/no"
    android:layout_width="40dp" 
    android:layout_height="40dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="5dp"
    android:gravity="center"
    />

<View
    android:id="@+id/line"
    android:layout_width="0.3dip"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/no"
    android:background="#FFdddddd" />

<TextView 
    android:id="@+id/name"
    android:textColor="#150517"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/line"
    android:layout_marginTop="15dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="10dp"
    android:layout_gravity="right"
    android:layout_alignParentRight="true"
    android:gravity="right"
    android:textSize="38sp"  />
</RelativeLayout>

MyCursorAdapter.java 的代码:

  public class MyCursorAdapter extends SimpleCursorAdapter{

  public MyCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
      super(context, layout, c, from, to, flags);
  }

  @Override 
  public View getView(int position, View convertView, ViewGroup parent) {
      View view = convertView;
      if(position % 2 == 0){
          view.setBackgroundColor(Color.rgb(243, 234, 214));
      }else{
          view.setBackgroundColor(Color.rgb(253, 247, 230));
      }
      ((TextView)view.findViewById(R.id.name)).setTypeface(Typeface.createFromAsset(view.getContext().getAssets(), "fonts/nytype.ttf"));
      return view;  
  }
 }  

当我开始运行我的应用程序时。有错误消息:

E/AndroidRuntime(19583):致命异常:主要

E/AndroidRuntime(19583): java.lang.IllegalStateException: android.view.View 不是这个 SimpleCursorAdapter 可以限制的视图

E/AndroidRuntime(19583):在 android.support.v4.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:145)

我该如何解决这个问题?

4

1 回答 1

1

这是因为你正在这样做

View view = super.getView(position, convertView, parent);

你不想那样做,你需要做的就是设置viewconvertView

View view = convertView;

检查视图是否为空,如果是则在此处膨胀您想要的视图

if(view == null){
    view = getLayoutInflater().inflate(r.layout.name,null);
} 

这是我如何做的一个例子

@Override
public View getView(int position, View convertView, ViewGroup parent){
    View v = convertView;
    if(v == null){
        convertView = View.inflate(context,R.layout.sin_preplan_row,null);
    }

    bindView(position, convertView);
    return convertView;
}

private void bindView(int position, View view){
    Map<String,Object> data = list.get(position);

    TextView name = (TextView)view.findViewById(R.id.name_text);
    TextView address = (TextView)view.findViewById(R.id.address_text);
    TextView notes = (TextView)view.findViewById(R.id.notes_text);
    TextView distance = (TextView)view.findViewById(R.id.distance_text);

    name.setText((String)data.get("name"));
    address.setText((String)data.get("address"));
    notes.setText((String)data.get(notes));
    distance.setText(String.valueOf((Float)data.get("distance")));

}

编辑:

将xml更改为此

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView 
android:id="@+id/no"
android:layout_width="40dp" 
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:gravity="center"
/>

<FrameLayout
android:id="@+id/line"
android:layout_width="0.3dip"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/no"
android:background="#FFdddddd" />

<TextView 
android:id="@+id/name"
android:textColor="#150517"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/line"
android:layout_marginTop="15dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_gravity="right"
android:layout_alignParentRight="true"
android:gravity="right"
android:textSize="38sp"  />
</RelativeLayout>
于 2013-05-21T21:04:00.403 回答