码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-04-09 18:37:15      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

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();
            }
        });

    }
}

效果图:

技术分享

 

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

标签:

原文地址:http://www.cnblogs.com/0927wyj/p/5371998.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!