2012-01-28 58 views
0

如何從一個Activity訪問ArrayList到另一個Activity並清除ArrayList的值和存儲&將它檢索到數據庫?android-array list

示例代碼:

**Activity 1:** 

public static ArrayList<String> arrList =new ArrayList<String>(); 
arrList.clear(); 
arrList.add(0,txt_phone1.getText().toString()); 
arrList.add(1,txt_phone2.getText().toString()); 
finish(); 



**Activity 2:** 

dbAdapter.openDataBase(); 
Cursor c = dbAdapter.selectRecordsFromDB("SELECT * FROM tbProspect where id="+row_id,null); 
c.moveToFirst(); 
      contact_details.arrList.add(c.getString(c.getColumnIndex("Phone1"))); 
      contact_details.arrList.add(c.getString(c.getColumnIndex("Phone2"))); 
c.close();dbAdapter.close(); 
+0

你有任何問題你在這裏發佈的代碼? – 2012-01-28 12:33:41

回答

2

來,最好的辦法是創建共享對象的單身人士,並獲得/從應用程序中的任何地方需要時設置你的對象。

只記得撥打getInstance()onCreate()每個Activity

public class SharedObjects { 

    static SharedObjects instance; 
    ArrayList<String> shraedList; 

    private SharedObjects() 
    { 
     shraedList = new ArrayList<String>(); 
    } 

    public synchronized static DataContext getInstance() 
    { 
     if (instance == null) 
      instance = new SharedObjects(); 

     return instance; 
    } 

    public ArrayList<String> getArrayList() 
    { 
     return instance.shraedList; 
    } 

    public void setArrayList(ArrayList<String> sharedList) 
    { 
    this.sharedList = sharedList; 
    } 
} 
+0

感謝你的答覆... – Prakash 2012-01-28 13:13:09

+0

我不同意單身人士的使用。你可以在這裏找到一個討論http://stackoverflow.com/questions/3826905/singletons-vs-application-context-in-android – Frohnzie 2012-01-28 16:54:04

0

我會建議延長應用程序上下文:

public class MyApplication extends Application { 
    // Your functions here 
    getArray() 
}; 

更新的AndroidManifest.xml:

<application 
     android:name=".MyApplication" 
     android:debuggable="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
... 
</Application> 

要獲取應用程序上下文:

(MyApplication)getApplication(); 
相關問題