我正在研究自学习机器人,现在正尝试用复选框创建列表视图。但是当我运行程序时,我确实看到带有复选框的列表视图,但是当我选择任何选项时,我看不到复选框中的检查。我在这里错过了什么吗?
MainActivity:
git revert
activity_main.xml中
package com.example.practisetutorials.myapplication;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.Menu;
public class MainActivity extends Activity {
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
ListView listView = (ListView) findViewById(R.id.listView);
String[] fruits = {"Banana", "Mango", "Apple", "Guava", "Orange", "Water Melon", "Grapes", "Cantaloupe", "Strawberry", "Blackberry", "Blueberry", "Papaya", "Pineapple"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.fruit_item, R.id.checkedTextView, fruits);
listView.setAdapter(adapter);
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
fruit_item.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.practisetutorials.myapplication.MainActivity">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
答案 0 :(得分:0)
您缺少setOnItemClickListener
看看这里:http://windrealm.org/tutorials/android/listview-with-checkboxes.php
答案 1 :(得分:0)
您是否尝试使用CheckedTextView?我通常只使用CheckBox。它开箱即用。以下是如何在不修改代码的情况下执行此操作:
MainActivity:
package com.example.test;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
ListView listView = (ListView) findViewById(R.id.listView);
String[] fruits = {"Banana", "Mango", "Apple", "Guava", "Orange", "Water Melon", "Grapes", "Cantaloupe", "Strawberry", "Blackberry", "Blueberry", "Papaya", "Pineapple"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.fruit_item, R.id.checkedTextView, fruits);
listView.setAdapter(adapter);
// listView.setItemsCanFocus(false);
// listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
然后是fruit_item.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="vertical" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/checkedTextView"
android:gravity="center"
android:checkMark="@color/accent_material_light"/>
</LinearLayout>
希望这有帮助!
答案 2 :(得分:0)
如上所述,您在列表视图中缺少点击监听器。在onCreate的底部添加:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "You tapped: " +fruits[position], Toast.LENGTH_SHORT).show();
}
});
并声明你的String []为final,这样你就可以在你的点击监听器中访问它。
答案 3 :(得分:0)
尝试在listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE)下面添加以下这些行;在onCreate()方法中。
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView checkedTextView = (CheckedTextView) view.findViewById(R.id.checkedTextView);
if(checkedTextView.isChecked()) {
checkedTextView.setChecked(false);
} else {
checkedTextView.setChecked(true);
}
}
});