【发布时间】:2014-07-13 13:46:16
【问题描述】:
在我的应用程序中,我从设备的现有联系人中选择了大约 3 个收藏夹联系人。我能够成功地将 FAV 联系人存储到带有 Name 和 PhoneNo 的 SharedPreferences 中,并且还能够检索该信息并将其显示在 TextView 中。但我想要的是在那之后使用 custom_row 布局在列表视图中呈现这些数据。 (我已经使用 TextView 的东西来确保我可以正确检索它 - 对我自己的小测试)
我需要帮助的地方是“在我从 SharedPreferences 检索的 ListView 中显示 FAV 联系人数据”。我正在展示我的代码,我可以在 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();
}
});
}
}
如果我通过 AlertDialogs 将 FAV 联系人存储到 SharedPreferences 上,是否需要其他代码部分,请告诉我。
以下是屏幕截图:
【问题讨论】:
-
你应该了解自定义列表视图here,因为如果你能理解如何使用自定义列表视图......我相信你不会有任何问题
-
感谢@1baga 提供链接。感谢指导。
标签: android listview sharedpreferences