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

Android控件之ListView

  ListView是Android中最常用的控件之一. ListView中一个重要的概念就是适配器(Adapter),它是控件与数据源之间的桥梁.

常见的数据源有数组/集合(Array/List),游标(Cursor).

        1,  从数组中获取数据:

         运行结果:


        2,  从Cursor中获取数据:
[html] 
// Get a cursor with all people 
Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null); 
 
startManagingCursor(c);//使用的cursor的生命周期随Activity生命周期变化而变化. 
 
ListAdapter adapter = new SimpleCursorAdapter(this,  
        // Use a template that displays a text view 
        android.R.layout.易做图_list_item_1,  
        // Give the cursor to the list adatper 
        c,  
        // Map the NAME column in the people database to... 
        new String[] {People.NAME} , 
        // The "text1" view defined in the XML template 
        new int[] {android.R.id.text1});  
setListAdapter(adapter); 

运行结果:


            3, 使用自定义Adapter

[java] 
private class SpeechListAdapter extends BaseAdapter { 
        public SpeechListAdapter(Context context) { 
            mContext = context; 
        } 
 
        public int getCount() { 
            return mTitles.length; 
        } 
 
        public Object getItem(int position) {//每个条目对应的对象 
            return position; 
        } 
 
        public long getItemId(int position) {//每个条目的id 
            return position; 
        } 
 
        public View getView(int position, View convertView, ViewGroup parent) {//ListView每个item对应的view 
            SpeechView sv; 
            if (convertView == null) { 
                sv = new SpeechView(mContext, mTitles[position], 
                        mDialogue[position]); 
            } else { 
                sv = (SpeechView) convertView; 
                sv.setTitle(mTitles[position]); 
                sv.setDialogue(mDialogue[position]); 
            } 
 
            return sv; 
        } 

       结果:


一些ListView效果

                        1,  带有分隔符的ListView.
效果所下图所示(红框表示的是分隔条):

[html] 
private class MyListAdapter extends BaseAdapter { 
    public MyListAdapter(Context context) { 
        mContext = context; 
    } 
 
    public int getCount() { 
        return mStrings.length; 
    } 
 
    @Override 
    public boolean areAllItemsEnabled() {//是否所有的item都可点击和选中 
        return false; 
    } 
 
    @Override 
    public boolean isEnabled(int position) {//如果字符串是以"-"开头,那么对应的item为分隔item 
        return !mStrings[position].startsWith("-"); 
    } 
     
     
 
    public Object getItem(int position) { 
        return position; 
    } 
 
    public long getItemId(int position) { 
        return position; 
    } 
 
    public View getView(int position, View convertView, ViewGroup parent) { 
        TextView tv; 
        if (convertView == null) { 
            tv = (TextView) LayoutInflater.from(mContext).inflate( 
                    android.R.layout.易做图_expandable_list_item_1, parent, false); 
        } else { 
            tv = (TextView) convertView; 
        } 
        tv.setText(mStrings[position]); 
        return tv; 
    } 
 
    private Context mContext; 

                          2, 带收缩和展开效果的ListView
效果如下图所示:


             
                3 ,类似带悬浮字母效果的ListView(类似通讯录导航的效果 )        
如下图所示

 

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,