0

我有 3 个活动:表单活动、数据库助手和主活动。Main Activity 应该显示存储在数据库中的“compNameAdd”的列表视图。当我尝试通过 setAdapter 填充列表视图时

    DataBase dbc = new DataBase(NituachActivity.this);
    dbc.open();

    String[] cs = new String[] { DataBase.RAW_COMPNAMEADD };

    Log.d(TAG, cs.toString());

     blv.setAdapter(new ArrayAdapter<String>(this,
     android.R.layout.simple_list_item_1, cs));

    dbc.close();

我得到了列的名称,而不是列中的值。问题是什么?

数据库助手:

package com.nituach.nituach;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBase {

public static final String RAW_ID = "_id";
public static final String RAW_COMPNAMEADD = "compNameAdd";
public static final String RAW_CASH = "etCash";
public static final String RAW_CUSTOMERS = "castumers";
public static final String RAW_STOCK = "stock";
public static final String RAW_FIXED_ASSETS = "fixedAssets";
public static final String RAW_ACCUMULTED_DEPRECIATION = "accumultedDepreciation";
public static final String RAW_BONDS = "bonds";
public static final String RAW_LOANS = "loans";
public static final String RAW_EQUITY = "equity";

public static final String RAW_COMPNAME_ID = "_id";
public static final String RAW_COMPNAME = "compName";

private static final String DATABASE_NAME = "ProsseDatBase";
static final String DATABASE_TABLE = "data";
static final String DATABASE_TABLE_SETTINGS = "compNameConnector";

private static final int DATABASE_VERSION = 1;

private ProsseDatBase thdb;
private static Context tcontext;
private SQLiteDatabase tdb;

private static class ProsseDatBase extends SQLiteOpenHelper {

    public ProsseDatBase(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String pdb = "CREATE TABLE  " + DATABASE_TABLE + " ( " + RAW_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
RAW_COMPNAMEADD
                + " TEXT, " + RAW_CASH + " TEXT, " + RAW_CUSTOMERS
                + " TEXT, " + RAW_STOCK + " TEXT, " + RAW_FIXED_ASSETS
                + " TEXT, " + RAW_ACCUMULTED_DEPRECIATION + " TEXT, "
                + RAW_BONDS + " TEXT, " + RAW_LOANS + " TEXT, " + 
RAW_EQUITY
                + " TEXT);";
        db.execSQL(pdb);

        String cndb = "CREATE TABLE  " + DATABASE_TABLE_SETTINGS + " ( "
                + RAW_COMPNAME_ID + " INTEGER PRIMARY KEY 
AUTOINCREMENT, "
                + RAW_COMPNAME + " TEXT);";
        db.execSQL(cndb);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_SETTINGS);
        onCreate(db);
    }
}

public DataBase(Context c) {
    tcontext = c;
}

public DataBase open() throws SQLiteException {
    thdb = new ProsseDatBase(tcontext);
    tdb = thdb.getWritableDatabase();
    return this;
}

public SQLiteDatabase getDatabase() {
    return tdb;
}

public void close() {
    tdb.close();
}

public long createEntry(String cm, String ch, String sk, String ot,
        String fs, String an, String bs, String ls, String ey) {
    ContentValues cv = new ContentValues();
    cv.put(RAW_COMPNAMEADD, cm);
    cv.put(RAW_CASH, ch);
    cv.put(RAW_CUSTOMERS, ot);
    cv.put(RAW_STOCK, sk);
    cv.put(RAW_FIXED_ASSETS, fs);
    cv.put(RAW_ACCUMULTED_DEPRECIATION, an);
    cv.put(RAW_BONDS, bs);
    cv.put(RAW_LOANS, ls);
    cv.put(RAW_EQUITY, ey);

    return tdb.insert(DATABASE_TABLE, null, cv);
}

public String getData() {
    String[] columns = new String[] { RAW_COMPNAMEADD };
    Cursor c = tdb.query(DATABASE_TABLE, columns, null, null, null, null,
            null);
    String results = "";

    int iCM = c.getColumnIndex(RAW_COMPNAMEADD);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        results = results + c.getString(iCM) + "\n";
    }
    return results;
}
}

主要活动:

包 com.nituach.nituach;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class NituachActivity extends Activity implements OnClickListener {

Button addNewBuisness;
ListView blv;
TextView tay;
String tyy;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final String TAG = NituachActivity.class.getSimpleName();
    addNewBuisness = (Button) findViewById(R.id.btnAddNewBuisness);
    addNewBuisness.setOnClickListener(NituachActivity.this);
    blv = (ListView) findViewById(R.id.listView1);
    tay = (TextView) findViewById(R.id.TempArray);

    Log.d(TAG, "All Variables was created");
    DataBase dbc = new DataBase(NituachActivity.this);
    dbc.open();

    String[] cs = new String[] { DataBase.RAW_COMPNAMEADD };

    Log.d(TAG, cs.toString());

     blv.setAdapter(new ArrayAdapter<String>(this,
     android.R.layout.simple_list_item_1, cs));

    dbc.close();


}

public void onClick(View v) {
    Intent addnbIntent = new Intent(NituachActivity.this,
            AddNewBuisness.class);
    NituachActivity.this.startActivity(addnbIntent);

}
}
4

1 回答 1

3

您实际上并没有调用 getData() 从您的主要活动中获取您的值。

首先,将方法签名 getData() 更改为返回字符串数组:

public List<String> getData() {
    String[] columns = new String[] { RAW_COMPNAMEADD };
    Cursor c = tdb.query(DATABASE_TABLE, columns, null, null, null, null,
        null);
    String results = "";
    List<String> results = new ArrayList<String>();
    int iCM = c.getColumnIndex(RAW_COMPNAMEADD);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        results.add(c.getString(iCM);
    }
    return results;
}

然后在您的主要活动中,更改为:

DataBase dbc = new DataBase(NituachActivity.this);
dbc.open();

List<String> cs = dbc.getData();
 blv.setAdapter(new ArrayAdapter<String>(this,
 android.R.layout.simple_list_item_1, cs));
于 2012-08-04T15:31:38.193 回答