2011-09-05 24 views
0

我创建的当前应用程序使用gson通过远程托管的json文件解析和填充listview。我在我的应用程序中有一个选项菜单,我希望使用它可以让用户选择/取消选择项目,以便它们可以显示(如果选中)或不显示(如果取消选择)在屏幕上显示的列表视图中远程json文件。如何在Android中使用RESTful Web服务保存并读回sharedpreferences?

我的问题是,我该如何去实现这个功能?我可以简单地使用共享首选项吗?使用json文件时甚至可以隐藏某些列表项?

我一直有很多麻烦,试图找出这一点,所以任何和所有的帮助将不胜感激。

回答

0

使用自定义ArrayAdapter你可以“选择”什么是根据一些显示首选项(我会做你传递给构造函数对象的名单上的过滤)。 类似的东西:

public class DbAdapter extends ArrayAdapter<Db> { 
    private SharedPreferences preferences; 
    int resource; 
    String response; 
    Context context; 
    //Initialize adapter 
    public DbAdapter(Context context, int resource, List<Database> items) { 
     super(context, resource); 
     this.preferences = PreferenceManager.getDefaultSharedPreferences(context); 
     filter(items); 
     this.resource=resource; 
    } 

    private final void filter(final List<Database> input) { 
     final String pref = this.preferences.getString("some_key", ""); 
     for (Database db : input) { 
      if (!pref.contains(db.dbid + ",")) { 
       this.add(db); 
      } 
     } 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     LinearLayout DbView; 
     //Get the current Database object 
     Database db = this.getItem(position); 
// ... 

这假定偏好 “some_key” 包含dbId一个逗号分隔的列表(如1,5,12,

NB

+0

嗯,我已经使用了没有经过测试数组适配器为我的listivew生成行。我一直在敲我的头靠在墙上一个星期,现在试图找出如何在我的应用程序中工作。你能详细说明你的答案吗?我对android还是有点新鲜。 – fuzz

+0

你可以添加你的适配器到你的问题?或者给出一个代码链接(pastebin或类似的东西) – 2011-09-05 11:23:14

+0

这里是我的数组和一个辅助类的pastebin链接,用于将json转换为字符串。 http://pastebin.com/yEq7BN3G – fuzz

相关问题