android阵列列表
如何从一个活动访问另一个活动并清除ArrayList值并将其存储和检索到数据库 示例代码:android阵列列表,android,Android,如何从一个活动访问另一个活动并清除ArrayList值并将其存储和检索到数据库 示例代码: **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(
**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();
**活动1:**
public static ArrayList arrList=new ArrayList();
arrList.clear();
添加(0,txt_phone1.getText().toString());
add(1,txt_phone2.getText().toString());
完成();
**活动2:**
dbAdapter.openDataBase();
光标c=dbAdapter.selectRecordsFromDB(“SELECT*FROM tbProspect where id=“+row\u 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();
实现这一点的最佳方法是创建一个单一的共享对象,并在需要时从应用程序中的任何位置获取/设置对象
只需记住在每个活动的onCreate()
处调用getInstance()
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;
}
}
公共类共享对象{
静态SharedObjects实例;
ArrayList shraedList;
私有共享对象()
{
shraedList=新的ArrayList();
}
公共同步静态DataContext getInstance()
{
if(实例==null)
实例=新的SharedObjects();
返回实例;
}
公共ArrayList getArrayList()
{
返回instance.shraedList;
}
公共无效setArrayList(ArrayList sharedList)
{
this.sharedList=共享列表;
}
}
我建议扩展应用程序上下文:
public class MyApplication extends Application {
// Your functions here
getArray()
};
(MyApplication)getApplication();
更新AndroidManifest.xml:
<application
android:name=".MyApplication"
android:debuggable="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
...
</Application>
你在这里发布的代码有什么问题吗?我不同意使用单例。你可以在这里找到一个讨论