2014-12-01 161 views
0

我需要一些幫助。對於android編程還有點新意,我不確定這是否可以完成。使用OnOptionItemSelected從列表視圖插入數據庫

我在做什麼的要點:我目前從RESTful API抓取數據,將數據作爲json數組吐出。我獲取數據並將其抽成表格,然後使用適配器並在列表視圖中顯示所有數據。

的數據看起來像這樣(替換數據X):

[{ 
id: xxx, 
number: "xxxxx", 
title: "xxxxxxx", 
definition: "xxxxxxx", 
status: "xxxxx", 
name: "xxxxx" 
}, 

這裏的問題:從列表視圖,我想借此數據,並onItemSelect,我想插入選定的數據到單獨的桌子。用戶可以選擇一個或多個listview,然後從onoptionitemselect中單擊一個menuItem,它會將數據插入表中。

我有兩個主要問題:1。 我目前正在使用的setOnItemClickListener驗證碼:

String item = adapterView.getItemAtPosition(position).toString(); 

它泵出數組。我可以從數組中解析每個單獨的元素嗎? (基於數據,我想拉出ID,數字,標題等數據) 2.我不知道這是否工作,但我有選定的項目後,我可以使用onOptionsItemSelected menuitem告訴應用程序將數據插入到新表中?

這是我現在所擁有的:

public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     if (id == R.id.menu_favorite){ 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

感謝您的任何和所有的幫助。這是一個很好的學習經歷,我希望能夠學到更多。

謝謝!

回答

0

我的處理方法是維護一個被選中的id的arrayList。

在onItemClick中,將位置移動到這個arrayList。

然後在onOptionsItemSelected中,獲取arrayList的內容並使用它將數據推送到DB