2014-07-13 45 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); 
相关问题