我需要帮助。我有两种共享偏好方法。它们是getList和setList。
这是setList()
echo $json_b['Time Series (Daily)'][4]['4. close'];
这是getList()
public static void setList(Application activity, List<tcmb> mValuesList) {
StringBuilder valuesBuilder = new StringBuilder();
/*Log.d(TAG, "List Size ------ \t: " + mValuesList.size());*/
for (tcmb s : mValuesList) {
valuesBuilder.append(s);
valuesBuilder.append(",");
Log.d(TAG, "setValuestcmb: " + valuesBuilder.toString());
}
SharedPreferences values = activity.getSharedPreferences("dd", MODE_PRIVATE);
SharedPreferences.Editor editor = values.edit();
editor.putString("ss", valuesBuilder.toString());
editor.apply();
}
对于我得到的值,我在主要活动的asyncTask类中运行setList方法,该类分配给共享首选项。 然后,为了获得值,我在Fragment的asyncTask类中运行getList方法。 我得到了对象values.namely,我可以看到我从logcat获得的值。
public static List<tcmb> getList(Activity a) {
SharedPreferences values = a.getSharedPreferences("dd", MODE_PRIVATE);
String wordsString = values.getString("ss", "");
/* Log.d(TAG, "wordsString \t:" + wordsString);*/
String[] itemWords = wordsString.split(",");
List<String> itemList = new ArrayList<String>();
itemList.addAll(Arrays.asList(itemWords));
dovizList = (List)itemsList;
Log.d(TAG, "getValuestcmb: " + dovizList.size());
return dovizList;
}
总共19个值。
但是当我在片段类中将值转换为recylerView的bindHolder时,我得到一个错误就是
[com.example.lscodex.ddddd.Model.tcmb@4ff695, com.example.lscodex.ddddd.Model.tcmb@b6b93aa, com.example.lscodex.ddddd.Model.tcmb@347db9b, com.example.lscodex.ddddd.Model.tcmb@f63eb38, com.example.lscodex.ddddd.Model.tcmb@4866711, com.example.lscodex.ddddd.Model.tcmb@8c74076, com.example.lscodex.ddddd.Model.tcmb@3be9677, com.example.lscodex.ddddd.Model.tcmb@10882e4, com.example.lscodex.ddddd.Model.tcmb@8d9634d, com.example.lscodex.ddddd.Model.tcmb@b5eee02, com.example.lscodex.ddddd.Model.tcmb@b7c2313, com.example.lscodex.ddddd.Model.tcmb@a3ce950, com.example.lscodex.ddddd.Model.tcmb@ee5e749, com.example.lscodex.ddddd.Model.tcmb@fd1e84e, com.example.lscodex.ddddd.Model.tcmb@b7bdd6f, com.example.lscodex.ddddd.Model.tcmb@40f4a7c, com.example.lscodex.ddddd.Model.tcmb@b1caf05, com.example.lscodex.ddddd.Model.tcmb@b683b5a, com.example.lscodex.ddddd.Model.tcmb@f12e18b]
这是片段
的asyncTaskjava.lang.ClassCastException: java.lang.String cannot be cast to com.example.lscodex.ddddd.Model.tcmb
以及我收到错误的地方。
private class DownloadXmlTask extends AsyncTask<Void, Integer, List<tcmb>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressBar.setVisibility(View.VISIBLE);
}
@Override
protected List<tcmb> doInBackground(Void... voids) {
return SharedPreferenceValues.getList(getActivity());
}
@Override
protected void onPostExecute(List<tcmb> tcmb) {
if (tcmb.size() == 0) {
mtcmbList = tcmb;
setupAdapter();
mRecyclerViewAnim.runLayoutAnimation(getContext(), mRecyclerView);
mProgressBar.setVisibility(View.INVISIBLE);
} else if (mRecyclerView != null) {
mtcmbList.addAll(tcmb);
Log.d(TAG, "onPostExecute: " + mtcmbList.size());
mRecyclerView.getAdapter().notifyDataSetChanged();
dateUpdate();
mRecyclerViewAnim.runLayoutAnimation(getContext(), mRecyclerView);
mProgressBar.setVisibility(View.INVISIBLE);
mSwipeRefreshLayout.setRefreshing(false);
}
}
}
我不知道为什么?
修改
我使用共享首选项尝试它,我从启动画面获取XML列表数据并将其导入其他活动。
@Override
public void onBindViewHolder(tcmbHolder holder, int position) {
tcmb tcmb= mtcmbList.get(position); ---- the error here
holder.bindTcmb(tcmb);
答案 0 :(得分:4)
您正尝试将List<String>
更改为List<tcmb>
,问题就在于此处
public static List<tcmb> getList(Activity a){
// your code
//
List<String> itemList = new ArrayList<String>();
itemList.addAll(Arrays.asList(itemWords));
dovizList = (List)itemsList;
Log.d(TAG, "getValuestcmb: " + dovizList.size());
return dovizList;
另外,我不明白,你在这里想要达到的目标。
List<String> itemsList = new ArrayList<String>();
dovizList = (List)itemsList;
但是,根据代码共享,您需要更改
List<String> itemsList = new ArrayList<String>();
到
List<tcmb> itemsList = new ArrayList<tcmb>();
而且,你不需要
dovizList = (List)itemsList;
答案 1 :(得分:0)
我找到了怎么做。
使用splashscreen.activity中的共享首选项保存XML List对象
public static void setValues(Context context, List<tcmb> curProduct){
Gson gson = new Gson();
String jsonCurProduct = gson.toJson(curProduct);
SharedPreferences sharedPref = context.getSharedPreferences("Prefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("dd", jsonCurProduct);
editor.apply();
Log.d(TAG, "setValues: " + jsonCurProduct);
}
然后,在片段
中获取带有共享首选项的List对象public static List<tcmb> getValuesAltın(Context context){
Gson gson = new Gson();
List<tcmbDoviz> productFromShared = new ArrayList<>();
SharedPreferences sharedPref = context.getSharedPreferences("Prefs", Context.MODE_PRIVATE);
String jsonPreferences = sharedPref.getString("dd", "");
Type type = new TypeToken<List<tcmb>>() {}.getType();
productFromShared = gson.fromJson(jsonPreferences, type);
return productFromShared;
}
最后,谢谢,拉维。