Android课程---关于ListView列表视图的学习

activity_ui3.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lv_1"
    android:divider="#0ff"
    android:dividerHeight="2dp">

</ListView>

UIActivity.java

package com.hanqi.test5;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class UIActivity3 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ui3);

        ListView lv_1 = (ListView)findViewById(R.id.lv_1);

        //设置ListView的Adapter
        //准备数据

        //String[] str_1 = {"A","B","C","D","E","F","G","H","I","J","K","L","M"};
        List<String> list_1 = new ArrayList<String>();

        list_1.add("A");
        list_1.add("B");
        list_1.add("C");
        list_1.add("D");
        list_1.add("E");
        list_1.add("F");
        list_1.add("D");
        list_1.add("H");
        //构造适配器
        ArrayAdapter aa_1 = new ArrayAdapter(this,R.layout.layout_array,list_1);  //实例化
        //设置适配器
        lv_1.setAdapter(aa_1);

        lv_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String str = parent.getItemAtPosition(position).toString();
                Toast.makeText(UIActivity3.this, "position = "+position + "id = " + id, Toast.LENGTH_SHORT).show();
            }
        });

    }
}

效果图:

 

posted @ 2016-04-09 16:45  秦萧不再  阅读(153)  评论(0编辑  收藏  举报