Code Bye

SimpleAdapter重写时响应CheckBox复选框状态改变-源码修改

本人想实现的是:点击button按钮就可以在info哪里显示出来,或弹出一个对话框显示也可以。
下面是源码,运行就错误,哪位高手帮忙修改一下,第一次在android区发帖,请多多指点。
工具android studio
下面是content_main_xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/info"
        android:id="@+id/info"
        android:textSize="15dp"
        android:layout_marginTop="20dp" />
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/datalist"
        android:fastScrollAlwaysVisible="false"
        android:layout_marginTop="20dp" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button1" />
</LinearLayout>

下面是:fruit_item_xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <ImageView android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:maxHeight="72px"
        android:maxWidth="72px"
        android:adjustViewBounds="true"
        android:paddingBottom="20px"
        android:paddingTop="20px"
        android:paddingRight="10px"
        android:id="@+id/image"
        android:layout_marginLeft="20dp" />
    <TextView android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/title"
        android:layout_gravity="center"
        android:padding="10px"/>
    </LinearLayout>
    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="right">
           <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:scaleX="1.5"
            android:scaleY="1.5"
            android:checked="false"
            android:focusable="false"
            android:clickable="false"
            android:focusableInTouchMode="false"
            android:id="@+id/ckb"
               android:layout_marginTop="20dp"
               android:layout_marginRight="20dp" />
    </LinearLayout>
</LinearLayout>

下面是MainActivity:

package tw.a05.com.flag.ch06_listviewImageCheck;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
    // 1. 新建一个数据适配器
    private List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
    private ListView listview = null;
    private SimpleAdapter adapter = null;
    private TextView info = null;
    private CheckBox ckb = null;
    private boolean checked;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview = (ListView) findViewById(R.id.datalist);
        info = (TextView) findViewById(R.id.info);
        ckb = (CheckBox) findViewById(R.id.ckb);
        int[] imageId = new int[]{R.drawable.img01, R.drawable.img02,
                R.drawable.img03, R.drawable.img04, R.drawable.img05,
                R.drawable.img06, R.drawable.img07, R.drawable.img08}; // 定义并初始化保存图片id的数组
        String[] title = new String[]{"保密设置", "安全", "系统设置", "上网", "本人的文档",
                "GPS导航", "本人的音乐", "E-mail"}; // 定义并初始化保存列表项文字的数组
        boolean[] checked = {false, false, false, false, false, false, false, false,};
        for (int i = 0; i < imageId.length; i++) {
            Map<String, Object> map = new HashMap<>(); // 实例化Map对象
            map.put("image", imageId[i]);
            map.put("title", title[i]);
            map.put("ckb", checked[i]);
            listItems.add(map); // 将map对象添加到List集合中
        }
        adapter = new MySimpleAdapter(this, listItems, R.layout.fruit_item, new String[]{"image", "title", "ckb"},
                new int[]{R.id.image,R.id.title, R.id.ckb});
        listview.setAdapter(adapter);
        listview.setOnItemClickListener((AdapterView.OnItemClickListener) this);
        //这里要设置CKB的监听
        ckb.setOnCheckedChangeListener(new OnCheckedChangeListenermpl());
    }
    //点击标题相应事件
    private class OnItemClickListenerImpl implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Map<String, Object> map = (Map<String, Object>) adapter.getItem(position);
            String _id = map.get("image").toString();
            String name = map.get("title").toString();
            info.setText("你选择的是:" + name);
        }
    }
    // 写一个能监听checkBox勾选事件的SimpleAdapter,假如不是checkBox而是button.onClick事件也是相似的
    private class MySimpleAdapter extends SimpleAdapter
    {
        MainActivity context;
        public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
            super(context, data, resource, from, to);
            this.context = (MainActivity) context;
            // TODO Auto-generated constructor stub
        }
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View v = super.getView(position, convertView, parent);
            Button btn=(Button) v.findViewById(R.id.button1);
            //final CheckBox check = (CheckBox)v.findViewById(R.id.ckb);
            final int p = position;
            Log.d("Position", Integer.toString(position));
            //复选框的响应
            ckb.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    String s = (p+":"+ckb.isChecked());
                    Toast.makeText(context, s, Toast.LENGTH_LONG).show();
                    if(ckb.isChecked()){
                        context.setChecked(p, true);
                    }else if(!ckb.isChecked()){
                        context.setChecked(p, false);
                    }
                }
            });
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(context, "删除"+Integer.toString(p),Toast.LENGTH_LONG).show();
                    //context.deleteItemsTodayPlanList(p);
                }
            });
            return v;
        }
    }
    public void setChecked(int position,boolean m){
        //Map<String,Object> map = today_plan_listItems.get(position);
        Map<String, Object> map = (Map<String, Object>) adapter.getItem(position);
        map.put("ckb", m);
        listItems.set(position, map);
        listview.invalidate();
    }
}

重写SimpleAdapter引用出处:http://blog.csdn.net/zgljl2012/article/details/44578181
由于是新手,本人想要改好的完整代码,哪位高手能帮下忙。

解决方案

100

昨天把解决方案发你邮箱了,收到了吗?

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明SimpleAdapter重写时响应CheckBox复选框状态改变-源码修改