我在填充了事件的列表片段的列表視圖。我在這些列表項上有一個心臟圖標,允許用戶單擊並最喜歡這些項目。當項目被收藏時,我正在將事件ID保存在sharedprefs中。如何將ListView人口從JSON轉換爲SharedPreferences?
這是正常工作,並節省了整個用戶會話的最愛的項目。我現在需要在收藏夾列表視圖中填充這些收藏的項目。舊的開發者從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。我只是不能讓他們顯示在收藏夾列表中。
這大概是我必須做的。感謝您的評論。 –