2011-09-05 20 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

相關問題