andorid 列表视图 ListView 之ArrayAdapter

Posted 让心开始

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了andorid 列表视图 ListView 之ArrayAdapter相关的知识,希望对你有一定的参考价值。

activity_ui3.xml

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

</ListView>

UI3Activity.java

package com.hanqi.application3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 UI3Activity 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[] lv_str={"A","B","C","D","E","F","G","H","I","G","K","L","M","N"};

        List<String> lv_list= new ArrayList<String>();

        lv_list.add("A");
        lv_list.add("B");
        lv_list.add("C");
        lv_list.add("D");
        lv_list.add("E");
        lv_list.add("F");
        lv_list.add("G");

        //构造适配器
        ArrayAdapter aa_1 = new ArrayAdapter(this,R.layout.layout_array,lv_list);

        lv_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            // 集合parent  普通视图View view   下标position  数据Item
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String srt1= parent.getItemAtPosition(position).toString();
                Toast.makeText(UI3Activity.this, "position"+position+"srt1"+srt1, Toast.LENGTH_SHORT).show();
            }
        });
        //设置适配器

        lv_1.setAdapter(aa_1);




    }
}

layout_array.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:textSize="30sp"
    android:gravity="center_vertical"
    android:paddingLeft="10dp">

</TextView>

 

以上是关于andorid 列表视图 ListView 之ArrayAdapter的主要内容,如果未能解决你的问题,请参考以下文章

Flutter:如何知道列表视图中的项目位置?

颤振键盘隐藏嵌套列表视图中的文本字段

ListView 没有向我显示包含数据库名称的列表。我使用 SimpleCursorAdapter

Andorid ListView使用技巧

Andorid-15k+的面试题

android requestFocusFromTouch 调出菜单,突出显示第一项