2013-10-15 30 views
2

我想從一個活動轉移到另一個ArrayList通過意圖。我如何能做到這一點,因爲我不能做的JSONObject類來實現parcelable如何把意圖放入一個ArrayList <JSONObject>

+0

爲什麼它不可Parcelable? –

+0

那麼JSONObject類不是我創建的類,它是從org.json.JSONObject派生的,我怎樣才能使它實現parcable? – Libathos

+0

哦,我明白你的意思了。檢查了這一點:http://stackoverflow.com/questions/5082122/passing-jsonobject-into-another-activity –

回答

5

您可以簡單地把一個完整的JSONObject的更多信息串。

intent.putString("YOUR_KEY", jsonObj.toString); 

然後在SecondActivity您可以將JSON字符串轉換成JSONObject的

JSONObject jsonObj = new JSONObject(getIntent().getStringExtra("YOUR_KEY")); 

您可以創建一個ArrayList的即json.ToString()

JSONObject json1 = null,json2 = null; 
ArrayList<String> jsonList=new ArrayList<String>(); 
jsonList.add(json1.toString()); 
jsonList.add(json2.toString()); 
i.putStringArrayListExtra("json_list", jsonList); 

然後在您可以將Json字符串轉換爲JsonObject的SecondActivity

JSONObject jsonObj1 = new JSONObject(getIntent().getStringExtra("json_list").get(0)); 
JSONObject jsonObj2 = new JSONObject(getIntent().getStringExtra("json_list").get(1)); 
0

如果你讓你的對象類實現Parcelable你可以收拾你的ArrayList到你的意圖

髮束看到this link爲示例

+0

好吧,但它不是我的對象類它派生自org.json.JSONObject,我沒有寫如何可以我讓它實現parcelable? – Libathos

+0

看看這個答案:http://stackoverflow.com/a/5082395/1484779 – jlopez

-1

要從一個Activity更改爲另一個,您需要一個意圖。你可以把這個意圖額外的信息。並非所有類型的都可以,但字符串是*:

Intent mIntent = new Intent(this, Example.class); 
Bundle mBundle = new Bundle(); 
mBundle.extras.putString(key, value); 
mIntent.putExtras(mBundle); 

*您可以找到更多的信息:Passing a Bundle on startActivity()?

正如你可能知道,你能夠把一個ArrayListJSONArray,這最後一個可序列化作爲一個字符串**:

ArrayList<String> list = new ArrayList<String>(); 
list.add("foo"); 
list.add("baar"); 
JSONArray jsArray = new JSONArray(list); 

**你可以找到更多的信息:convert ArrayList<MyCustomClass> to JSONArray

現在你只需要把你的陣列串到你的意圖,並在您最近打開類***再次對其進行分析:

String value = getIntent().getExtras().getString(key); 

***在第一連桿

+0

好吧,但然後如何將我的字符串轉換爲ArrayList ? – Libathos

+0

應該像新的JSONArray(值)。 然後你有一個JSONArray,從那裏你可以得到 –

+0

@libathos裏面的對象,在最後一步,你有一個字符串,它有一個JSON數組的JSON數組。你只需要解析json字符串並「重新安裝」你的ArrayList ...這是一個簡單的JSON例程。在這個鏈接上檢查我的答案,其中用戶不知道如何解析JSON以將其放入ListView中,解析部分與您需要執行的操作類似:http:// stackoverflow。com/questions/19179610/google-api-parsing-in-json/19180887#19180887 –

相關問題