Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用共享首选项在listview中显示特定项_Android_Listview_Xml Parsing_Sharedpreferences - Fatal编程技术网

Android 使用共享首选项在listview中显示特定项

Android 使用共享首选项在listview中显示特定项,android,listview,xml-parsing,sharedpreferences,Android,Listview,Xml Parsing,Sharedpreferences,我有一个Rss提要,正在解析为列表视图。我想做的是,当用户第一次加载应用程序时,它会保存日期或类似的内容,然后只显示列表中的第一天。然后,当用户在第二天登录时,它会从首选项中记住该日期并向其中添加一个日期,并在列表视图中显示第一天和第二天,依此类推。此外,如果用户有几天没有打开应用程序,则还需要显示用户错过的天数。例如,用户在第一天和第二天打开应用程序并查看这些文章,然后直到第5天才打开应用程序,他们仍然需要查看列表中的第1-5天。我想我可以用共享偏好来完成所有这些,但是我没有使用共享偏好,也没

我有一个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);

        }
    });
}
}
公共类AndroidXMLParsingActivity扩展了ListActivity{
//所有静态变量
静态最终字符串URL=”http://www.cpcofc.org/devoapp.xml";
//XML节点密钥
静态最终字符串KEY\u ITEM=“ITEM”//父节点
静态最终字符串键\u ID=“item”;
静态最终字符串键\u NAME=“title”;
静态最终字符串键\u COST=“description”;
静态最终字符串键\u DESC=“description”;
静态最终字符串键\u GUID=“GUID”;
静态最终字符串键\u LINK=“LINK”;
ArrayList menuItems=新建ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
最终ArrayList menuItems=新ArrayList();
XMLParser=新的XMLParser();
字符串xml=parser.getXmlFromUrl(URL);//获取xml
Document doc=parser.getDomeElement(xml);//获取DOM元素
NodeList nl=doc.getElementsByTagName(键项);
//循环通过所有项目节点
对于(int i=0;ivalue
put(KEY_ID,parser.getValue(e,KEY_ID));
put(KEY_NAME,parser.getValue(e,KEY_NAME));
put(KEY_COST,parser.getValue(e,KEY_COST));
put(KEY_DESC,parser.getValue(e,KEY_DESC));
put(KEY_GUID,parser.getValue(e,KEY_GUID));
put(KEY_-LINK,parser.getValue(e,KEY_-LINK));
//将哈希列表添加到ArrayList
menuItems.add(地图);
}
//向ListView添加菜单项
ListAdapter=new SimpleAdapter(此,菜单项,
R.layout.list_项目,
新字符串[]{KEY_DESC,KEY_NAME,KEY_COST,KEY_GUID},新int[]{
R.id.description,R.id.name,R.id.cost});
setListAdapter(适配器);
集合。反向(菜单项);
//选择单个ListView项
ListView lv=getListView();
lv.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//从选定的ListItem获取值
字符串名称=((TextView)view.findviewbyd(R.id.name)).getText().toString();
字符串成本=((TextView)view.findViewById(R.id.cost)).getText().toString();
uriUrl=Uri.parse(menuItems.get(position.get(KEY_-GUID));
//开始新的意图
Intent in=新的Intent(getApplicationContext(),SingleMenuItemActivity.class);
in.putExtra(键名称、名称);
额外费用(关键成本、成本);
//**需要传递字符串对象而不是URI对象**
in.putExtra(KEY_GUID,uriUrl.toString());
星触觉(in);
}
});
}
}
下面是它现在的样子


检查SharedReferences文档,我认为在这种情况下,您需要的只是获取/放置SharedReferences的值,例如

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());
    }
}
操纵SharedReference很简单,但要实现您的需求,您需要考虑如何设计SharedReference的密钥

一个简单的想法是,键可以形成为“post-20130101”、“post-20130102”等。在这种情况下,您可以将日期字符串放入键中,当您想要在一段时间内获取rss数据时,您可以构建相应的键。您可以尝试SharedReferences.getAll()获取所有数据,然后根据内存中的日期进行筛选


然而,我认为另一种选择是使用sqlite数据库。在我看来,使用sql筛选日期列更适合这种需求。

您是否愿意将其带到聊天室,并帮助我完成这项工作。我真的很想了解这是如何更好地工作的,我已经阅读了教程,但它们似乎只有在我希望用户登录时才有帮助,这对我处理这种情况真的没有帮助