【问题标题】:Using Shared Preference to show specific items in listview使用共享首选项在列表视图中显示特定项目
【发布时间】:2013-01-05 04:43:09
【问题描述】:

我有一个 Rss 提要,我正在将其解析为列表视图。我试图用它做的是当用户第一次加载应用程序时,它会保存日期或类似的东西,然后只显示列表的第一天。然后,当用户在第二天登录时,它会从首选项中记住该日期并向其添加一个日期,并在列表视图中显示第一天和第二天,依此类推。此外,如果用户几天没有打开应用程序,它还需要显示用户错过的日子。例如,用户在第一天和第二天打开应用程序并看到这些文章,然后直到第 5 天才打开应用程序,他们仍然需要查看列表中的第 1-5 天。我认为我可以使用共享偏好来完成所有这些工作,但是我没有使用任何共享偏好,也没有找到任何可以涵盖我在这里尝试做的任何事情的教程。我将在这里列出我正在使用的代码。如果有人愿意和我一起解决这个问题,我将不胜感激。

xml解析活动和listview

public class AndroidXMLParsingActivity extends ListActivity {

// All static variables
static final String URL = "http://www.cpcofc.org/devoapp.xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "item";
static final String KEY_NAME = "title";
static final String KEY_COST = "description";
static final String KEY_DESC = "description";
static final String KEY_GUID = "guid";
static final String KEY_LINK = "link";

ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_ITEM);
    // looping through all item nodes <item>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
        map.put(KEY_COST, parser.getValue(e, KEY_COST));
        map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
        map.put(KEY_GUID, parser.getValue(e, KEY_GUID));
        map.put(KEY_LINK, parser.getValue(e,KEY_LINK));


        // adding HashList to ArrayList
        menuItems.add(map);
    }

    // Adding menuItems to ListView
    ListAdapter adapter = new SimpleAdapter(this, menuItems,
            R.layout.list_item,
            new String[] { KEY_DESC, KEY_NAME, KEY_COST, KEY_GUID}, new int[] {
                    R.id.desciption, R.id.name, R.id.cost});

    setListAdapter(adapter);

    Collections.reverse(menuItems);

    // selecting single ListView item
    ListView lv = getListView();

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
            String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
            Uri uriUrl = Uri.parse(menuItems.get(position).get(KEY_GUID));

            // Starting new intent
            Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
            in.putExtra(KEY_NAME, name);
            in.putExtra(KEY_COST, cost);

            // **NEED TO PASS STRING OBJECT NOT URI OBJECT**
            in.putExtra(KEY_GUID, uriUrl.toString());
            startActivity(in);

        }
    });
}
}

这是它现在的样子

【问题讨论】:

    标签: android listview xml-parsing sharedpreferences


    【解决方案1】:

    查看 SharedPreferences 的文档,http://developer.android.com/reference/android/content/SharedPreferences.html,我认为在这种情况下您需要的只是将值获取/放入 SharedPreferences,例如

    public static String getPreference(Application application, String key) {
        try {
            SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(application);
            return preference.getString(key, "");
        } catch (Exception e) {
            return ""
        }
    }
    
    public static void putPreference(Application application, String key, String value) {
        try {
            SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(application);
            preference.edit().putString(key, value).commit();
        } catch (Exception e) {
            Log.d("...", e.getMessage());
        }
    }
    

    操作 SharedPreference 很简单,但要实现您的需求,您需要考虑如何设计 SharedPreferences 的键。

    一个快速的想法是,密钥可以形成为'post-20130101','post-20130102'等。在这种情况下,你可以将日期字符串放入 key,当你想在一个期间,您可以构建相应的密钥。您可以尝试 SharedPreferences.getAll() 来获取所有数据,然后根据内存中的日期进行过滤。

    不过,我认为另一种选择是使用 sqlite 数据库。我认为使用sql过滤日期列更适合这种需求。

    【讨论】:

    • 你愿意带这个去聊天并帮助我解决这个问题吗?我真的很想了解它如何更好地工作,并且我已经阅读了教程,但它们似乎只有在我希望用户登录时才有帮助,在这种情况下它真的对我没有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多