2013-02-24 99 views
0

我正在開發一個簡單的應用程序,它由一個ListView和一個按鈕組成。我無法看到ListView,但該按鈕在我的活動中可見。我不知道發生了什麼事。請幫助我。ListView不可見與SimpleAdapter

我的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擴展活動{

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

如果數組中沒有數據你通過它,爲什麼會出現什麼?我沒有看到你添加數據。如果您在onClick()之後添加數據,請嘗試撥打adapter.notifyDataSetChanged()來告訴它數據已添加。

+0

我同意。您正在使用「dataList = new ArrayList >();」創建數據列表但沒有添加任何東西。 – AndroidGuy 2013-02-25 17:02:03

+0

如果我從另一個活動動態添加更多的數據到arraylist(datalist),那麼它會替換(datalist)arraylist中的舊的附加數據..所以如何處理.... – saravana 2013-03-19 13:43:52