0
Good Day! 私のアプリケーションにはスピンダウンがあり、選択時にタイトルとその投稿の著者がリストビューで表示されます。どちらがうまくいくの?私はシリアル化によっても保存しようとしています。私はそれが働いていると信じている。接続されていないときに、シリアル化されたデータをArrayListにロードする方法は?
私の問題:かつて私は連絡がない場合のArrayListに戻ってデータをロードし、私のリストビューに表示したいセーブ。
私は以下のようないくつかの同様の質問を見ましたが、まだ私の問題を解決できませんでした。
のonCreate:私は接続状態に
// onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate: RAN");
Context context = this;
final Spinner spinner = (Spinner) findViewById(R.id.querySpinner);
listView = (ListView) findViewById(R.id.resultsListView);
ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
if (manager != null) {
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null) {
boolean isConnected = info.isConnected();
// Network Operations
if (isConnected) {
Log.i(TAG, "onCreate: CONNECTED TO A NETWORK");
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
DataTask task = new DataTask();
if (pos == 0) {
Log.i(TAG, "onItemSelected: Pandas");
task.execute("https://api.reddit.com/r/pandas");
} else if (pos == 1) {
Log.i(TAG, "onItemSelected: Koalas");
task.execute("https://api.reddit.com/r/koalas");
} else if (pos == 2) {
Log.i(TAG, "onItemSelected: Chimpanzees");
task.execute("https://api.reddit.com/r/chimpanzees");
} else if (pos == 3) {
Log.i(TAG, "onItemSelected: Emus");
task.execute("https://api.reddit.com/r/emus");
} else if (pos == 4) {
Log.i(TAG, "onItemSelected: Zebras");
task.execute("https://api.reddit.com/r/zebras");
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
Log.i(TAG, "onNothingSelected: NOTHING SELECTED");
}
});
} else {
Log.i(TAG, "onCreate: NO CONNECTION");
Toast.makeText(context, R.string.toast_notConnected, Toast.LENGTH_LONG).show();
}
}
}
} // End onCreate
を取得していAsyncTask:私はコールsaveSerialization(よ)
// DataTask Class
private class DataTask extends AsyncTask<String, Void, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.i(TAG, "onPreExecute: RAN");
}
@Override
protected String doInBackground(String... url) {
Log.i(TAG, "doInBackground: RAN");
return getNetworkData(url[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.i(TAG, "onPostExecute: RAN");
try {
JSONObject outerObject = new JSONObject(s);
JSONObject dataObject = outerObject.optJSONObject("data");
JSONArray childrenArray = dataObject.optJSONArray("children");
ArrayList<Post> postList = new ArrayList<>();
for (int i = 0; i <childrenArray.length() ; i++) {
Post post = new Post("","");
JSONObject data = childrenArray.optJSONObject(i).optJSONObject("data");
postTitle = MainActivity.this.getString(R.string.string_title) + " " + data.getString("title");
post.setmTitle(postTitle);
postAuthor = MainActivity.this.getString(R.string.string_author) + " " + data.getString("author");
post.setmAuthor(postAuthor);
postList.add(post);
saveSerializable(postList);
}
setupBaseAdapter(postList);
} catch (Exception e) {
e.printStackTrace();
}
}
} // End DataTask
saveSerialization()とloadSerialization()メソッド:どこで配列liにロードしようとしますかst。
// saveSerializable
private void saveSerializable(ArrayList<Post> arrposts) {
Log.i(TAG, "saveSerializable: RAN");
try {
FileOutputStream fileOutputStream = openFileOutput("post.txt", MODE_PRIVATE);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(arrposts);
objectOutputStream.close();
} catch(IOException e) {
e.printStackTrace();
}
} // End saveSerializable
private void loadSerializable() {
Post post = null;
try {
FileInputStream fileInputStream = openFileInput("post.txt");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
post = (Post) objectInputStream.readObject();
objectInputStream.close();
} catch(IOException | ClassNotFoundException e) {
e.printStackTrace();
}
if(post != null) {
}
}
ポストクラス:
public class Post implements Serializable {
private String mTitle;
private String mAuthor;
public Post(String mTitle, String mAuthor) {
this.mTitle = mTitle;
this.mAuthor = mAuthor;
}
public String getmTitle() {
return mTitle;
}
public void setmTitle(String mTitle) {
this.mTitle = mTitle;
}
public String getmAuthor() {
return mAuthor;
}
public void setmAuthor(String mAuthor) {
this.mAuthor = mAuthor;
}
}
PostAdapter:
class PostAdapter extends BaseAdapter {
private final Random rand = new Random();
private final int ID = rand.nextInt(Integer.MAX_VALUE);
private final Context context;
private List<Post> posts = new ArrayList<>();
public PostAdapter(Context context, List<Post> posts) {
this.context = context;
this.posts = posts;
}
@Override
public int getCount() {
return (posts != null) ? posts.size() :0;
}
@Override
public Post getItem(int pos) {
return (posts != null && pos < posts.size() && pos >= 0) ? posts.get(pos) : null;
}
@Override
public long getItemId(int pos) {
return ID + pos;
}
@Override
public View getView(int pos, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.lv_redditposts, viewGroup, false);
holder = new ViewHolder(view);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
Post post = getItem(pos);
holder.postTitle.setText(post.getmTitle());
holder.postAuthor.setText(post.getmAuthor());
return view;
}
// ViewHolder
static class ViewHolder {
public final TextView postTitle;
public final TextView postAuthor;
public ViewHolder(View v){
postTitle = (TextView) v.findViewById(R.id.postTitleTextView);
postAuthor = (TextView) v.findViewById(R.id.postAuthorTextView);
}
} // End ViewHolder
}