Android MultiChoiceAdapter列表视图
我正在用Listview进行MultiChoiceAdapter。但当我运行时崩溃了。什么是错误,请帮助我 这一行可能有问题: CheckedTextView checkedView=(CheckedTextView)view.findViewById(R.id.checkBox1) 维护性:Android MultiChoiceAdapter列表视图,android,android-listview,android-checkbox,Android,Android Listview,Android Checkbox,我正在用Listview进行MultiChoiceAdapter。但当我运行时崩溃了。什么是错误,请帮助我 这一行可能有问题: CheckedTextView checkedView=(CheckedTextView)view.findViewById(R.id.checkBox1) 维护性: package com.example.ppp; 导入java.util.ArrayList; 导入java.util.HashMap; 导入java.util.List; 导入android.app.A
package com.example.ppp;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入android.app.Activity;
导入android.content.Context;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.ViewGroup;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.CheckedTextView;
导入android.widget.ListView;
导入android.widget.Toast;
公共类MainActivity扩展了活动{
列表视图;
按钮获取结果;
private ArrayList DayOfWeeklList=新建ArrayList();
私有void initdayOfWeeklList(){
添加(“星期日”);
添加星期一(“星期一”);
添加“星期二”;
添加星期三;
添加(“星期五”);
添加(“星期日”);
添加(“星期六”);
}
MyArrayAdapter适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getResult=(按钮)findViewById(R.id.btnResult);
initDayOfWeeklList();
ListView lv=(ListView)findViewById(R.id.listView1);
适配器=新的MyArrayAdapter(此,
R.layout.adapter_布局,R.id.复选框1
,星期一);
低压设置适配器(适配器);
lv.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共控件单击(AdapterView父对象、视图v、内部位置、,
长id){
适配器。开关已检查(位置);
}
});
getResult.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图arg0){
//TODO自动生成的方法存根
字符串res=“”;
ArrayList resultList=adapter.getCheckedItem();
对于(int i=0;i
adapter_layout.xml:
activity_main.xml
如评论所述,您的适配器\u布局没有
CheckedTextView
-checkBox1
是一个复选框(这是一个TextView
)。您应该这样做:
<CheckedTextView
android:id="@+id/checkBox1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginTop="5dp"
android:text="CheckBox" />
此外,您的“活动”主布局没有listView1,但看起来您可能已经粘贴了两次相同的布局。因为它是一个复选框,而不是checkedtextview。要么在布局中声明checkedtextview,要么在java中强制转换为复选框。(是的,发布代码(xml布局和适配器定义)会很有用。如果你看一些其他android问题,你会发现发布代码和stacktrace通常是解决问题的先决条件)
package com.example.ppp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView myListView;
Button getResult;
private ArrayList<String> dayOfWeekList=new ArrayList<String>();
private void initDayOfWeekList(){
dayOfWeekList.add("sunday");
dayOfWeekList.add("Monday");
dayOfWeekList.add("Tuesday");
dayOfWeekList.add("Wednesday");
dayOfWeekList.add("Fridayday");
dayOfWeekList.add("sunday");
dayOfWeekList.add("satday");
}
MyArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getResult=(Button)findViewById(R.id.btnResult);
initDayOfWeekList();
ListView lv=(ListView)findViewById(R.id.listView1);
adapter=new MyArrayAdapter(this,
R.layout.adapter_layout, R.id.checkBox1
, dayOfWeekList);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
adapter.toggleChecked(position);
}
});
getResult.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String res="";
ArrayList<String> resultList=adapter.getCheckedItem();
for (int i = 0; i < resultList.size(); i++) {
res+=String.valueOf(resultList.get(i))+"\n";
adapter.getCheckedPosition().toString();
Toast.makeText(getApplicationContext(), res, Toast.LENGTH_LONG).show();
}
}
});
}
private class MyArrayAdapter extends ArrayAdapter<String>{
private HashMap<Integer, Boolean> myChecked=new HashMap<Integer, Boolean>();
public MyArrayAdapter(Context context, int resource,
int textViewResourceId, List<String> objects) {
super(context, resource, textViewResourceId, objects);
for (int i = 0; i < objects.size(); i++) {
myChecked.put(i, false);
}
}
public void toggleChecked(int position) {
if(myChecked.get(position)){
myChecked.put(position, false);
Toast.makeText(getApplicationContext(), "false", Toast.LENGTH_LONG).show();
}else{
myChecked.put(position, true);
}
notifyDataSetChanged();
}
public ArrayList<Integer> getCheckedPosition() {
ArrayList<Integer> CheckedPosition=new ArrayList<Integer>();
for (int i = 0; i < myChecked.size(); i++) {
if(myChecked.get(i)){
(CheckedPosition).add(i);
}
}
return CheckedPosition;
}
public ArrayList<String> getCheckedItem() {
ArrayList<String> CheckedItem=new ArrayList<String>();
for (int i = 0; i < myChecked.size(); i++) {
if(myChecked.get(i)){
(CheckedItem).add(dayOfWeekList.get(i));
}
}
return CheckedItem;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=convertView;
if(view==null){
LayoutInflater inflat=getLayoutInflater();
view=inflat.inflate(R.layout.adapter_layout, parent,false);
}
CheckedTextView checkedView=(CheckedTextView) view.findViewById(R.id.checkBox1);
checkedView.setText(dayOfWeekList.get(position));
Boolean checked=myChecked.get(position);
if(checked!=null){
checkedView.setChecked(checked);
}
return view;
}
}
}
<?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="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginTop="5dp"
android:text="CheckBox" />
</LinearLayout>
<?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="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginTop="5dp"
android:text="CheckBox" />
</LinearLayout>
<CheckedTextView
android:id="@+id/checkBox1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginTop="5dp"
android:text="CheckBox" />