Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 483|回复: 0

开发交流:Android 重写Adapter(二)

[复制链接]

该用户从未签到

发表于 2011-10-24 10:46:39 | 显示全部楼层 |阅读模式
  这个文件是Activity的布局文件:

java代码: <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>



<ListView android:id="@+id/listview"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:cacheColorHint="#00000000"

/>



</LinearLayout>
复制代码有了上面的两个文件,就可以写Activity了

       下面的是源码:

java代码: import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;



import android.app.Activity;

import android.content.Context;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.AdapterView;

import android.widget.CheckBox;

import android.widget.ListView;

import android.widget.SimpleAdapter;

import android.widget.TextView;

import android.widget.AdapterView.OnItemClickListener;



import com.heji.demo.R;



public class MultipleChoiceActivity extends Activity implements OnItemClickListener{



private MyAdapter mSimpleAdapter;



public final static String NAME = "name";

public final static String PHONE_NUMBER = "phone";



@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);



setTitle("带有多选的Listview");



setContentView(R.layout.multiple_checkbox_main);



ListView listView = (ListView) findViewById(R.id.listview);



ArrayList<Map<String, String>> al = new ArrayList<Map<String,String>>();

Map<String, String> map1 = new HashMap<String, String>();



map1.put(NAME, "A");

map1.put(PHONE_NUMBER, "132456789");

al.add(map1);



Map<String, String> map2 = new HashMap<String, String>();

map2.put(NAME, "B");

map2.put(PHONE_NUMBER, "132134");

al.add(map2);



Map<String, String> map3 = new HashMap<String, String>();

map3.put(NAME, "C");

map3.put(PHONE_NUMBER, "132134");

al.add(map3);



Map<String, String> map4 = new HashMap<String, String>();

map4.put(NAME, "D");

map4.put(PHONE_NUMBER, "132134");

al.add(map4);



Map<String, String> map5 = new HashMap<String, String>();

map5.put(NAME, "E");

map5.put(PHONE_NUMBER, "132134");

al.add(map5);



Map<String, String> map6 = new HashMap<String, String>();

map6.put(NAME, "F");

map6.put(PHONE_NUMBER, "132134");

al.add(map6);



Map<String, String> map7 = new HashMap<String, String>();

map7.put(NAME, "G");

map7.put(PHONE_NUMBER, "132134");

al.add(map7);





Map<String, String> map8 = new HashMap<String, String>();

map8.put(NAME, "H");

map8.put(PHONE_NUMBER, "132134");

al.add(map8);



Map<String, String> map9 = new HashMap<String, String>();

map9.put(NAME, "I");

map9.put(PHONE_NUMBER, "132134");

al.add(map9);



Map<String, String> map10 = new HashMap<String, String>();

map10.put(NAME, "J");

map10.put(PHONE_NUMBER, "132134");

al.add(map10);



Map<String, String> map11 = new HashMap<String, String>();

map11.put(NAME, "K");

map11.put(PHONE_NUMBER, "132134");

al.add(map11);





String[] from = { NAME, PHONE_NUMBER };

int[] to = { R.id.multiple_title, R.id.multiple_summary };

mSimpleAdapter = new MyAdapter(this, al, R.layout.multiple_checkbox_main_row, from, to);



listView.setAdapter(mSimpleAdapter);

listView.setOnItemClickListener(this);

}



@Override

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

CheckBox checkBox = (CheckBox) view.findViewById(R.id.multiple_checkbox);



checkBox.toggle();



mSimpleAdapter.map.put(position, checkBox.isChecked());



}



public class MyAdapter extends SimpleAdapter {



Map<Integer, Boolean> map;



LayoutInflater mInflater;



private List<? extends Map<String, ?>> mList;



public MyAdapter(Context context, List<Map<String, String>> data,

int resource, String[] from, int[] to) {

super(context, data, resource, from, to);

map = new HashMap<Integer, Boolean>();

mInflater = LayoutInflater.from(context);

mList = data;

for(int i = 0; i < data.size(); i++) {

map.put(i, false);

}

}



@Override

public int getCount() {

return mList.size();

}



@Override

public Object getItem(int position) {

return position;

}



@Override

public long getItemId(int position) {

return position;

}



@Override

public View getView(int position, View convertView, ViewGroup parent) {

if(convertView == null) {

convertView = mInflater.inflate(R.layout.multiple_checkbox_main_row, null);

}

TextView tN = (TextView) convertView.findViewById(R.id.multiple_title);

tN.setText((String)mList.get(position).get(NAME));



TextView tP = (TextView) convertView.findViewById(R.id.multiple_summary);

tP.setText((String)mList.get(position).get(PHONE_NUMBER));



CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.multiple_checkbox);



checkBox.setChecked(map.get(position));



return convertView;

}



}



}
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2025-1-11 08:12 , Processed in 0.304465 second(s), 36 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表