我正在创建一个自定义列表视图,该列表视图在Android Studio中具有一个复选框和一个文本视图。目标是根据选中的复选框更改为其他活动。我想为此使用onitemclicklistener,但它不起作用。
布局正确显示,我可以选中和取消选中这些框。任何帮助将不胜感激!
package com.example.godoc;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import java.util.ArrayList;
public class SymptomListAdapter extends ArrayAdapter<String> {
private static final String TAG = "SymptomListAdapter";
private Context mContext;
int mResource;
public SymptomListAdapter(Context context, int resource,
ArrayList<String> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}
//alt+insert override methods and insert getview. Responsible for
attaching listview
to layout
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//get information
String symptom = getItem(position);
//inflate layout
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
TextView symptomTextView = (TextView)
convertView.findViewById(R.id.textviewlistviewlayout);
symptomTextView.setText(symptom);
CheckBox symptomCheckBox = (CheckBox)
convertView.findViewById(R.id.checkboxlistviewlayout);
convertView.setClickable(true);
return convertView;
}
}
final ListView listView = findViewById(R.id.listView);
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("Gevallen");
arrayList.add("Duizeligheid");
arrayList.add("Verlamming");
arrayList.add("Zicht- of spraakstoornis");
arrayList.add("Verward- of sufheid");
arrayList.add("Nekstijfheid");
arrayList.add("Braken");
arrayList.add("Geen van bovenstaande");
//use own adapter
SymptomListAdapter adapter = new SymptomListAdapter(this,
R.layout.listviewlayout, arrayList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
Log.i("OnitemClick", "pressed");
}
});
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="40dp"
android:layout_width="match_parent"
android:orientation="horizontal"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:weightSum="100"
>
<TextView
android:id="@+id/textviewlistviewlayout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="10"
android:textColor="#ffffff"
app:fontFamily="@font/montserrat"
android:gravity="center_vertical"
android:textSize="18sp"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="90">
<CheckBox
android:id="@+id/checkboxlistviewlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
app:buttonTint="#ffffff"
app:fontFamily="@font/montserrat"/>
</LinearLayout>
</LinearLayout>