Android开发 ListView的用法
来源:互联网 发布:sql case语句用法实例 编辑:程序博客网 时间:2024/06/06 01:01
首先说一下在其中遇到的的一个巨大的Bug.
由于没有在意ListView的id的设置导致程序出错。
对初学者来说我的理解是:ListView不像其他控件那样必须进行findviewbyid操作,因为ListView有默认的id那就是android:list
详见下面的代码
xml1
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.genius.listview.MainActivity"><LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"><ListView android:id="@+id/android:list" <!--就是这里对id 的设置--> android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical" android:drawSelectorOnTop="false"> <!--scrollobars是ListView的数量超过屏幕大小后的填充方式--></ListView></LinearLayout></LinearLayout>
xml2
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"><TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/username" android:layout_gravity="left"/><TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/phone" android:layout_gravity="right"/></LinearLayout>
MainActivity
package com.example.genius.listview;import android.app.ListActivity;import android.os.Bundle;import android.view.View;import android.widget.ListView;import android.widget.SimpleAdapter;import java.util.ArrayList;import java.util.HashMap;public class MainActivity extends ListActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayList<HashMap<String, String>> list = new ArrayList<>(); HashMap<String, String> map1 = new HashMap<>(); HashMap<String, String> map2 = new HashMap<>(); HashMap<String, String> map3 = new HashMap<>(); map1.put("username", "Tom"); map1.put("phone", "1105221"); map2.put("username", "kite"); map2.put("phone", "555451"); map3.put("username", "Pig"); map3.put("phone", "751521"); list.add(map1); list.add(map2); list.add(map3); SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.user, new String[]{"username", "phone"}, new int[]{R.id.username, R.id.phone}); setListAdapter(simpleAdapter);} @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); //you can do something you want to do here . }}
0 0
- Android开发 ListView的用法
- Android ListView的用法
- android listview的用法
- Android开发进阶系列(六) ListView的基本用法
- android 中ListView的用法
- android listview的用法详解
- Android中ListView的用法
- Android ListView的常见用法
- android 中listview 的用法
- Android中ListView的用法
- android-ListView的简单用法
- Android ListView 的基本用法
- xamarin android listview的用法
- android开发中几种关于ListView用法总结
- android开发之ListView基本用法
- Android开发系列(十四):ListView用法、对ListView监听的三种方法以及ListView中Adapter的使用方法
- Android开发--ListView的操作
- Android开发-ListView的使用
- c++11多线程编程之condition_variable
- Unity用UGUI做虚拟摇杆
- 《Head first设计模式》学习笔记 – 抽象工厂模式
- 六大原则-23种模式速记
- javascript笔记整理系列
- Android开发 ListView的用法
- Spark Streaming基础原理
- Singleton Pattern
- Uva 725 Division(简单枚举)
- 内置对象(转,侵立删)
- 【九度OJ】题目1183:守形数 解题报告
- 无sudo权限下安装Python-package
- 深入理解ThreadLocal
- Tensorflow | Sequences | 常用函数介绍