Java Android从另一个活动获取MainActivity内的方法值
我尽量使这件事尽可能简单。我有一个SQLite数据库,其中包含以下方法,该方法返回表中的所有名称:Java Android从另一个活动获取MainActivity内的方法值,java,android,sqlite,Java,Android,Sqlite,我尽量使这件事尽可能简单。我有一个SQLite数据库,其中包含以下方法,该方法返回表中的所有名称: public ArrayList<String> getAllPlacesNames() { SQLiteDatabase db = this.getWritableDatabase(); Cursor result = db.rawQuery("select placeName FROM " + DataTableName,null);
public ArrayList<String> getAllPlacesNames() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("select placeName FROM " + DataTableName,null);
ArrayList<String> companyNames = new ArrayList<String>();
while(result.moveToNext()) {
String places = result.getString(result.getColumnIndex("placeName"));
companyNames.add(places);
}
return companyNames;
}
现在是我的问题。我正在尝试为列表视图创建一个自定义适配器,以便显示名称。但是我不知道如何从我的适配器文件StringArrayAdapter.java获取GetAllPlacesNameMain()
的返回值,并将其转换为数组,以便在构造函数中使用。以下是我到目前为止的情况:
public class StringArrayAdapter extends BaseAdapter {
Context ctxt;
LayoutInflater myInflator;
DataBaseHelper summerJobDB;
public ArrayList<String> getAllPlacesNamesMain() {return summerJobDB.getAllPlacesNames();}
String[] places = new String [getAllPlacesNamesMain().size()];
// convert places to Array
places = getAllPlacesNamesMain().toArray(places);
public StringArrayAdapter(String [] arr, Context c) {
// here I need to set places to arr
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// create a cell to be displayed inside listview (a view)
return null;
}
}
公共类StringArrayAdapter扩展BaseAdapter{
上下文ctxt;
充气机;
数据库数据库;
public ArrayList getAllPlacesNamesMain(){return summerjobd.getAllPlacesNames();}
String[]places=新字符串[GetAllPlacesNameMain().size()];
//将位置转换为数组
places=GetAllplacesNameMain().toArray(位置);
公共StringArrayAdapter(字符串[]arr,上下文c){
//在这里,我需要设置arr的位置
}
@凌驾
public int getCount(){
返回0;
}
@凌驾
公共对象getItem(int位置){
返回null;
}
@凌驾
公共长getItemId(int位置){
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//创建要在listview(视图)中显示的单元格
返回null;
}
}
任何帮助都将不胜感激 试试这个:
public class StringArrayAdapter extends BaseAdapter {
Context ctxt;
LayoutInflater myInflator;
DataBaseHelper mSummerJobDB;
private ArrayList<String> mAllPlacesNames;
public StringArrayAdapter(DataBaseHelper summerJobDB, Context c) {
// here I need to set places to arr
this.ctxt = c;
this.mSummerJobDB = summerJobDB;
mAllPlacesNames = summerJobDB.getAllPlacesNames();
}
@Override
public int getCount() {
return mAllPlacesNames.size();
}
@Override
public Object getItem(int position) {
return mAllPlacesNames.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// create a cell to be displayed inside listview (a view)
return null;
}
}
公共类StringArrayAdapter扩展BaseAdapter{
上下文ctxt;
充气机;
数据库助手mSummerJobDB;
私人ArrayList商场;
PublicStringArrayAdapter(DataBaseHelper summerJobDB,上下文c){
//在这里,我需要设置arr的位置
this.ctxt=c;
this.mSummerJobDB=summerJobDB;
mAllPlacesNames=summerjobd.getAllPlacesNames();
}
@凌驾
public int getCount(){
返回mallsplacesnames.size();
}
@凌驾
公共对象getItem(int位置){
返回mallsplacesnames.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//创建要在listview(视图)中显示的单元格
返回null;
}
}
在MainActivity上,您可以创建适配器,提供对DatabaseHelper的引用。您可以使用CursorAdapter,它是为连接到数据库而设计的,可以避免太多额外的工作
public class StringArrayAdapter extends BaseAdapter {
Context ctxt;
LayoutInflater myInflator;
DataBaseHelper mSummerJobDB;
private ArrayList<String> mAllPlacesNames;
public StringArrayAdapter(DataBaseHelper summerJobDB, Context c) {
// here I need to set places to arr
this.ctxt = c;
this.mSummerJobDB = summerJobDB;
mAllPlacesNames = summerJobDB.getAllPlacesNames();
}
@Override
public int getCount() {
return mAllPlacesNames.size();
}
@Override
public Object getItem(int position) {
return mAllPlacesNames.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// create a cell to be displayed inside listview (a view)
return null;
}
}