gpt4 book ai didi

android - 如何将 ArrayList 或 HashMap 存储到文件或 SharedPreferences 中?

转载 作者:行者123 更新时间:2023-11-30 02:01:48 26 4
gpt4 key购买 nike

我进行了搜索,但未能找到适合我的问题的解决方案。我正在从服务器接收数据并显示它,我希望将此数据存储在本地,最好是在 SharedPreferences 或文件中如果可以访问 Internet,则应存储和更新数据。如果没有 Internet 访问权限,则应显示此数据。

下面是我的代码

package com.timetable;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MondayTimeTable_TE_Elex extends ListActivity {
// JSON Node names
private static String TAG_ID = "id";
private static String TAG_NAME = "name";
String FILENAME = "monday_te_elex_file";
FileInputStream fis;

ListView listViewItem;
// contacts JSONArray
JSONArray monday_te_elex = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_monday_time_table_te_elex);

new PlaceOrder().execute();
listViewItem = getListView();

}

ArrayList<HashMap<String, String>> categoriesList;

class PlaceOrder extends AsyncTask<Void, Void, Void> {

@Override
protected Void doInBackground(Void... params) {

// TODO Auto-generated method stub
InputStream inputStream = null;
String result = null;

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
// Example for param :
// nameValuePairs.add(new BasicNameValuePair("id", 1));
inputStream = Request.setHttpRequest(
"http://asimmerchant.hostoi.com/TE/Elex/Monday/monday_te_elex.php",
nameValuePairs);

// Hashmap for ListView
categoriesList = new ArrayList<HashMap<String, String>>();

if (inputStream != null) {
result = StringResponse.convertResponseToString(inputStream);
result = result.trim();
}

if (result == null || result.equals("null")) {
System.out.println("Invalid request");

} else {

try {
JSONArray jArray = new JSONArray(result);
JSONObject json_data = null;
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);

try {
String id = json_data.getString(TAG_ID);
String name = json_data.getString(TAG_NAME);

// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();

// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);

categoriesList.add(map);

} catch (ParseException e) {
e.printStackTrace();
}
}
} catch (JSONException e1) {
Toast.makeText(getBaseContext(), "ERROR", Toast.LENGTH_LONG)
.show();
} catch (ParseException e1) {
Toast.makeText(getBaseContext(), "Error", Toast.LENGTH_LONG)
.show();
}

}
return null;

}

@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(MondayTimeTable_TE_Elex.this,
categoriesList, R.layout.list, new String[] { TAG_NAME},
new int[] { R.id.name });

setListAdapter(adapter);

/**
* Selecting single ListView item
*/
}
}
}

最佳答案

SharedPreferences 不能只存储基本类型,所以你不能用它来存储 Collection。您可以使用序列化将其写入内部存储,但它可能会变得棘手。我建议您将 JSONObject/JSONArray(结果是您的情况)作为 String 存储在 SharedPreferences 中。

关于android - 如何将 ArrayList 或 HashMap 存储到文件或 SharedPreferences 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31354703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com