如何使用复选框和文本创建自定义列表

时间:2015-05-11 09:43:35

标签: android android-listview android-checkbox

Options +Indexes

} 的 adapter_xml



**Adapter class**
 public class checklist extends ArrayAdapter<String> {
private final Activity context;
private final ArrayList<String> name;

public checklist(Activity context,ArrayList<String> name) {
    super(context, R.layout.activity_custm_list, name);
    this.context = context;
    this.name = name;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView= inflater.inflate(R.layout.activity_custm_list, null, true);
    TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);

    CheckBox checkBox = (CheckBox) rowView.findViewById(R.id.check);
    txtTitle.setText(name.get(position));

    return rowView;
}
&#13;
&#13;
&#13; 的 listview_actitvty

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TableRow>
        <CheckBox
            android:id="@+id/check"
            android:layout_width="50dp"
            android:layout_height="50dp"/>

        <TextView
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="50dp" />

    </TableRow>
</TableLayout>

activity_xml

&#13;
&#13;
public class Delete extends ActionBarActivity {


ListView lv ;
ArrayList<String> name = new ArrayList<>();
ArrayList<Integer> ids = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_delete);
    lv = (ListView) findViewById(R.id.list);

    Intent intent = getIntent();
    name = intent.getStringArrayListExtra("note name");
    ids = intent.getIntegerArrayListExtra("id");
    System.out.println("-------*********----------------"+name);
   // checklist dapter = new checklist(Delete.this,name);
    checklist adapter = new checklist(Delete.this, name);

    lv.setAdapter(adapter);


}
&#13;
&#13;
&#13;

system.out.print 的输出 05-11 05:19:03.072 5464-5464 / com.example.mohamedreda.note I / System.out:----------- ********* --------- ------- [alissaas,mes,hhhhhhhhh,meshoo,zeko,mostafaaaa,ahmed,hi,ahmedddd,kattttb,xxxxxxxxxxx,sssssssssssssss,------------------ -----------------,a44444,n,no,mes2,zeko5,ahmed7]

错误 *     java.lang.NullPointerException:尝试调用虚方法&#39; void android.widget.CheckBox.setTag(java.lang.Object)&#39;在null对象引用上             在com.example.mohamedreda.note.checklist.getView(checklist.java:32)*

1 个答案:

答案 0 :(得分:0)

你可以:""+name.get(position);

成为:txtTitle.setText(""+name.get(position));