SimpleAdapter实现点击事件

package com.woyoli.fragments;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.woyoli.R;

public class HomePageFragment extends Fragment {

	private ListView mListView;
	private SimpleAdapter simpleAdapter;
	private ArrayList> mList;
	private MyAdapter mMyAdapter;
	int[] drawables = new int[]{R.id.iv_home_page, R.id.iv_home_page};
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		//getActivity().getActionBar()
		mMyAdapter = new MyAdapter(getActivity(), getData(), R.layout.home_page_list_row, 
				new String[]{"image"}, 
				new int[]{R.id.iv_home_page});
		
		View mView = inflater.inflate(R.layout.fragment_main_home_page, null);
		mListView = (ListView) mView.findViewById(R.id.lv_home_page);
		mListView.setAdapter(mMyAdapter);
		mListView.setOnItemClickListener(new MyListener());
		return mView;
	}
	
	private List> getData() {
		mList = new ArrayList>();
		Map mMap = new HashMap();
		mMap.put("image", R.drawable.home_page_sample);
		mMap.put("data", "one");
		mList.add(mMap);
		
		mMap = new HashMap(); 
		mMap.put("image", R.drawable.home_page_sample);
		mMap.put("data", "two");
		mList.add(mMap);
		return mList;
	}
	
	class MyListener implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView parent, View view, int position, long id) {
			Map mMap = (Map) mMyAdapter.getItem(position);
			Toast.makeText(getActivity(), mMap.get("data").toString(), 0).show();
		}
	}
	
	class MyAdapter extends SimpleAdapter {
		private Context context;

		public MyAdapter(Context context, List> data, int resource, String[] from, int[] to) {
			super(context, data, resource, from, to);
			this.context = context;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			convertView = super.getView(position, convertView, parent); 
			CheckBox mCheckBox = (CheckBox) convertView.findViewById(R.id.favorites);
			mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					Toast.makeText(context, "state changes", 0).show();
				}
			});
			
			return convertView;
		}
		
		
		
	}
}

你可能感兴趣的:(Android,SimpleAdapter,安卓Adapter,安卓SimpleAdapter,SimpleAdapter例子,Adapter例子)