试着在做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信息。不知道为什么。

解决方案 »

  1.   

     ListView list = (ListView) findViewById(R.id.myslist);
    ......
     LV = new ListView(this);
    楼主太马虎了。等散分。来自android.growprogress.com
      

  2.   

    修改后的java文件
    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);  
        }
    }
    谢谢!
      

  3.   

    键也错了        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);
             // 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);