这是我使用预先填充数据库的主要活动。我试图在列表视图中显示行。我的数据库中有五列,我希望显示除id之外的所有列。
public class MainActivity extends Activity implements OnClickListener{
private IngHelper dbIngHelper = null;
private Cursor curname=null;
private SimpleCursorAdapter adapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView myList;
myList = (ListView)findViewById(R.id.listView1);
dbIngHelper = new IngHelper(this);
dbIngHelper.createDB();
dbIngHelper.openDB();
fillData();
}
public void fillData(){
//ListView myList;
ListView myList;
myList = (ListView)findViewById(R.id.listView1);
curname=dbIngHelper.getCursor();
startManagingCursor(curname);
String[] columns = new String[] {
IngHelper.COLUMN_ID,
IngHelper.COLUMN_NAME,
IngHelper.COLUMN_NATURE,
IngHelper.COLUMN_SECTION,
IngHelper.COLUMN_PENALTY
};
int[] to = new int[] {
R.id.id,
R.id.name,
R.id.nature,
R.id.penalty,
R.id.section
};
adapter = new SimpleCursorAdapter(
this, R.layout.activity_main,
curname,
columns,
to);
myList.setAdapter(adapter);
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view,int position, long id) {
//Get the cursor, positioned to the corresponding row in the result set
// Cursor cursor = (Cursor) listView.getItemAtPosition(position);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class IngAdapter extends CursorAdapter{
@SuppressWarnings("deprecation")
IngAdapter(Cursor c){
super(MainActivity.this,c);
}
@Override
public void bindView(View row, Context ctxt, Cursor c) {
// TODO Auto-generated method stub
IngHolder holder=(IngHolder)row.getTag();
holder.populateFrom(c,dbIngHelper);
}
@Override
public View newView(Context cxtx, Cursor c, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inf = getLayoutInflater();
View row=inf.inflate(R.layout.row, parent, false);
IngHolder holder = new IngHolder(row);
row.setTag(holder);
return(row);
}
}
static class IngHolder{
private TextView name=null;
// private TextView nature=null;
//private TextView section=null;
//private TextView penalty=null;
IngHolder(View row){
name=(TextView)row.findViewById(R.id.name);
}
void populateFrom(Cursor c,IngHelper r){
name.setText(r.getName(c));
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
这是数据库类IngHelper.java
public class IngHelper extends SQLiteOpenHelper{
private static final String DATABASE_PATH = "//data//data//com.example.db//databases//";
private static final String DATABASE_NAME = "traffic.sqlite";
private static final int SCHEMA_VERSION = 1;
public static final String TABLE_NAME="mumbai";
public static final String COLUMN_ID="_id";
public static final String COLUMN_NAME="name";
public static final String COLUMN_NATURE="nature";
public static final String COLUMN_SECTION="section";
public static final String COLUMN_PENALTY="penalty";
public SQLiteDatabase dbSqlite;
private final Context myCnt;
public IngHelper (Context context)
{
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
this.myCnt=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public void createDB(){
boolean dbExist = DBExist();
if(!dbExist){
this.getReadableDatabase();
copyDBFromResource();
}
}
private boolean DBExist(){
SQLiteDatabase db = null;
try{
String dbPath = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
}catch(SQLiteException e){
Log.e("SqlHelper", "db nt found");
}
return db != null ? true : false;
}
private void copyDBFromResource(){
InputStream iS =null;
OutputStream oS = null;
String dbFilePath = DATABASE_PATH + DATABASE_NAME;
try{
iS = myCnt.getAssets().open(DATABASE_NAME);
oS = new FileOutputStream(dbFilePath);
byte[] buffer = new byte[1024];
int length;
while((length=iS.read(buffer))>0){
oS.write(buffer, 0, length);
}
oS.flush();
oS.close();
iS.close();
}catch(IOException e){
throw new Error("Prob copyin db from resource file.");
}
}
public void openDB()throws SQLException {
String myPath = DATABASE_PATH + DATABASE_NAME;
dbSqlite = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
// TODO Auto-generated method stub
if(dbSqlite != null)
{
dbSqlite.close();
}
super.close();
}
public Cursor getCursor(){
SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder();
qBuilder.setTables(TABLE_NAME);
String[] asColmToRetrn = new String[] {COLUMN_ID, COLUMN_NAME, COLUMN_NATURE, COLUMN_SECTION, COLUMN_PENALTY};
Cursor mCur = qBuilder.query(dbSqlite, asColmToRetrn, null, null, null, null, null);
if (mCur != null) {
mCur.moveToFirst();
}
return mCur;
}
public String getName(Cursor c){
return(c.getString(1));
}
}
有两个xml文件。这是activity_main.xml ...它有一个列表View。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" >
</ListView>
</RelativeLayout>
另一个布局是row.xml文件。这里我为数据库中的每一列创建一个文本视图。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="TextView" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="TextView" />
<TextView
android:id="@+id/nature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="TextView" />
<TextView
android:id="@+id/section"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="TextView" />
<TextView
android:id="@+id/penalty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="TextView" />
</LinearLayout>
我得到一个空白列表作为输出,每行有一个列表条目,但无法看到数据。请提出一些解决问题的建议