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 ...
随机推荐
- Selenium2学习-039-WebUI自动化实战实例-文件上传下载
通常在 WebUI 自动化测试过程中必然会涉及到文件上传的自动化测试需求,而开发在进行相应的技术实现是不同的,粗略可划分为两类:input标签类(类型为file)和非input标签类(例如:div.a ...
- placeholder文字颜色与是否显示兼容性
1.ie显示问题 <script type="text/javascript"> $(document).ready(function(){ var doc=docum ...
- 阿里云docker镜像加速
echo "DOCKER_OPTS=\"--registry-mirror=https://yeyieqis.mirror.aliyuncs.com\"" | ...
- 调试一个socket通信bug的心理过程和反思
背景交代.最近在玩lua的服务端编码, 有项目A,B,AB都是同一个模子的.我手上有A的winsocket客户端和服务端的代码,B项目早期的一份linux下的lua client.服务端.客户端都是L ...
- post- build event
Ref:http://blog.csdn.net/teng_ontheway/article/details/8307410 Ref: http://blog.csdn.net/sodickbird/ ...
- front-end plugin, generate pdf with html5 and jquery
http://www.jqcool.net/jquery-jspdf.html[from this site] <html> <head></head> <s ...
- 所有的cursor图标
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 我的第二个FluentNHibernate例子with Knockout
在上一篇我的第一个FluentNHibernate例子的基础上,我们用上knockoutjs 1用nuget添加knockoutjs包 2用nuget添加json.net包 3..在Index.csh ...
- PHP 返回JSON
有个朋友说PHP不能返回JSON对象,作为.net的我认为应该是可以的,设置一下header 就行了. 果不然,google 一下,备忘如下: <?php $result = array('Na ...
- CSS中隐藏内容的3种方法及属性值
CSS中隐藏内容的3种方法及属性值 (2011-02-11 13:33:59) 在制作网页时,隐藏内容也是一种比较常用的手法,它的作用一般有:隐藏文本/图片.隐藏链接.隐藏超出范围的内容.隐藏弹出 ...