2016-10-25 9 views
0

Good Day! 私のアプリケーションにはスピンダウンがあり、選択時にタイトルとその投稿の著者がリストビューで表示されます。どちらがうまくいくの?私はシリアル化によっても保存しようとしています。私はそれが働いていると信じている。接続されていないときに、シリアル化されたデータをArrayListにロードする方法は?

私の問題:かつて私は連絡がない場合のArrayListに戻ってデータをロードし、私のリストビューに表示したいセーブ。

私は以下のようないくつかの同様の質問を見ましたが、まだ私の問題を解決できませんでした。

How to load a serialized file back to an 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 

}

答えて

0

ここでは、私はファイル内のArrayListを救うために何をすべきかです。

   try { 
        MyArrayList=new ArrayList<>(); 
        if (file.exists()) { 
         int nn; 
         FileInputStream localFileInputStream = new FileInputStream(new File("YOUR FILE PATH")); 
         byte[] arr=new byte[1024]; 
         ByteArrayOutputStream by=new ByteArrayOutputStream(); 
         while ((nn=localFileInputStream.read(arr)) != -1){ 
          by.write(arr,0,nn); 
         } 
         List list=(List)new ObjectInputStream((InputStream)new ByteArrayInputStream(by.toByteArray())).readObject(); 
         MyArrayList.clear(); 
         MyArrayList.addAll(list); 
        } 
        }catch (Exception e){ 
        e.printStackTrace(); 
     } 
:バックそのリストの使用のこのコードを取得するための

  if (MyArrayList.size() > 0) { 
         FileOutputStream lObject = new FileOutputStream(new File("YOUR FILE PATH")); 
         ObjectOutputStream lco = new ObjectOutputStream(lObject); 
         lco.writeObject(MyArrayList); 
         lco.flush(); 
         lObject.close(); 
         return true; 
        } else { 
         return false; 
        } 

関連する問題