在我的应用程序中,我创建了“我最喜欢的联系人”约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 ..
以下是屏幕快照:
你应该了解自定义列表视图[here](http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/),因为如果你能理解如何工作与自定义列表...我相信你不会有任何问题,这个 – 1baga
感谢@ 1baga的链接。欣赏指导。 –