2014-07-13 86 views
0

在我的應用程序中,我創建了「我最喜歡的聯繫人」約3個,我從設備中的現有聯繫人中選擇。我能夠成功地將FAV聯繫人存儲到帶有姓名和電話號碼的SharedPreferences中,並且能夠檢索該信息並將其顯示在TextView中。但是我想要的是在後面用custom_row佈局在列表視圖中呈現數據。 (我用TextView的東西,以確保我能正確地檢索它 - 小試爲我自己)如何在ListView中呈現存儲在SharedPreferences中的數據?

在我需要幫助「存在於一個ListView的FAV聯繫人數據這是我從SharedPreferences檢索」 。我展示了我的代碼,我可以在TextView中呈現數據。我會很高興,如果你們可以建議我如何把那到ListView ...

下面是代碼片段:

public class FavContacts_Activity extends ActionBarActivity { 
TextView title, tV_Contacts_list; 
Button go_Back_to_ContactsList; 
ListView imp_Contacts_List; 
public static final String PREF_FILE_NAME = "PACKAGE"; 
SharedPreferences preferences; 
String [] imp_Contacts = {}; 
StringBuffer sb = new StringBuffer(); 
int count = 0, i = 1; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView (R.layout.favcontacts_list); 
     title = (TextView) findViewById (R.id.contacts_title); 
     tV_Contacts_list = (TextView) findViewById (R.id.tV_Contacts_list); 
     imp_Contacts_List = (ListView) findViewById (R.id.listView1); 
     go_Back_to_ContactsList = (Button) findViewById (R.id.btn_Back); 
     ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, imp_Contacts); 


     preferences = getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE); 

     for (count = 0; count <3; count++) { 
      String contacts_list = preferences.getString("CONTACT "+count, null); 
      tV_Contacts_list.setTextSize(15); 
      tV_Contacts_list.append("\n  "+ i +". "+ contacts_list); 
      imp_Contacts_List.setAdapter(adapter); 
      i++; 
     } 

     go_Back_to_ContactsList.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       finish(); 
      } 
     }); 
    } 
} 

讓我知道,如果需要代碼的其他部分,其中我的FAV聯繫人存儲到通過AlertDialogs的SharedPreferences ..

以下是屏幕快照: Emulator Screenshot showing the data

+1

你應該瞭解自定義列表視圖[here](http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/),因爲如果你能理解如何工作與自定義列表...我相信你不會有任何問題,這個 – 1baga

+0

感謝@ 1baga的鏈接。欣賞指導。 –

回答

2

我不知道爲什麼你在循環中設置轉接器,但這裏有一個如何與您的聯繫人創建一個適配器,然後用它來填充自定義行的列表視圖:

preferences = getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE); 

List<String> contacts = new ArrayList<String>(); 
// Loop through the shared prefs, adding contacts to our List 
for (int i = 0; i < 3; i++) { 
    String contacts_list = preferences.getString("CONTACT "+i, null); 
    contacts.add((i+1)+". "+contacts_list); 
} 

// Create the adapter with contacts 
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.listview_row, R.id.list_item, contacts); 

// Populate the list 
imp_Contacts_List.setAdapter(adapter); 

您的自定義列表視圖的列,listview_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/list_item" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="10dp"> 
    </TextView> 

</LinearLayout> 

注意: 確保您有列表視圖您favcontacts_list.xml內。

+0

這很完美。謝謝@ user3249477。還有一件事是我想添加數字,如1,2,3到list_row項目,而不是隻有名稱n phoneNo ...可以這樣做,如果是這樣,請幫助提示.. –

+1

如果我明白正確地,你可以改變爲:'contacts.add((i + 1)+「。」+ contacts_list);'我會更新答案來證明這一點。 – Simas

+0

酷..u得到它的權利...看起來我可以玩的東西傳遞給add()我想要的方式...爲用戶界面。感謝師父的支持! –

0

您可以使用adapter.add(data);adapter.remove(data);方法ŧ o修改適配器中的數據。

嘗試這個 -

preferences = getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE); 
    for (count = 0; count <3; count++) { 
     String contacts_list = preferences.getString("CONTACT "+count, null); 
     adapter.add(contacts_list); 
     tV_Contacts_list.setTextSize(15); 
     tV_Contacts_list.append("\n  "+ i +". "+ contacts_list); 
     i++; 
    } 
    imp_Contacts_List.setAdapter(adapter); 
相關問題