使用SimpleAdapter看不到ListView

时间:2013-02-24 17:30:22

标签: android listview

我正在开发一个简单的应用程序,它由ListView和一个按钮组成。我无法看到ListView,但按钮在我的活动中可见。我不知道发生了什么。请帮帮我。

我的main.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"
android:orientation="vertical" >

<Button
    android:id="@+id/Add_Contact"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Add Contact" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    >
</ListView>

</LinearLayout>

我的MainActivity.java

公共类MainActivity扩展了Activity {

Button add_Contact;
ListView selectedList;
private ArrayList<SelectedListModel> selectedArrayList = new ArrayList<SelectedListModel>();
List<Map<String, String>> dataList;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    dataList = new ArrayList<Map<String, String>>();
    add_Contact = (Button)findViewById(R.id.Add_Contact);
    selectedList = (ListView)findViewById(R.id.listView1);

    add_Contact.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent in = new Intent(MainActivity.this,SecondActivity.class);
            //in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivityForResult(in, 1);

             }
    });

    //SimpleAdapter adpt = new SimpleAdapter(this, dataList, R.layout.simple, new String[]{"name","number"},new int[]{R.id.textView1, R.id.textView2});
    //selectedList.setAdapter(adpt);

    selectedList.setAdapter(new SimpleAdapter(this, dataList, android.R.layout.two_line_list_item, new String[] {"name", "number"}, new int[] {android.R.id.text1, android.R.id.text2}));


}



protected void onActivityResult(int requestCode, int resultCode, Intent data) {


    if (requestCode == 1) {

         if(resultCode == RESULT_OK){

          selectedArrayList = data.getParcelableArrayListExtra("result");
          System.out.println("activity result called4 :"+selectedArrayList.size());
          Iterator<SelectedListModel> it = selectedArrayList.iterator();

            while (it.hasNext()){
                SelectedListModel selectedModel = (SelectedListModel)it.next();


                    String name1 = selectedModel.getName();
                    String number1 = selectedModel.getNumber();
                    Map<String, String> dict = new HashMap<String, String>(2);
                    dict.put("name", name1);
                    dict.put("number", number1);
                    dataList.add(dict);



            }

    }


    }

} }

1 个答案:

答案 0 :(得分:1)

如果数组中没有数据传递给你,为什么会出现什么?我没有看到你向它添加数据。如果您在onClick()之后添加数据,请尝试调用adapter.notifyDataSetChanged()告诉它已添加数据。