Android开发 在ListView中添加控件

     在android课程项目开发过程中,我们小组需要展现一个列表,在列表中实现相对应栏目的编辑等功能,这也就出现了在列表中添加控件的问题。众所周知,android开发中的列表使用ListView实现的,在ListView中针对每个选项添加控件,通过查找相关资料,我找到一种比较通用的办法,介绍如下:

    首先,需要为列表控件新写一个xml布局文件,在这个布局文件中定义列表中一行需要展现的内容,比如每一列中添加TextView,Button等控件;

    接下来,在主界面xml布局文件中定义ListView控件;

    最后在activity文件中定义相关操作,这里的主要思路为:首先编写一个final类ViewHolder,在这个类中定义列表每一行将要实现的控件;然后定义MyAdapter,编写子类的构造方法。同时,MyAdapter类继承基类BaseAdapter,覆写基类中的getCount()、getItem(int arg0)、getItemId(int arg0)、getView(int position, View convertView, ViewGroup parent)等方法。getCount方法返回列表选项的数目、getItem返回列表的选项、getItemId返回列表选项的id。在这几个方法中,最重要的就是最后一个getView方法,它将ViewHolder类中的控件动态加入到ListView中。

    整个代码部分如下所示:

    列表控件布局vlist.xml:




	
	
	
	


主界面list.xml:





	
	
	    
	    
	
	


动态MyGoodsActivity.java:


package com.pku.activity;

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.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MyGoodsActivity extends Activity {
	/**
	 * @查看我发布的商品
	 */
    private List> mData;    
    private ListView mListView;
    
    @Override    
    public void onCreate(Bundle savedInstanceState) {        
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.list);
    	mListView=(ListView)findViewById(R.id.lv); 
    	returnButton = (Button)findViewById(R.id.returnButton);
    	returnButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//返回上层界面(用户中心)
				Intent intent = new Intent(MyGoodsActivity.this,MyInfoActivity.class);
				startActivity(intent);
			}
		});   
    	MyAdapter adapter = new MyAdapter(this);       
    	mListView.setAdapter(adapter); 
    	mListView.setItemsCanFocus(false);
    	mListView.setOnItemClickListener(new OnItemClickListener(){                
    		@Override                
    		public void onItemClick(AdapterView parent, View view, int position, long id) { 
				//点击列表中栏目发生的事件
    			Toast.makeText(MyGoodsActivity.this, "商品描述:" + (String)mData.get(position).get("gooddescription"), Toast.LENGTH_LONG).show();               
    		}            
    	}); 
    }
    
    //初始化        
    private void init() {            
    	mData=new ArrayList>();
    	Map map = new HashMap();        
    	map.put("goodname", "G1");        
    	map.put("goodprice", "100");        
    	map.put("goodlocation", "location 1"); 
    	map.put("gooddescription", "google 1");
    	mData.add(map);         
    	map = new HashMap();        
    	map.put("goodname", "G2");        
    	map.put("goodprice", "200");        
    	map.put("goodlocation", "location 2");
    	map.put("gooddescription", "google 1");
    	mData.add(map);         
    	map = new HashMap();        
    	map.put("goodname", "G3");        
    	map.put("goodprice", "300");        
    	map.put("goodlocation", "location 3");  
    	map.put("gooddescription", "google 3");
    	mData.add(map);
    }
    
    public final class ViewHolder{        
    		public TextView goodname;               
    		public TextView goodprice;
    		public TextView goodlocation;
    		public Button editButton;  
    		public Button deleteButton;    
    		} 
    	
    public class MyAdapter extends BaseAdapter{         
    	private LayoutInflater mInflater; 
    	
    	public MyAdapter(Context context){            
    			this.mInflater = LayoutInflater.from(context);  
    			init(); 
    	}        
    		
    	@Override        
    	public int getCount() {            
    		// TODO Auto-generated method stub           
    		return mData.size();       
    	}  
    	
    	@Override           			
    	public Object getItem(int arg0) {            
    		// TODO Auto-generated method stub            
    		return null;       
    	}         
    	
    	@Override        
    	public long getItemId(int arg0) {            
    		// TODO Auto-generated method stub            
    		return 0;        
    	}         
    	
    	@Override        
    	public View getView(int position, View convertView, ViewGroup parent) {                         
    		ViewHolder holder = null;            
    		if (convertView == null) {                                 
    			holder=new ViewHolder();                                   
    			convertView = mInflater.inflate(R.layout.vlist, null);                
    			holder.goodname = (TextView)convertView.findViewById(R.id.goodname);                               
    			holder.goodprice = (TextView)convertView.findViewById(R.id.goodprice);
    			holder.goodlocation = (TextView)convertView.findViewById(R.id.goodlocation); 
    			holder.editButton = (Button)convertView.findViewById(R.id.editButton);
    			holder.deleteButton = (Button)convertView.findViewById(R.id.deleteButton);
    			convertView.setTag(holder);                             
    			}else {                                 
    				holder = (ViewHolder)convertView.getTag();            
    			}                                      
    		holder.goodname.setText((String)mData.get(position).get("goodname"));
    		holder.goodprice.setText((String)mData.get(position).get("goodprice"));            
    		holder.goodlocation.setText((String)mData.get(position).get("goodlocation"));                         
    		holder.editButton.setOnClickListener(new View.OnClickListener() {                                 
    			@Override                
    			public void onClick(View v) {                    
    				//编辑商品信息                                 
    				}            
    			});  
    		holder.deleteButton.setOnClickListener(new View.OnClickListener() {                                 
    			@Override                
    			public void onClick(View v) {                    
    				//删除商品信息                                 
    				}            
    			}); 
    		return convertView;        
    	}            
    }           
    private Button returnButton;
}



最终实现的界面如下:



你可能感兴趣的:(Android应用开发技巧,Android应用开发系列教程)