2017-09-26 47 views
0

我在填充了事件的列表片段的列表視圖。我在這些列表項上有一個心臟圖標,允許用戶單擊並最喜歡這些項目。當項目被收藏時,我正在將事件ID保存在sharedprefs中。如何將ListView人口從JSON轉換爲SharedPreferences?

enter image description here

這是正常工作,並節省了整個用戶會話的最愛的項目。我現在需要在收藏夾列表視圖中填充這些收藏的項目。舊的開發者從api嘗試了一個JSON實現,但它從來沒有奏效。我現在只想從sharedprefs填充這個列表。下面有我最喜歡的片段代碼......

import android.animation.Animator; 
import android.animation.AnimatorListenerAdapter; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.preference.PreferenceManager; 
import android.support.design.widget.TabLayout; 
import android.support.v4.app.Fragment; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.ListView; 
import android.widget.TextView; 

import com.appsflyer.AppsFlyerLib; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.Map; 

import lakeviewlabs.ticketliquidator.api.delete_favorites.DeleteFavoritesApi; 
import lakeviewlabs.ticketliquidator.api.delete_favorites.DeleteFavoritesApiOut; 
import lakeviewlabs.ticketliquidator.api.get_favorites.GetFavoritesApi; 
import lakeviewlabs.ticketliquidator.api.get_favorites.GetFavoritesApiOut; 

import static com.urbanairship.UAirship.getApplicationContext; 

public class FavoritesFragment extends Fragment implements GetFavoritesApiOut, DeleteFavoritesApiOut { 

    View rootView; 
    ProgressDialog pDialog; 
    FavoritesAdapter adapter; 
    Boolean checkState = false; 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     Map<String, Object> eventValue = new HashMap<String, Object>(); 
     AppsFlyerLib.getInstance().trackEvent(getActivity().getApplicationContext(), "View Favorites",eventValue); 

     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
     Map<String,?> keys = prefs.getAll(); 

     for(Map.Entry<String,?> entry : keys.entrySet()){ 
      Log.d("map values",entry.getKey() + ": " + 
        entry.getValue()); 

      if (entry.getKey().matches("[0-9]+") && entry.getValue().equals(true)) { 
       Log.d("using values",entry.getKey() + ": " + 
         entry.getValue()); 
       //  performersList.add(entry.getKey().); 
      } 
     } 

     rootView = inflater.inflate(R.layout.fragment_favorites, container, false); 

