新手Android开发之三:listview修改选中项中的textview

相信很多初学者都会遇到Listview的使用问题,下面就给出两种改变listview选中项中的textview来向大家解释一下Listview的使用方法。

1. 使用simpleadapter填充Listview

package dickren123.hui.say_hello_to_world;

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

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Hello_to_worldActivity extends ListActivity  {//继承listactivity类,与activity类相似
    /** Called when the activity is first created. */
	private SimpleAdapter myadapter;//定义全局变量SimpleAdapter,用来填充listview
	List<Map<String, Object>> mylist;//定义全局数组mylist,用来连接adapter
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mylist = getmydata();//获取初始化数据
        myadapter = new SimpleAdapter(this,mylist,R.layout.main,
        									new String[]{"textView1","textView2"},
        									new int[]{R.id.textView1,R.id.textView2});
        //定义myadapter,构造函数中有五个参数,第一个为context对象,用this传入,第二个为填充的数据
        //第三个为adapter的布局文件xml,第四个为填充数据的string对象,第五个为对应的id       
        setListAdapter(myadapter);//将适配器连接到listview中
        
        
    }	

	private List<Map<String, Object>> getmydata()//存入数据
    {
    	mylist = new ArrayList<Map<String, Object>>();
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("textView1","yes");
    	map.put("textView2","health");
    	mylist.add(map);
    	
    	map = new HashMap<String, Object>();
    	map.put("textView1","yesyes");
    	map.put("textView2","health health");
    	mylist.add(map);   
    	
    	map = new HashMap<String, Object>();
    	map.put("textView1","yesyesyes");
    	map.put("textView2","health health health");
    	mylist.add(map);  
    	
    	return mylist;
    }
	
	@Override
	 protected void onListItemClick(ListView l, View v, int position, long id)
	 {//选中事件,获取对应项的参数
		super.onListItemClick(l, v, position, id);
		changeitem(position);
	 }
	private void changeitem(int position)
	{//改变对应项的文字
		HashMap<String, Object> map = new HashMap<String, Object>();
		switch (position)
		{
			case 0 : 		map.put("textView1","yes");
						map.put("textView2", "forever1");
						break;
			case 1 : 		map.put("textView1","yesyes");
						map.put("textView2", "forever2");
						break;
			case 2 : 		map.put("textView1","yesyesyes");
						map.put("textView2", "forever3");
						break;			
			default:		break;	
		}
		mylist.remove(position);//移除选中项
		mylist.add(position,map);//在选中位置添加新项
		myadapter.notifyDataSetChanged();//改变adapter
	}
	
	
}
2. 自定义adapter,调用类中定义的新方法

package dickren123.hui.say_hello_to_world;

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

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class Hello_to_worldActivity extends ListActivity  {//继承listactivity类,与activity类相似
    /** Called when the activity is first created. */
	private MyAdapter myadapter;//自定义全局变量MyAdapter,用来填充listview
	List<Map<String, Object>> mylist;//定义全局数组mylist,用来连接adapter
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mylist = getmydata();//获取初始化数据
        myadapter = new MyAdapter(this);//构造函数只写了一个context见下面类的定义    
        setListAdapter(myadapter);//将适配器连接到listview中
    }	

	private List<Map<String, Object>> getmydata()//存入数据
    {
    	mylist = new ArrayList<Map<String, Object>>();
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("textView1","yes");
    	map.put("textView2","health");
    	mylist.add(map);
    	
    	map = new HashMap<String, Object>();
    	map.put("textView1","yesyes");
    	map.put("textView2","health health");
    	mylist.add(map);   
    	
    	map = new HashMap<String, Object>();
    	map.put("textView1","yesyesyes");
    	map.put("textView2","health health health");
    	mylist.add(map);  
    	
    	return mylist;
    }
	
	@Override
	 protected void onListItemClick(ListView l, View v, int position, long id)
	 {//选中事件,获取对应项的参数
		super.onListItemClick(l, v, position, id);
		myadapter.setText2(position);//调用新类中的方法
	 }
	
	public class MyAdapter extends BaseAdapter
	{
		 private LayoutInflater mInflater;//类似于 findViewById() 用于找到layout文件夹下的xml布局文件,并且实例化
		 public MyAdapter(Context context){//新类的构造函数,只需从原context中传入layout即可
	            this.mInflater = LayoutInflater.from(context);
	        }
		 @Override//返回当前listView的长度
	        public int getCount() {
	            // TODO Auto-generated method stub
	            return mylist.size();
	        }
		 @Override//返回项
	        public Object getItem(int arg0) {
	            // TODO Auto-generated method stub
	            return null;
	        }
		 @Override//返回id
	        public long getItemId(int arg0) {
	            // TODO Auto-generated method stub
	            return 0;
	        }
		 
		 public View getView(int position, View convertView, ViewGroup parent) //重要!每次绘制时都会调用!
		 { 
	        LinearLayout linearLayout = (LinearLayout)mInflater.inflate(R.layout.main,null);
	        TextView text1 = (TextView)linearLayout.findViewById(R.id.textView1);
	        TextView text2 = (TextView)linearLayout.findViewById(R.id.textView2);
	        text1.setText((String)mylist.get(position).get("textView1"));//将适配器中的数据传入,重要
	        text2.setText((String)mylist.get(position).get("textView2"));
	        return linearLayout;
		 }
		 
		 public void setText2(int position)//定义新方法,更新适配器中的数据
		 {
			 HashMap<String, Object> map_temp = new HashMap<String, Object>();
			 switch (position)
				{
					case 0 : 		map_temp.put("textView1", "yes");
								map_temp.put("textView2", "forever1");
								break;
					case 1 : 		map_temp.put("textView1", "yesyes");
								map_temp.put("textView2", "forever2");
								break;
					case 2 : 		map_temp.put("textView1", "yesyesyes");
								map_temp.put("textView2", "forever3");
								break;			
					default:		break;	
				}
			 mylist.set(position , map_temp);//将数据更新
			 myadapter.notifyDataSetChanged();//适配器更新
		 }
	}
	
	
}
希望大家多多交流!^^



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值