Android Listview
方法一:
xml文件
<LinearLayout 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:orientation="vertical"
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=".MainActivity" > <ListView
android:id="@+id/listView1"
android:choiceMode="singleChoice"
android:layout_width="match_parent"
android:layout_height="318dp"
android:layout_weight="0.73"
android:entries="@array/spinner" >
</ListView> <EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine" > <requestFocus />
</EditText> </LinearLayout>
java 文件
package com.example.listview; import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class MainActivity extends Activity {
ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showListview();
} private void showListview()
{
mListView=(ListView) findViewById(R.id.listView1);
String[] COUNTRIES = new String[] { "Angola ", "Afghanis", "A,lbania", ",lgeria", ",ndorra", ",nguilla", "Ant,igua ", "Argen,tin", "Armenia,", "Ascens,o", "Australi", "Austria" ,"Azerbaij" ,"Bahamas", "B,ahrain", ",anglade", ",arbados", "Bel,arus", "Belgi,um", "Beli,e ", "Beni, ", "Bermuda ,", "Bolivia" , "Botswana" ,"Brazil " ,"Brunei ", "B,ulgaria", "Bur,kina-", "Burma, ", "Burundi", "Cameroon", "Canada " ,"Cayman I" ,"Central ", "C,had ", "Chile, ", "China ", "Colombia" ,"Congo ", ",Cook Is.", "C,osta Ri", "Cu,ba ", "Cyprus, ", "Czech , ", "Denmark" , "Djibouti" ,"Dominica" ,"Ecuador" , "E,gypt ", "EI S,alva", "Est,nia" , "Ethiop,ia", "Fiji ", "Finland" , "France " ,"French G", "G,abon ", "G,mbia ", "Geor,gia", "Germ,any" , "Gha,a ", "Gibralt,a", "Greece ", "Grenada", "Guam ", "Gu,atemal", "G,inea ", "G,yana ", "Hai,ti ", "Hondur,as", "Hongkong,", "Hungary", "Iceland", "India " ,"Indonesi", "I,ran ", "Iraq ", "Ireland", "Israel ", "Italy ", "I,vory Co", ",amaica" , ",apan ", "Jor,dan ", "Kampu,che", "Kazak,sta", "Kenya, ", "Korea, ", "Kuwait ", "Kyrgyzst", "Laos ", "La,tvia ", "L,banon" , "Les,otho" , "Liber,ia" , "Libya ,", "Liechten", "Lithuani" };
//ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, COUNTRIES);
ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(this, android.R.layout.select_dialog_singlechoice, COUNTRIES);
mListView.setAdapter(arrayAdapter); } }
======================================
方法二:
/**
* @Title listviewDirect.java
* @package com.example.listview
* @since
* @version 1.0.0
* @author Vic Lee
* @date Aug 24, 2016-11:15:35 AM
*/
package com.example.listview; import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class listviewDirectActivity extends ListActivity {
ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
showlistviewDirect(); } private void showlistviewDirect()
{
//mListView=getListView();
String[] COUNTRIES = new String[] { "Angola ", "Afghanis", "A,lbania", ",lgeria", ",ndorra", ",nguilla", "Ant,igua ", "Argen,tin", "Armenia,", "Ascens,o", "Australi", "Austria" ,"Azerbaij" ,"Bahamas", "B,ahrain", ",anglade", ",arbados", "Bel,arus", "Belgi,um", "Beli,e ", "Beni, ", "Bermuda ,", "Bolivia" , "Botswana" ,"Brazil " ,"Brunei ", "B,ulgaria", "Bur,kina-", "Burma, ", "Burundi", "Cameroon", "Canada " ,"Cayman I" ,"Central ", "C,had ", "Chile, ", "China ", "Colombia" ,"Congo ", ",Cook Is.", "C,osta Ri", "Cu,ba ", "Cyprus, ", "Czech , ", "Denmark" , "Djibouti" ,"Dominica" ,"Ecuador" , "E,gypt ", "EI S,alva", "Est,nia" , "Ethiop,ia", "Fiji ", "Finland" , "France " ,"French G", "G,abon ", "G,mbia ", "Geor,gia", "Germ,any" , "Gha,a ", "Gibralt,a", "Greece ", "Grenada", "Guam ", "Gu,atemal", "G,inea ", "G,yana ", "Hai,ti ", "Hondur,as", "Hongkong,", "Hungary", "Iceland", "India " ,"Indonesi", "I,ran ", "Iraq ", "Ireland", "Israel ", "Italy ", "I,vory Co", ",amaica" , ",apan ", "Jor,dan ", "Kampu,che", "Kazak,sta", "Kenya, ", "Korea, ", "Kuwait ", "Kyrgyzst", "Laos ", "La,tvia ", "L,banon" , "Les,otho" , "Liber,ia" , "Libya ,", "Liechten", "Lithuani" };
ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(this, android.R.layout.select_dialog_singlechoice, COUNTRIES);
setListAdapter(arrayAdapter);
} }
错误:
- ::52.402: E/AndroidRuntime(): java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example.listview/com.example.listview.listviewDirectActivity}: java.lang.NullPointerException 解决办法:
因为listview 自动已经生成了id ,所以引用已经生成的id即可 在xml 页面。android:id="@android:id/list"
listview 增加
<LinearLayout 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:orientation="vertical"
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=".MainActivity" > <ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="186dp"
android:layout_weight="0.87" >
</ListView> <EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine" > <requestFocus />
</EditText> </LinearLayout>
package com.example.listviw1; import java.util.ArrayList;
import java.util.List; import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText; public class MainActivity extends ListActivity {
EditText mEditText;
ArrayAdapter<String> arrayAdapter;
List<String> mlistStrings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showListview1();
}
private void showListview1()
{
mlistStrings=new ArrayList<String>();
arrayAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mlistStrings);
mEditText=(EditText) findViewById(R.id.editText1);
setListAdapter(arrayAdapter);
mEditText.setOnKeyListener(new OnKeyListener() { @Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_ENTER&& event.getAction()==KeyEvent.ACTION_UP)
{
mlistStrings.add(mEditText.getText().toString()); // setListAdapter(arrayAdapter);//慢
arrayAdapter.notifyDataSetChanged();//快
mEditText.setText(null);
} return false; }
});
}
}
删除列表
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
mlistStrings.remove(position);
arrayAdapter.notifyDataSetChanged();
}
CursorList
/**
* @Title Cursor_list.java
* @package com.example.listviw1
* @since
* @version 1.0.0
* @author Vic Lee
* @date Aug 24, 2016-1:53:16 PM
*/
package com.example.listviw1; import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.Phones;
import android.widget.SimpleCursorAdapter; public class Cursor_list extends ListActivity { @Override
protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.item_list);
showCursorlist();
}
private void showCursorlist()
{
//1.数据集
Cursor cursor=getContentResolver().query(Phones.CONTENT_URI, null, null, null, null);
//2.adapter
SimpleCursorAdapter simpleCursorAdapter=new SimpleCursorAdapter(this, R.layout.item_list, cursor,
new String[]{Phones.NAME,Phones.NUMBER},
new int[]{R.id.textView1,R.id.textView2}); //3.绑定
setListAdapter(simpleCursorAdapter);
} }
<?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:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" /> <TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" /> </LinearLayout>
模拟器没有数据 显示。。。
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
===============
MyExpandableListViewActivity
/**
* @Title MyExpandableListViewActivity.java
* @package com.example.listviw1
* @since
* @version 1.0.0
* @author Vic Lee
* @date Aug 24, 2016-2:48:54 PM
*/
package com.example.listviw1; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter; public class MyExpandableListViewActivity extends ExpandableListActivity
{ @Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) { // TODO Auto-generated method stub
return super.onChildClick(parent, v, groupPosition, childPosition, id); } @Override
public void onGroupCollapse(int groupPosition) { // TODO Auto-generated method stub
super.onGroupCollapse(groupPosition); } @Override
public void onGroupExpand(int groupPosition) { // TODO Auto-generated method stub
super.onGroupExpand(groupPosition); } @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
//1.//build data source
List<HashMap<String, String>> groupData=new ArrayList<HashMap<String,String>>();
HashMap<String, String> _map=new HashMap<String, String>();
_map.put("Name", "LD1");
groupData.add(_map);
_map=new HashMap<String, String>();
_map.put("Name", "LD2");
groupData.add(_map);
_map=new HashMap<String, String>();
_map.put("Name", "changsha");
groupData.add(_map);
_map=new HashMap<String, String>(); List<List<HashMap<String, String>>> childData=new ArrayList<List<HashMap<String,String>>>();
List<HashMap<String, String>> childItem=new ArrayList<HashMap<String,String>>();
HashMap<String, String> _mapchild=new HashMap<String, String>();
_mapchild.put("Name", "LD11");
_mapchild.put("Name", "LD22");
_mapchild.put("Adrress", "changsha1");
childItem.add(_mapchild);
childData.add(childItem);
childData.add(childItem);
childData.add(childItem); //2.build data source and bind data source
ExpandableListAdapter expandableListAdapter
=new SimpleExpandableListAdapter(this,
groupData,
android.R.layout.simple_expandable_list_item_1,
android.R.layout.simple_expandable_list_item_1,
new String[]{"Name","Adrress"},
new int[]{android.R.id.text1,android.R.id.text2},
childData,
android.R.layout.simple_expandable_list_item_2,
new String[]{"Name","Adrress"},
new int[]{android.R.id.text1,android.R.id.text2});
//3.bind UI
setListAdapter(expandableListAdapter);
} }
tab
/**
* @Title MyTabActivity.java
* @package com.example.listviw1
* @since
* @version 1.0.0
* @author Vic Lee
* @date Aug 24, 2016-6:41:38 PM
*/
package com.example.listviw1; import android.R.anim;
import android.R.drawable;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec; @SuppressWarnings("deprecation")
public class MyTabActivity extends TabActivity { @Override
protected void onChildTitleChanged(Activity childActivity,
CharSequence title) { // TODO Auto-generated method stub
super.onChildTitleChanged(childActivity, title); } @SuppressWarnings("deprecation")
@Override
public void onContentChanged() { // TODO Auto-generated method stub
super.onContentChanged(); } @SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
Drawable drawable1 =getResources().getDrawable(android.R.drawable.ic_dialog_email);
Drawable drawable2 =getResources().getDrawable(android.R.drawable.ic_dialog_map);
Drawable drawable3 =getResources().getDrawable(android.R.drawable.ic_dialog_info); TabHost tabHost=getTabHost();
TabSpec tabSpec1=tabHost.newTabSpec("Tab1").setIndicator("",drawable1)
.setContent(new Intent(this, MyExpandableListViewActivity.class));
tabHost.addTab(tabSpec1); TabSpec tabSpec2=tabHost.newTabSpec("Tab2").setIndicator("",drawable2)
.setContent(new Intent(this, Cursor_list.class));
tabHost.addTab(tabSpec2); TabSpec tabSpec3=tabHost.newTabSpec("Tab3").setIndicator("",drawable3)
.setContent(new Intent(this, MySlidingDrawerActivity.class));
tabHost.addTab(tabSpec3); } }
Android Listview的更多相关文章
- android ListView 九大重要属性详细分析、
android ListView 九大重要属性详细分析. 1.android ListView 一些重要属性详解,兄弟朋友可以参考一下. 首先是stackFromBottom属性,这只该属性之后你做好 ...
- Android ListView onItemClick Not Work
Android ListView onItemClick Not Work ListView item中有Button和RadioButton的时候,它的Item点击事件不起作用,需要设置item的属 ...
- 【腾讯Bugly干货分享】Android ListView与RecyclerView对比浅析--缓存机制
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/5811d3e3ab10c62013697408 作者:黄宁源 一,背景 Recy ...
- Android ListView 常用技巧
Android ListView 常用技巧 Android TextView 常用技巧 1.使用ViewHolder提高效率 ViewHolder模式充分利用了ListView的视图缓存机制,避免了每 ...
- Android listview addHeaderView 和 addFooterView 详解
addHeaderView()方法:主要是向listView的头部添加布局addFooterView()方法:主要是向listView的底部添加布局 需要注意的是添加布局的时候应该添加从父容器开始添加 ...
- Android ListView滑动过程中图片显示重复错乱闪烁问题解决
最新内容建议直接访问原文:Android ListView滑动过程中图片显示重复错乱闪烁问题解决 主要分析Android ListView滚动过程中图片显示重复.错乱.闪烁的原因及解决方法,顺带提及L ...
- Android --ListView分页
参考博客:Android ListView分页加载(服务端+android端)Demo 监听OnScrollListener事件 class OnListScrollListener implemen ...
- Android ListView ListActivity PreferenceActivity背景变黑的问题ZT
Android ListView ListActivity PreferenceActivity背景变黑的问题 ListView在滚动时背景会变暗甚至变黑,这个要从Listview的效果说起,默认的L ...
- android listview去掉分割线
1:android listview去掉分割线 1>设置android:divider="@null" 2>android:divider="#0000000 ...
- 【转】android ListView 几个重要属性
android ListView 几个重要属性 分类: Android2012-03-08 19:25 19324人阅读 评论(5) 收藏 举报 listviewandroid活动javalistnu ...
随机推荐
- 学习笔记---C/C++语法
一.char *s char s[] 区别 char *s中的s是指针,而指针是指向一块内存区域,它指向的内存区域的大小可以随时改变,而且当指针指向常量字符串时,它的内容是不可以被修改的,否则在运行时 ...
- WP8.1 和UWP 如何使用下载网页的上的音频 并保存
WP8.1: private async Task<StorageFile> GetVoiceData() { HttpClient httpclient = new HttpClient ...
- xshell的快捷命令
xshell的快捷命令 reconnect Type `help' to learn how to use Xshell prompt.[c:\~]$ ?Internal Commands:new: ...
- 后移动互联网时代:到底还要不要开发一个App?
后移动互联网时代,到底是什么样的一个时代? 首先,后移动互联网时代中,产生头部应用的几率变小了,像微信这样巨头式的App很难在产生第二个.其次,后移动互联网时代,物联网发展迅速,所有的智能硬件都需要一 ...
- sed处理url编码解码=== web日志的url处理
URL 编码/解码方法(linux shell实现),方法如下: 1.编码的两种方法: admin@~ 11:14:29>echo '手机' | tr -d '\n' | xxd -plain ...
- C# Console控制命令
参考博客:C# 控制台程序(命令行程序)设置字体颜色,窗口宽高,光标行数 禁用控制台关闭按钮,参考博客:禁用C#控制台应用程序的关闭按钮 参考博客:c# 控制台程序禁用关闭按钮完美解决 #region ...
- APP开发:对于IOS APP应用的推广渠道有哪些?
亿合科技了解到,随着移动互联网时代的到来,苹果从2007年进入中国市场,一直备受大众喜爱,手机应用也层出不穷.那么对于那么多的IOS APP应用怎么能获得用户的喜爱呢?于是推广APP应用是需要做的,亿 ...
- python爬虫感想
老师说,能用程序解决一个实际问题,说明已经会使用工具了.今天用python做了一个小爬虫,很幸运,成功了. 有几个难点:就是学会伪装,还有一个觉得打开的方式太多,有点糊涂,正则表达式也要加强了.
- 纯html、css3、js的时钟
之前在网上看了一些使用js写的时钟,但感觉实现的方法有点麻烦,所以就自己重新写了一个例子,样子有点丑,但方法比较简单,大家就凑合看吧 其中采用的主要方法是原生js里面的Data(时期)对象,以及它的. ...
- Windows Phone 六、JSON序列化
JSON序列化 public class Person { public int Id { get; set; } public string Name { get; set; } public in ...