简体   繁体   English

如何在Android中的Fragment中用chechBox实现listView

[英]How to implement listView with chechBox in Fragment in android

I am new to android. 我是Android新手。 I have built Tabs with swipe gesture. 我已经使用滑动手势构建了标签页。 In one of the fragments I want listView with CheckBox. 在片段之一中,我想要带CheckBox的listView。 Here is my code. 这是我的代码。

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;
} } 

Here is CustomAdapter class: 这是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;
 }
}

Here is row.xml file: 这是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>

Here is fragment_addpeople.xml file : 这是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>

I am not getting any error. 我没有任何错误。 But Check box does not show up. 但是复选框不会显示。 But with Activity this code works fine. 但是使用Activity,此代码可以正常工作。 Please help me out. 请帮帮我。 Thanks in advance 提前致谢

您只需要将setAdapterListView

listView.setAdapter(adapter);

Use this it will work for you. 使用它会为您工作。

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;
} } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM