首页 > 代码库 > 07_ListView使用1

07_ListView使用1

  ListView是用来显示一个列表的控件,它在Android源代码中是继承AbsListView类的,AbsListView类是继承于AdapterView类的,而AdapterView类是继承ViewGroup类的。

 

  下面程序的功能:创建ListView,点击Item会显示全部信息。

package com.example.listviewdemo;import android.support.v7.app.ActionBarActivity;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 MainActivity extends ActionBarActivity implements OnItemClickListener {    private ListView lv;    private ArrayAdapter<ListCellData> adapter;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                adapter = new ArrayAdapter<ListCellData>(this, android.R.layout.simple_expandable_list_item_1);//        adapter = new ArrayAdapter<String>(this, R.layout.lsit_cell);                lv = (ListView) findViewById(R.id.listView1);        lv.setAdapter(adapter);                adapter.add(new ListCellData("张三", "男", 45));        adapter.add(new ListCellData("李四", "男", 22));        adapter.add(new ListCellData("王五", "女", 33));                lv.setOnItemClickListener(this);        //        adapter.add("Hello");//        adapter.add("What?");    }    @Override    public void onItemClick(AdapterView<?> parent, View view, int position,            long id) {        ListCellData data = adapter.getItem(position);                Toast.makeText(this, String.format("名字: %s, 性别: %s, 年龄: %d", data.getUserName(), data.getSex(), data.getAge()), Toast.LENGTH_SHORT).show();            }}

 

 

07_ListView使用1