Android CheckedTextView在ListView中不可单击
当我点击listView查看选中的文本视图时,应用程序停止。下面是list_item.xmlAndroid CheckedTextView在ListView中不可单击,android,android-layout,listview,checkedtextview,Android,Android Layout,Listview,Checkedtextview,当我点击listView查看选中的文本视图时,应用程序停止。下面是list_item.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="?andro
<?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="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<CheckedTextView
android:id="@+id/checkedTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:gravity="center"
android:drawableLeft="?android:attr/listChoiceIndicatorMultiple"
/>
</RelativeLayout>
以下是活动的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" >
<ListView
android:id="@+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
</ListView>
<Button
android:id="@+id/button1"
android:layout_width="127dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Submit" />
</LinearLayout>
以下是活动的代码:
package com.example.arf.mycitymyenvironment;
import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class ProblemActivity extends Activity {
ListView listView ;
String[] values;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_problem);
//Toast.makeText(ProblemActivity.this,"new activity",Toast.LENGTH_SHORT).show();
listView = (ListView) findViewById(R.id.list);
values = new String[] { "Garbage","Water Pollution","Air Pollution" };
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.list_item, R.id.checkedTextView, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
// we want multiple clicks
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnItemClickListener(new CheckBoxClick());
/*
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
//checkbox
//checkBox.isChecked();
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :" + itemPosition + " ListItem : "+itemValue , Toast.LENGTH_LONG)
.show();
}
});
*/
}
public class CheckBoxClick implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
CheckedTextView ctv = (CheckedTextView)arg1;
if(ctv.isChecked()){
Toast.makeText(ProblemActivity.this, "now it is unchecked", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(ProblemActivity.this, "now it is checked", Toast.LENGTH_SHORT).show();
}
}
}
}
package com.example.arf.mycitymyenvironment;
导入android.app.Activity;
导入android.content.Context;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.BaseAdapter;
导入android.widget.CheckBox;
导入android.widget.CheckedTextView;
导入android.widget.ImageView;
导入android.widget.ListView;
导入android.widget.TextView;
导入android.widget.Toast;
公共类问题活动扩展活动{
列表视图列表视图;
字符串[]值;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_问题);
//Toast.makeText(ProblemActivity.this,“新活动”,Toast.LENGTH_SHORT.show();
listView=(listView)findViewById(R.id.list);
值=新字符串[]{“垃圾”、“水污染”、“空气污染”};
//定义一个新适配器
//第一个参数-上下文
//第二个参数-行的布局
//第三个参数-写入数据的TextView的ID
//第四,数据数组
最终ArrayAdapter适配器=新的ArrayAdapter(此,
R.layout.list_项,R.id.checkedTextView,值);
//将适配器分配给ListView
setAdapter(适配器);
//我们需要多次点击
listView.setChoiceMode(listView.CHOICE\u MODE\u MULTIPLE);
setOnItemClickListener(新的CheckBoxClick());
/*
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//ListView单击的项目索引
int itemPosition=位置;
//ListView单击的项目值
String itemValue=(String)listView.getItemAtPosition(position);
//复选框
//checkBox.isChecked();
//显示警惕
Toast.makeText(getApplicationContext(),
位置:“+itemPosition+”列表项:“+itemValue,Toast.LENGTH\u LONG)
.show();
}
});
*/
}
公共类CheckBoxClick实现AdapterView.OnItemClickListener{
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
//TODO自动生成的方法存根
CheckedTextView ctv=(CheckedTextView)arg1;
如果(ctv.isChecked()){
Toast.makeText(ProblemActivity.this,“现在未选中”,Toast.LENGTH_SHORT.show();
}否则{
Toast.makeText(ProblemActivity.this,“现在已检查”,Toast.LENGTH_SHORT.show();
}
}
}
}
每次,错误都是CheckedTextView ctv=(CheckedTextView)arg1代码>
如何解决此问题?公共类复选框Click implements AdapterView.OnItemClickListener{
public class CheckBoxClick implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
CheckedTextView ctv = (CheckedTextView)findViewById(R.id.checkedTextView);
if(ctv.isChecked()){
Toast.makeText(ProblemActivity.this, "now it is unchecked", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(ProblemActivity.this, "now it is checked", Toast.LENGTH_SHORT).show();
}
}
}
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
//TODO自动生成的方法存根
CheckedTextView ctv=(CheckedTextView)findViewById(R.id.CheckedTextView);
如果(ctv.isChecked()){
Toast.makeText(ProblemActivity.this,“现在未选中”,Toast.LENGTH_SHORT.show();
}否则{
Toast.makeText(ProblemActivity.this,“现在已检查”,Toast.LENGTH_SHORT.show();
}
}
}
试试这个代码。试试这个:
public class CheckBoxClick implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
CheckedTextView ctv = (CheckedTextView)arg1.findViewById(R.id.checkedTextView);
if(ctv.isChecked()){
Toast.makeText(ProblemActivity.this, "now it is unchecked", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(ProblemActivity.this, "now it is checked", Toast.LENGTH_SHORT).show();
}
}
}
public类CheckBoxClick实现AdapterView.OnItemClickListener{
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
//TODO自动生成的方法存根
CheckedTextView ctv=(CheckedTextView)arg1.findViewById(R.id.CheckedTextView);
如果(ctv.isChecked()){
Toast.makeText(ProblemActivity.this,“现在未选中”,Toast.LENGTH_SHORT.show();
}否则{
Toast.makeText(ProblemActivity.this,“现在已检查”,Toast.LENGTH_SHORT.show();
}
}
}
您的视图arg1
是RelativeLayout
CheckedTextView
是RelativeLayout arg1
的子级,因此可以使用arg1.findViewById(R.id.CheckedTextView)获取它代码>为每行的CheckedTextView设置onClickListener,并在onClick事件中切换CheckedTextView的值
在ListView适配器的getView方法中添加以下代码
checkedTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkedTextView.setChecked(!checkedTextView.isChecked());
}
});
您在一个RelativeLayout
中有CheckedTextView
,因此arg1
是一个RelativeLayout
,您不能将其转换为CheckedTextView
。如果您不打算在列表项布局中放置任何其他内容,则可以去掉RelativeLayout
。否则,请在arg1
上调用findViewById()
以获取CheckedTextView
。我不知道为什么有人否决了你的答案。谢谢!这有助于我让它工作