android.widget.ListView无法使用SQLite导入的数据库强制转换为android.widget.TextView

时间:2012-03-29 04:25:14

标签: android database sqlite listview textview

我正在SQLite数据库浏览器中导入自定义数据库。它由1个DB和3个表组成。

起初我得到的错误是表格和列显然不存在,所以我进入了DDMS文件浏览器,删除了那里的数据库并重新启动了我的程序。但是,现在我得到一个新的错误android.widget.ListView无法强制转换为android.widget.TextView

这里有一些代码:

public class SearchbyAlpha extends Activity {
private IngredientHelper dbIngredientHelper = null;
private Cursor ourCursor = null;
private IngredientAdapter adapter=null;

public void onCreate(Bundle savedInstanceState){
    try{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewportingredients);

    ListView myListView = (ListView)findViewById(R.id.myListView);

    dbIngredientHelper = new IngredientHelper(this);
    dbIngredientHelper.createDatabase();
    dbIngredientHelper.openDataBase();
    ourCursor = dbIngredientHelper.getCursor();
    startManagingCursor(ourCursor);
    adapter=new IngredientAdapter(ourCursor);
    myListView.setAdapter(adapter);
}
catch (Exception e)
{
    Log.e("ERROR", "ERROR IN CODE: " + e.toString());
    e.printStackTrace();
}
}

class IngredientAdapter extends CursorAdapter{
IngredientAdapter(Cursor c){
    super(SearchbyAlpha.this, c);
}
@Override
public void bindView(View row, Context ctxt, Cursor c){
    IngredientHolder holder=(IngredientHolder)row.getTag();
    holder.populateFrom(c, dbIngredientHelper);
}

@Override
public View newView(Context ctxt, Cursor c, ViewGroup parent) {
    LayoutInflater inflater=getLayoutInflater();
    View row=inflater.inflate(R.layout.viewportingredients, parent, false);
    IngredientHolder holder=new IngredientHolder(row);
    row.setTag(holder);
    return(row);
}
}

static class IngredientHolder{
private TextView name=null;

IngredientHolder(View row){
    name = (TextView) row.findViewById(R.id.myListView);
}

void populateFrom(Cursor c, IngredientHelper r){
    name.setText(r.getName(c));
}
}
}

这是我的DB助手:

public SQLiteDatabase dbSqlite;

private final Context myContext;

public IngredientHelper(Context context) {
    super(context, DATABASE_NAME, null, SCHEMA_VERSION);
    this.myContext = context;
}

@Override
public void onCreate(SQLiteDatabase db){

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){

}

public void createDatabase(){
    createDB();
}

private void createDB(){
    boolean dbExist = DBExists();

    if (!dbExist){
        this.getReadableDatabase();
        copyDBFromResource();
    }
}

private boolean DBExists() {
    SQLiteDatabase db = null;

    try {
        String databasePath = DATABASE_PATH + DATABASE_NAME;
        db = SQLiteDatabase.openDatabase(databasePath, null, SQLiteDatabase.OPEN_READWRITE);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);
        db.setVersion(1);
    } catch (SQLiteException e){
        Log.e("SqlHelper", "database not found");
    }

    if (db != null){
        db.close();
    }

    return db != null ? true : false;
}

private void copyDBFromResource(){
    InputStream inputStream = null;
    OutputStream outStream = null;
    String dbFilePath = DATABASE_PATH + DATABASE_NAME;

    try{
        inputStream = myContext.getAssets().open(DATABASE_NAME);

        outStream = new FileOutputStream(dbFilePath);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outStream.write(buffer, 0, length);
        }

        outStream.flush();
        outStream.close();
        inputStream.close();
        } catch (IOException e){
            throw new Error("Problem copying database from resource file.");
    }
}

public void openDataBase() throws SQLException {
    String myPath = DATABASE_PATH + DATABASE_NAME;
    dbSqlite = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}

@Override
public synchronized void close(){
    if(dbSqlite != null)
    {
        dbSqlite.close();
    }
    super.close();
    }

public Cursor getCursor(){
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(TABLE_NAME);
    String[] asColumnsToReturn = new String[] { COLUMN_ID, COLUMN_TITLE, COLUMN_TITLE2};
    Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null, null, null, null, COLUMN_TITLE);
    return mCursor;
            }

public String getName(Cursor c){
    return(c.getString(1));
}
}

我在“viewportingradients.xml”文件中尝试过ListViews和TextViews的组合无济于事。我怀疑错误在于以下内容:

static class IngredientHolder{
private TextView name=null;

IngredientHolder(View row){
    name = (TextView) row.findViewById(R.id.myListView);
}

void populateFrom(Cursor c, IngredientHelper r){
    name.setText(r.getName(c));
}
}

但由于我是Android新手,我真的不知道如何解决这个问题。提前谢谢。

编辑:我打算将这个DB表放入ListView,每个条目都可以点击,这会将点击的条目写入db中的另一个表。

1 个答案:

答案 0 :(得分:0)

尝试从表中选择*这应该有助于否则回发aa