     GetFavoritesApi api = new GetFavoritesApi(getContext(), this); 
     rootView.findViewById(R.id.delete_favorites).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       deleteFavorites(); 
      } 
     }); 

     showLoadingMessage(); 

     api.get(); 
     return rootView; 

    } 

    private void deleteFavorites() { 
     ArrayList<String> ids = new ArrayList<String>(); 
     ListView listView = (ListView) rootView.findViewById(R.id.favorites); 
     DeleteFavoritesApi api = new DeleteFavoritesApi(getContext(), this); 

     for (int i = 0; i < listView.getCount(); i++){ 
      View view = listView.getChildAt(i); 
      CheckBox check = (CheckBox)view.findViewById(R.id.favorite_select); 
      if(check.isChecked()){ 
       ids.add(view.getTag().toString()); 
      } 
     } 

     if(ids.isEmpty()){ 
      onDeleteFavoritesSuccess(); 
      return; 
     } 

     api.delete(android.text.TextUtils.join(",", ids)); 

    } 

    private void showLoadingMessage(){ 
     pDialog = new ProgressDialog(getContext()); 
     pDialog.setMessage("Getting favorite performers..."); 
     pDialog.show(); 

    } 

    @Override 
    public void onGetFavoritesSuccess(JSONArray favorites) { 
     ListView listView = (ListView) rootView.findViewById(R.id.favorites); 

     ArrayList<JSONObject> performersList = new ArrayList<JSONObject>(); 

     for(int i = 0; i < favorites.length(); i++) { 
      JSONObject performer = null; 

      try { 
       performer = favorites.getJSONObject(i); 
       performersList.add(performer); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

     } 

     adapter = new FavoritesAdapter(getContext(), performersList); 

     listView.setAdapter(adapter); 
     listView.setOnItemClickListener(new ListView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       if(checkState) { 
        CheckBox check = (CheckBox)view.findViewById(R.id.favorite_select); 
        check.setChecked(!check.isChecked()); 
       }else{ 
        Intent intent = new Intent(getContext(), PerformerActivity.class); 
        intent.putExtra("performer_id", view.getTag().toString()); 
        startActivity(intent); 
       } 

      } 
     }); 

     pDialog.dismiss(); 

    } 

    @Override 
    public void onGetFavoritesError(JSONObject errors) { 

    } 

    @Override 
    public void onGetFavoritesUnexpectedError() { 

    } 

    public void showCheckboxes() { 

     checkState = !checkState; 

     rootView.findViewById(R.id.delete_favorites).setVisibility(checkState ? View.VISIBLE : View.INVISIBLE); 

     ListView listView = (ListView) rootView.findViewById(R.id.favorites); 

     for (int i = 0; i < listView.getCount(); i++){ 
      View view = listView.getChildAt(i); 
      CheckBox check = (CheckBox)view.findViewById(R.id.favorite_select); 
      check.setVisibility(checkState ? View.VISIBLE : View.GONE); 
      check.setChecked(false); 
//   view.animate() 
//    .translationX(150) 
//    .setDuration(300) 
//    .setListener(new AnimatorListenerAdapter() { 
//     @Override 
//     public void onAnimationEnd(Animator animation) { 
// 
//     } 
//    }); 
     } 

// 
    } 

    @Override 
    public void onDeleteFavoritesSuccess() { 
     GetFavoritesApi api = new GetFavoritesApi(getContext(), this); 
     showLoadingMessage(); 
     showCheckboxes(); 
     api.get(); 
    } 
} 

我試着解析所有能夠登錄所需要的那些的sharedprefs和Im。我只是不能讓他們顯示在收藏夾列表中。

回答

0

您可以將項目的對象作爲JSON字符串存儲在共享首選項中,並且每當將新項目添加到收藏夾時,都會將該項目的JSON對象附加到JSON字符串和收藏夾列表中,並從中解析json字符串共享首選項。我假設你從API獲取JSON數組的結果。我建議以接收的格式存儲JSON字符串,以便您可以重複使用相同的POJO作爲收藏夾列表。 希望這有助於。

+0

這大概是我必須做的。感謝您的評論。 –

0

你不應該存儲你的喜好這樣。將您的活動存儲爲使用其ID作爲密鑰會在您的共享首選項中造成大量混亂。 改爲:

  • 存儲事件ID列表。由於無法在共享首選項中存儲列表,請使用Gson將列表轉換爲Json字符串。然後存儲字符串。再次使用Gson從存儲的字符串中重新生成列表。
  • 其他(更好)的選擇,是使用本地數據庫。這會更容易,更清潔。您將能夠輕鬆存儲更多信息。就像你可以存儲整個事件(而不是ID只),並要求他們回到填充自己喜歡的活動的ListView

注:由@Varun_Ramani的建議你可以在整個事件存儲在共享偏好,但不推薦使用它,因爲共享首選項的目的不是取代數據庫(如official documentation中所述,見下文)。

如果您想要保存的鍵值的集合相對較小,則應使用SharedPreferences API。

+0

理論上它會保存少量的集合。真的不應該超過10個左右。 –

+0

Idk你的事件對象(大/小)以及你試圖完成的事情。您可以將它們存儲在共享首選項中,因爲它可以更快地實現,但是也許您會意識到在某些情況下,數據庫更好。在任何情況下,將它們(整個列表)作爲Json字符串存儲在一個唯一鍵(如「key_favourite」)下。您需要知道您用於存儲數據的密鑰,否則不可能(非常困難)檢索它們。 – Eselfar