Android--ListView显示列表数据

简单的显示

import android.os.Bundle;

import android.app.ListActivity;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.Toast;



/**

 * 

 *简单列表展示

 *继承ListActivity

 */

public class MainActivity extends ListActivity {



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        

        //创建一个数组数据适配器

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getData());

        //设置适配器

        setListAdapter(adapter);

    }

    

    /**

     * 选中列表某项触发的事件

     */

    @Override

    protected void onListItemClick(ListView l, View v, int position, long id) {

        Toast.makeText(this, "选择了:"+getData()[position], Toast.LENGTH_SHORT).show();

    }



    /**

     * 模拟数据

     * @return

     */

    private String[] getData(){

        String[] names = {

                "Dwight D. Eisenhower",

                "John F. Kennedy",

                "Lyndon B. Johnson",

                "Richard Nixon",

                "Gerald Ford",

                "Jimmy Carter",

                "Ronald Reagan",

                "George H. W. Bush",

                "Bill Clinton",

                "George W. Bush",

                "Barack Obama"};

        return names;

    }



}

效果:

Android--ListView显示列表数据

你可能感兴趣的:(ListView)