3

I have a Rss feed that I am parsing into a list view. What I am trying to do with it is when the user first loads the app, it saves the date or something like that and then only shows Day One of the list. Then when the user logs in the second day it remembers that date from the preference and adds one to it and shows Day One and Day Two in the list view and so forth and so forth. Also, if the user doesn't open the app for a few days it needs to show the days that the user missed as well. For Example, user opens app day one, and day two and sees those articles, then doesn't open the app till day 5, they would still need to see days 1-5 in the list. I am thinking that I can do all this with Shared Preference, however I haven't worked with shared preferences any and haven't found any tutorials that would cover anything I am trying to do here. I will list my code here that I am using. If anyone is willing to work through this issue with me I would greatly appreciate it.

xml 解析アクティビティとリストビュー

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

        }
    });
}
}

これが現在の様子です

ここに画像の説明を入力

4

1 に答える 1

0

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」などのように形成できるということです。この場合、日付文字列をキーに入れることができます。期間内の RSS データを取得したい場合は、その後、対応するキーを構築できます。また、SharedPreferences.getAll() を試してすべてのデータを取得し、メモリ内の日付に基づいてフィルター処理することもできます。

ただし、別の選択肢は sqlite データベースを使用することだと思います。私の意見では、SQLを使用して日付列をフィルタリングすることは、この種の要件により適しています。

于 2013-01-22T03:28:52.983 に答える