2016-12-02 60 views
-2

我想將片段I傳遞給非活動類並將字符串轉換爲JSONObject。獲取字符串從片段到非活動類,然後轉換爲JSONObject

在我的片段我存儲的字符串,我需要在一個共享偏好

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

    final View rootView = inflater.inflate(R.layout.store_list, container, false); 

    //super.onCreate(); 
    VolleyGet.getInstance(getActivity()); 
    VolleyGet.getInstance().VolleyGET(domain, api, new CustomListener<String>(){ 
     @Override 
     public void getResult(String result){ 
      if (!result.isEmpty()) 
      { 
       Log.e("storelist---------->", result); 

       SharedPreferences sp1=getActivity().getSharedPreferences("storeList", Context.MODE_PRIVATE); 
       SharedPreferences.Editor editor = sp1.edit(); 
       editor.putString("storeList", result); 
       editor.commit(); 
      } 
     } 
    }); 

在我的非活性類我試圖獲取共享的偏好是這樣的:

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder>{ 


Context context; 
JSONArray storeArray; 



SharedPreferences preferences = context.getSharedPreferences("storeList", Activity.MODE_PRIVATE); 
String storeList = preferences.getString("storeList", ""); 


System.out.println("storelist", storeList); 
storeArray = new JSONArray(storeList); 
JSONObject jObj = storeArray.getJSONObject(0); 
List<Movie>  mItems; 

    public CardAdapter() { 
     super(); 
     mItems = new ArrayList<Movie>(); 
     Movie movie = new Movie(); 
     movie.setName("this will come from the JSONObject"); 
     movie.setThumbnail(R.drawable.will come from JSONObject); 
     mItems.add(movie); 

    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
     View v = LayoutInflater.from(viewGroup.getContext()) 
       .inflate(R.layout.store_cardview, viewGroup, false); 
     ViewHolder viewHolder = new ViewHolder(v); 
     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder viewHolder, int i) { 
     Movie movie = mItems.get(i); 
     viewHolder.tvMovie.setText(movie.getName()); 
     viewHolder.imgThumbnail.setImageResource(movie.getThumbnail()); 
    } 

    @Override 
    public int getItemCount() { 
     return mItems.size(); 
    } 

    class ViewHolder extends RecyclerView.ViewHolder{ 

     public ImageView imgThumbnail; 
     public TextView tvMovie; 

     public ViewHolder(View itemView) { 
      super(itemView); 
      imgThumbnail = (ImageView)itemView.findViewById(R.id.img_thumbnail); 
      tvMovie = (TextView)itemView.findViewById(R.id.tv_movie); 
     } 
    } 

}

我在1.cannot解決符號println錯誤2.Unknown類:storelist 3.storearray也是未知的

請大家幫忙,謝謝!

+0

你不能只在方法外println –

+0

你真的需要適配器中的共享首選項嗎?爲什麼不能在創建適配器之前提取json數組/對象? –

+0

@ cricket_007我可以在外面提取它,但我仍然想知道如何獲得課堂中的共享偏好,謝謝! – JerryKo

回答

0

首先,您不能System.out.println或訪問未經初始化的上下文,您已經完成它(超出您的問題範圍)。

關於其他錯誤,我不認爲你已經顯示了正確的代碼,但如果錯誤顯示「storelist」,那麼你的變量實際上是storeList,所以看起來像一個錯字。

看看RecyclerView適配器的任何教程,構造函數通常提供上下文。這就是你需要訪問SharedPreferences的東西。

public class CardAdapter extends 
    RecyclerView.Adapter<CardAdapter.ViewHolder> { 

    private JSONArray mArray; 
    private Context mContext; 
    private SharedPreferences mPrefs; 

    public CardAdapter(Context context) { 
     mContext = context; 
     mPrefs = context.getSharedPreferences("storeList", Activity.MODE_PRIVATE); 
     mArray = new JSONArray(mPrefs.getString("storeList", "[]")); 
    } 

} 
+0

關於你的評論_你不能只在print_n之外的方法_不在塊之外,以確切地;) – AxelH

+0

是的,因此「你已經做了它」 –