当前位置:操作系统 > 安卓/Android >>

android的一些组件学习


<span style="font-size:14px">package com.example.fortest;  
  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
  
import com.zyj.util.DBHelper;  
import com.zyj.util.UserListAdapter;  
  
import android.app.Activity;  
import android.app.ListActivity;  
import android.content.Context;  
import android.database.Cursor;  
import android.os.Bundle;  
import android.view.LayoutInflater;  
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.ViewGroup;  
import android.widget.BaseAdapter;  
import android.widget.ListView;  
import android.widget.TextView;  
import android.widget.Toast;  
  
  
public class UserList extends ListActivity {  
      
     //数据数据    
    private  static final String[] COUNTRIES=new String[]{"中国","俄罗斯","英国","法国"};  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
          
        //方法一,不使用XML,类继承ListActivity  
        /*//设置一个Adapter 
        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.易做图_list_item_1, 
                COUNTRIES));*/  
          
        //方法二,使用XML布局文件  
        /*setContentView(R.layout.activity_user_list); 
        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.易做图_list_item_1, 
                COUNTRIES));*/  
          
        //方法三,自定义要展示的元素  
        ArrayList<HashMap<String, Object>> data = getData();  
        //模仿SimpleAdapter实现的自己的adapter  
        setListAdapter(new UserListAdapter(this, data));  
          
    }  
      
    //listItem单击事件  
    @Override  
    protected void onListItemClick(ListView l, View v, int position, long id) {  
        Toast.makeText(this, "您单击了",Toast.LENGTH_LONG).show();  
    }  
      
    //得到用户数据  
    private ArrayList<HashMap<String,Object>> getData()  
    {  
         ArrayList<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();  
        DBHelper dbHelper = new DBHelper(this);  
        Cursor c =  dbHelper.query();  
        if (c.moveToFirst() && c.getCount()>0) {  
            for(int i = 0;i<c.getCount();i++)  
            {  
                HashMap<String,Object> m = new HashMap<String,Object>();  
                m.put("_id", c.getString(0));  
                m.put("name", c.getString(1));  
                m.put("pwd", c.getString(2));  
                list.add(m);  
            }  
        }  
        return list;  
    }  
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.user_list, menu);  
        return true;  
    }  
  
     //元素的缓冲类,用于优化ListView    
    private static class ItemViewCache{  
        public TextView idTextView;  
        public TextView nameTextView;  
        public TextView pwdTextView;  
    }  
      
    /** 
     * 自定义用户列表listview 
     * @author zyj   * 
     */   
    public class UserListAdapter extends BaseAdapter {  
        private ArrayList<HashMap<String, Object>> data;          
        private Context context;  
        private LayoutInflater inflater;  
          
        public UserListAdapter(Context c,ArrayList<HashMap<String, Object>> d)  
        {  
            this.context = c;  
            this.data = d;    
            inflater = LayoutInflater.from(context);  
        }  
          
        //元素的个数  
        @Override  
        public int getCount() {  
            return data.size();  
        }  
  
        //获取某一个位置的数据  
        @Override  
        public Object getItem(int position) {  
            return data.get(position);  
        }  
  
        //获取唯一标识  
        @Override  
        public long getItemId(int position) {  
            return position;  
        }  
  
        //生成在ListView中展示的一个个元素View  
        @Override  
        public View getView(int position, View convertView, ViewGroup parent) {  
            ItemViewCache cache = null;  
            //优化ListView  
            if(convertView == null)  
            {  
                convertView =inflater.inflate(R.layout.activity_user_list, null);  
                cache = new ItemViewCache();  
                cache.idTextView = (TextView) convertView.findViewById(R.id.id);  
                cache.nameTextView = (TextView) convertView.findViewById(R.id.name);  
                cache.pwdTextView = (TextView) convertView.findViewById(R.id.pwd);                
                convertView.setTag(cache);  
            }  
            else {  
                cache = (ItemViewCache) convertView.getTag();  
            }  
              
            //设置文本,然后返回这个View,用于ListView的Item展示  
            cache.idTextView.setText((String)data.get(position).get("_id"));  
            cache.nameTextView.setText((String)data.get(position).get("name"));  
            cache.pwdTextView.setText((String)data.get(position).get("pwd"));  
            return convertView;  
        }  
    }  
}  
</span>  

 


补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,