亲宝软件园·资讯

展开

ListView下拉列表控件

linkinparkzlz 人气:0

ListView列表控件

列表的显示需要三个元素:

view 用来显示数据的view
适配器 用来把数据映射到view的中介
数据 具体的将被映射的字符串,图片,或者基本组件

案例代码

布局文件代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     >

    <ListView 
        android:id="@+id/lv_list"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
        />


</LinearLayout>

activity代码

package rr.yy;

import android.app.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListViewDemo extends Activity {
   private ListView lv_list;
    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lv_list=(ListView)findViewById(R.id.lv_list);
        final ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData());
        lv_list.setAdapter(adapter);
        lv_list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                String txt =adapter.getItem(arg2);
                Toast.makeText(ListViewDemo.this, txt, 0).show();
            }
        });

    }   
    private String[] getData() {
        // TODO Auto-generated method stub
        return new String[]{"lin","kin","park","好好","先生","上学","但是","得分","读书","克扣","奋斗"};
    }
}

加载全部内容

相关教程
猜你喜欢
用户评论