标签:
方法一:
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); } }
错误:
08-24 11:48:52.402: E/AndroidRuntime(15786): 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); 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); mEditText.setText(null); } return false; } }); } }
标签:
原文地址:http://www.cnblogs.com/laopo/p/5802220.html