如何在android中的Fragment中用checkBox实现listView

时间:2015-01-29 11:23:05

标签: android listview fragment

我是android新手。我用滑动手势构建了Tabs。在其中一个片段中我想要带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,这段代码运行正常。请帮帮我。提前致谢

2 个答案:

答案 0 :(得分:2)

您只需要setAdapterListView

listView.setAdapter(adapter);

答案 1 :(得分:0)

使用此功能将对您有用。

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);
listView.setAdapter(adapter);
    return rootView;
} }