Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android的一些組件學習

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.simple_list_item_1, 
                COUNTRIES));*/  
          
        //方法二,使用XML布局文件  
        /*setContentView(R.layout.activity_user_list); 
        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_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>  

 


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved