【问题标题】:How to present in a ListView the data which is stored in a SharedPreferences?如何在 ListView 中显示存储在 SharedPreferences 中的数据?
【发布时间】: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


【解决方案1】:

我不知道您为什么要在循环中设置适配器,但这里有一个示例如何使用您的联系人创建适配器,然后使用它来使用自定义行填充列表视图:

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 中有一个带有 android:id="@+id/imp_Contacts_List" 的列表视图。

【讨论】:

  • 太完美了。谢谢@user3249477。还有一件事是我想将 1、2、3 之类的数字添加到 list_row 项目中,而不仅仅是名称为 n phoneNo...可以这样做吗?如果可以,请提供提示..
  • 如果我理解正确,您可以改为:contacts.add((i+1)+". "+contacts_list); 我将更新答案以证明这一点。
  • 酷..你做对了......看起来我可以按照我想要的方式处理传递给 add() 的东西......对于 UI。感谢大师的支持!
【解决方案2】:

您可以使用adapter.add(data);adapter.remove(data); 方法来修改适配器中的数据。

试试这个-

    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);

【讨论】:

    猜你喜欢
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    相关资源
    最近更新 更多