Using a general preference to display specific items in a list

I have an Rss feed that I parse as a list. What I'm trying to do with it is when the user first downloads the app, it saves the date or something, and then only shows one day from the list. Then when the user logs in on the second day, it remembers that date from the preference and adds it to it and shows Day the first and second day in the list, etc. Etc. Also, if the user doesn't open the app for several days, he needs to show the days the user skipped. For example, a user opens the first day of the application, and the second day and sees these articles, and then does not open the application until the 5th day, they will still need to see days 1-5 in the list. I think I can do it all with Shared Preference,however, I have not worked with general preference and have not found any tutorials that cover everything I am trying to do here. I have listed here my code that I am using. If anyone wants to work through this question with me, I would really appreciate it.

xml parsing and viewlist

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

        }
    });
}
}

      

and here's an idea of ​​how it looks now

enter image description here

+3


source to share


1 answer


Check out the SharedPreferences doc, http://developer.android.com/reference/android/content/SharedPreferences.html , I think what you need in this case is to just get / put the values ​​in SharedPreferences like

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

      

Manipulating the SharedPreference is simple, but to implement your requirement, you need to think about how to create the SharedPreferences key.



A quick thought is that the key could be shaped like "post-20130101", "post-20130102", etc. In this case, you can put the date string in the key, when you want to get the rss data within the period, you can create the corresponding keys. And you can try SharedPreferences.getAll () to get all data and then filter by date in memory.

However, I think another choice is to use a sqlite database. Using sql to filter by date column is more suitable for this kind of requirement in my opinion.

0


source







All Articles