2012-08-09 22 views
0

與按鈕一起從列表中選擇查看一個項目我XMLandroid系統

的按鈕有一個按鈕,列表視圖沿是新的聯繫人和列表是聯繫人列表。 1.如何在此列表視圖上使用ArrayAdapter/OnListItemClick?

setListAdapter(new ArrayAdapter<String>(Contacts.this, R.id.ListView)); 

該聲明是否正確?

2.該方法是否正確?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 


    <Button 
     android:id="@+id/bAddContact" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="ADD CONTACT" /> 

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

</LinearLayout> 

回答

0

試試這個顯示簡單的列表

String [] names = 
    { "a" , "b", "c", "d", "e", "f", "g", "h", "i", "j", 
      "a1" , "b1", "c1", "d1", "e1", "f1", "g1", "h1", "i1", "j1", 
      "a2" , "b2", "c2", "d2", "e2", "f2", "g2", "h2", "i2", "j2", 
      "a3" , "b3", "c3", "d3", "e3", "f3", "g3", "h3", "i3", "j3","j4" 
    }; 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, names); 
    yourList.SetListAdapter(adapter); 

這是使用adapter.And用於獲取的listView選擇的項目嘗試這種方式。

listView.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
    long arg3) { 
      // TODO Auto-generated method stub 
     String getSelectedItemOfList = yourList.get(pos); // here you can get selected item. 
    } 
    }); 

我認爲這是你想要的。 :)

+0

可以名稱是一個空的數組?因爲我想要做的是一個基本的聯繫人應用程序,其中列表中包含人的姓名和按鈕是新的聯繫人按鈕。我不想要除新聯繫人按鈕以外的任何初始顯示。 – 2012-08-09 11:44:09

+0

什麼,你想要的,如果你想聯繫然後閱讀這篇文章http://stackoverflow.com/questions/11643229/android-application-with-phone-book-synchronization/11643681#11643681。 – Akshay 2012-08-09 11:52:30

+0

你有什麼想要的嗎? – Akshay 2012-08-09 14:26:34