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 NDK学习(1) 简介


下一篇:【自然框架 免费视频】资源角色的思路介绍(整理了一下以前帖子的目录,请刷新)