试着在做listview实例代码如下:
main.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ListView
android:id="@+id/myslist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
listfile.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="TextView01" android:layout_height="wrap_content" android:textSize="20dip" android:layout_width="fill_parent" android:id="@+id/username" /><TextView android:text="TextView02" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/userid" /></LinearLayout>listview.javapackage listview.pack;import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.widget.ArrayAdapter;public class listview extends Activity {
/** Called when the activity is first created. */
private ListView LV;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = (ListView) findViewById(R.id.myslist); ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>(); HashMap <String,Object> item1=new HashMap <String,Object>();
item1.put("ID","1");
item1.put("name","one");
HashMap <String,Object> item2=new HashMap <String,Object>();
item2.put("id","2");
item2.put("name","two");
listItem.add(item1);
listItem.add(item2);
LV = new ListView(this);
SimpleAdapter listAdapter = new SimpleAdapter(this,listItem,R.layout.listfile,new String[] {"userid","username"},new int[] {R.id.userid,R.id.username } );
LV.setAdapter(listAdapter);
//setContentView(LV);
}
}运行后没有报错,但也没有显示listview信息。不知道为什么。
main.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ListView
android:id="@+id/myslist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
listfile.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="TextView01" android:layout_height="wrap_content" android:textSize="20dip" android:layout_width="fill_parent" android:id="@+id/username" /><TextView android:text="TextView02" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/userid" /></LinearLayout>listview.javapackage listview.pack;import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.widget.ArrayAdapter;public class listview extends Activity {
/** Called when the activity is first created. */
private ListView LV;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = (ListView) findViewById(R.id.myslist); ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>(); HashMap <String,Object> item1=new HashMap <String,Object>();
item1.put("ID","1");
item1.put("name","one");
HashMap <String,Object> item2=new HashMap <String,Object>();
item2.put("id","2");
item2.put("name","two");
listItem.add(item1);
listItem.add(item2);
LV = new ListView(this);
SimpleAdapter listAdapter = new SimpleAdapter(this,listItem,R.layout.listfile,new String[] {"userid","username"},new int[] {R.id.userid,R.id.username } );
LV.setAdapter(listAdapter);
//setContentView(LV);
}
}运行后没有报错,但也没有显示listview信息。不知道为什么。
解决方案 »
- nor current process has android.permission.WRITE_APN_SETTINGS崩溃问题 android4.0系统问题
- android 广播与服务问题
- android的socket效率出奇的低
- 求教一个布局问题
- 请问下android 系统下怎么mount/unmount SD
- 如何识别事件源?
- 请教一下,如何在Android里面实现该SQLite语句
- 这两句话是什么意思呢?为什么会有这些信息呢?
- com.android.ddmlib.SyncException: Not a directory
- android串口无法通信
- Android 视频采集的问题
- android怎么使用rtp协议接收图片?
......
LV = new ListView(this);
楼主太马虎了。等散分。来自android.growprogress.com
package listview.pack;import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap; public class listview extends Activity {
/** Called when the activity is first created. */
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = (ListView) findViewById(R.id.myslist); ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>(); HashMap <String,Object> item1=new HashMap <String,Object>();
item1.put("userid","1");
item1.put("username","one");
HashMap <String,Object> item2=new HashMap <String,Object>();
item2.put("userid","2");
item2.put("username","two");
listItem.add(item1);
listItem.add(item2);
SimpleAdapter listAdapter = new SimpleAdapter(this,listItem,R.layout.listfile,new String[] {"userid","username"},new int[] {R.id.userid,R.id.username } );
list.setAdapter(listAdapter);
//setContentView(LV);
}
}
谢谢!
item1.put("username","one");
HashMap <String,Object> item2=new HashMap <String,Object>();
item2.put("userid","2");
item2.put("username","two");
listItem.add(item1);
listItem.add(item2);
// LV = new ListView(this);
SimpleAdapter listAdapter = new SimpleAdapter(this,listItem,R.layout.listfile,new String[] {"userid","username"},new int[] {R.id.userid,R.id.username } );
list.setAdapter(listAdapter);