本帖最后由 jack_93 于 2014-5-1 14:29 编辑
这个类错了,这是自定义的SimperAdapter类,- package com.example.test3;
- import java.util.List;
- import java.util.Map;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- import android.widget.ImageView;
- import android.widget.TextView;
- public class MyAdapter extends BaseAdapter{
- private LayoutInflater mInflater;
- private List<Map<String, Object>> list;
- private int layoutID;
- private String flag[];
- private int ItemIDs[];
- public MyAdapter(Context context, List<Map<String, Object>> list, int layoutID, String flag[], int ItemIDs[]){
- this.mInflater = LayoutInflater.from(context);
- this.list = list;
- this.layoutID = layoutID;
- this.flag = flag;
- this.ItemIDs = ItemIDs;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return list.size();
- }
- @Override
- public Object getItem(int arg0) {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- convertView = mInflater.inflate(layoutID, null);
- for (int i = 0; i < flag.length; i++) {
- if (convertView.findViewById(ItemIDs[i]) instanceof ImageView) {
- ImageView iView = (ImageView) convertView.findViewById(ItemIDs[i]);
- iView.setBackgroundResource((Integer)list.get(position).get(flag[i]));
- }else if (convertView.findViewById(ItemIDs[i]) instanceof TextView) {
- TextView tView = (TextView) convertView.findViewById(ItemIDs[i]);
- tView.setText((String)list.get(position).get(flag[i]));
- }
- }
-
- ((Button)convertView.findViewById(R.id.Simple_btn)).setOnClickListener(
- new View.OnClickListener(){
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- new AlertDialog.Builder(MyAdapter.this)
- .setTitle("自定义SimpleAdapter")
- .setMessage("按钮成功触发监听事件!")
- .show();
- }
- });
- ((CheckBox)convertView.findViewById(R.id.Simple_cb)).
- setOnCheckedChangeListener(new OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- // TODO Auto-generated method stub
- new AlertDialog.Builder(null)
- .setTitle("自定义SimpleAdapter")
- .setMessage("CheckBox成功触发监听事件!")
- .show();
- }
- });
- return convertView;
- }
- }
复制代码
下面代码报错new AlertDialog.Builder(MyAdapter.this)- ((Button)convertView.findViewById(R.id.Simple_btn)).setOnClickListener(
- new View.OnClickListener(){
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- new AlertDialog.Builder(MyAdapter.this)
- .setTitle("自定义SimpleAdapter")
- .setMessage("按钮成功触发监听事件!")
- .show();
- }
- });
- ((CheckBox)convertView.findViewById(R.id.Simple_cb)).
- setOnCheckedChangeListener(new OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- // TODO Auto-generated method stub
- new AlertDialog.Builder(null)
- .setTitle("自定义SimpleAdapter")
- .setMessage("CheckBox成功触发监听事件!")
- .show();
- }
- });
复制代码
自己感觉是这个类没有实例化,但是android不是java,onCreate就是开始Activity。如果可以解释下android的class中什么标志着这个class被实例就更好了。
我在另一个类中调用了该类
- sp = new MyAdapter(this, list, R.layout.simpleadapter, new String[]{"item1_imageview","item1_bigtv","BIGTV","item1_smalltv"}, new int[] {R.id.Simple_iv,R.id.Simple_bigtv,R.id.Simple_smalltv})
-
复制代码
|