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

时间:2021-12-08 22:14:03
**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;
}

} adapter_xml

<?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>
listview_actitvty

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


}

activity_xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.mohamedreda.note.Delete">


    <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="38dp"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

    </ListView>


</RelativeLayout>

this output of 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]

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]

error * java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setTag(java.lang.Object)' on a null object reference at com.example.mohamedreda.note.checklist.getView(checklist.java:32)*

error * java.lang.NullPointerException:尝试在com.example.mohamedreda.note.checklist.getView(清单中)的空对象引用上调用虚方法'void android.widget.CheckBox.setTag(java.lang.Object)'。的java:32)*

1 个解决方案

#1


you can : ""+name.get(position);

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

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

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

#1


you can : ""+name.get(position);

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

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

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