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>
上一篇:HTTP协议是什么?(及get和post请求的区别)


下一篇:Spring Boot 系列教程15-页面国际化