请看下面的代码
数据库处理程序.java
用于检索数据的代码
public List<String> getAllBranches() {
// TODO Auto-generated method stub
String selectQuery = "select city from BranchNetwork";
Cursor cursor = database.rawQuery(selectQuery, null);
List <String>branches = new ArrayList<String>();
if(cursor.isFirst())
{
do
{
}
while(cursor.moveToNext());
Toast.makeText(context, "Data Retrieved: "+branches.get(1), Toast.LENGTH_LONG).show();
}
return branches;
}
用于插入数据的代码
public String insertData(String city, String streetAddress,String phoneNumber1, String phoneNumber2, String email)
{
String insertQuery = "insert into BranchNetwork ('city','streetAddress','phoneNumber1','phoneNumber2','email') values('"+city+"','"+streetAddress+"','"+phoneNumber1+"','"+phoneNumber2+"','"+email+"');";
try
{
database.execSQL(insertQuery);
return "Data Successfully Inserted";
}
catch(Exception e)
{
Toast.makeText(context, "Exception: "+e.getMessage(), Toast.LENGTH_LONG).show();
return "Data Insertion Failed";
}
}
表单.java
第一个活动。在这里,我检索数据并插入数据。以下是我检索数据的方式
public class Form extends Activity {
private List<String>branches = new ArrayList<String>();
private DatabaseConnector databaseConnector;
String[]arr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
databaseConnector = DatabaseHandler.getInstance(this);
databaseConnector.createConnection();
branches = databaseConnector.getAllBranches();
arr = branches.toArray(new String[branches.size()]);
}
}
但是,我什么都得不到!在 DatabaseHandler.java 中,我创建了一个Toast
在检索代码时应该生成消息的位置。但它没有产生任何东西!我正在将数据加载到 aListView
但它也是空的!为什么是这样?请帮忙!
PS:下面是DatabaseConnector.java,DatabaseHandler的插入面
package com.example.esoftcallmanager;
import java.util.List;
import android.content.Context;
public interface DatabaseConnector
{
public void createConnection();
public void closeConnection();
public String getPhoneNumber();
public String insertData(String city, String streetAddress, String phoneNumber1, String phoneNumber2, String email);
public List<String>getAllBranches();
}