【发布时间】:2015-01-29 11:23:04
【问题描述】:
我是安卓新手。我用滑动手势构建了标签。在其中一个片段中,我想要带有 CheckBox 的 listView。这是我的代码。
public class AddPeople extends Fragment
{
ListView listView;
List<String> names= new ArrayList<String>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.fragment_addpeople, container, false);
listView= (ListView) rootView.findViewById(R.id.listView1);
List<String> nameList= readContacts(getActivity());
CustomAdapter adapter= new CustomAdapter(getActivity(), nameList);
return rootView;
} }
这里是 CustomAdapter 类:
public class CustomAdapter extends ArrayAdapter
{
Context context;
List<String> modelItems;
@SuppressWarnings("unchecked")
public CustomAdapter(Context context, List<String> resource)
{
super(context,R.layout.row,resource);
// TODO Auto-generated constructor stub
this.context = context;
this.modelItems = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(R.layout.row, parent, false);
TextView name = (TextView) convertView.findViewById(R.id.textView1);
CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
name.setText(modelItems.get(position));
return convertView;
}
}
这里是 row.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="20dp" />
</LinearLayout>
这里是 fragment_addpeople.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@drawable/list_item_bg_pressed"
android:dividerHeight="1dp" >
</ListView>
</RelativeLayout>
我没有收到任何错误。但复选框不显示。但是对于 Activity,这段代码可以正常工作。请帮帮我。提前致谢
【问题讨论】: