我有一个ListView,该列表包含一个Layout with View Component添加到我的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)
如何解决此问题?
答案 0 :(得分:1)
因为你这样做
View view = super.getView(position, convertView, parent);
您不想这样做,您需要做的就是将view
设置为convertView
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>