I have code, and it got a force close all day. I dont know what is the problem, but when i pick up this 2 line its running (but i have not a list):
1. SimpleCursorAdapter sets = new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
2. setListAdapter(sets);
Here is the prgram:
The LoVoMa.java
Using java Syntax Highlighting
- ...
- public class LoVoMa extends ListActivity{
- private DbAdapter mDbHelper;
- private Cursor c;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.notes_list);
- mDbHelper = new DbAdapter(this);
- mDbHelper.open();
- fillData();
- registerForContextMenu(getListView());
- }
- private void fillData() {
- // Get all of the rows from the database and create the item list
- c = mDbHelper.fetchAllNotes();
- startManagingCursor(c);
- // Create an array to specify the fields we want to display in the list (only TITLE)
- String[] from = new String[]{DbAdapter.KEY_MODEL};
- // and an array of the fields we want to bind those fields to (in this case just text1)
- int[] to = new int[]{R.id.text122};
- // Now create a simple cursor adapter and set it to display
- SimpleCursorAdapter sets = new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
- //setListAdapter(sets);
- }...
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
The DbAdapter.java
Using java Syntax Highlighting
- public class DbAdapter {
- public static final String KEY_ID = "Id";
- public static final String KEY_MODEL = "Model";
- public static final String KEY_COMPANY = "Company";
- private static final String TAG = "DbAdapter";
- private DatabaseHelper mDbHelper;
- private SQLiteDatabase db;
- private static final String DATABASE_CREATE =
- "CREATE TABLE IF NOT EXISTS my_table (Id INTEGER PRIMARY KEY AUTOINCREMENT, Company TEXT, Model TEXT);";
- private static final String DATABASE_NAME = "myDB";
- private static final String DATABASE_TABLE = "my_table";
- private static final int DATABASE_VERSION = 2;
- private final Context mCtx;
- private static class DatabaseHelper extends SQLiteOpenHelper {
- DatabaseHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(DATABASE_CREATE);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
- + newVersion + ", which will destroy all old data");
- db.execSQL("DROP TABLE IF EXISTS notes");
- onCreate(db);
- }
- }
- public DbAdapter(Context ctx) {
- this.mCtx = ctx;
- }
- public DbAdapter open() throws SQLException {
- mDbHelper = new DatabaseHelper(mCtx);
- db = mDbHelper.getWritableDatabase();
- return this;
- }
- public void close() {
- mDbHelper.close();
- }
- public long createNote(String Model, String Company) {
- ContentValues initialValues = new ContentValues();
- initialValues.put(KEY_MODEL, Model);
- initialValues.put(KEY_COMPANY, Company);
- return db.insert(DATABASE_TABLE, null, initialValues);
- }
- public boolean deleteNote(long rowId) {
- return db.delete(DATABASE_TABLE, KEY_ID + "=" + rowId, null) > 0;
- }
- public Cursor fetchAllNotes() {
- return db.query(DATABASE_TABLE, new String[] {KEY_MODEL}, null, null, null, null, null);
- }
- ....
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
The notes_list.xml
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <ListView android:id="@+id/android:list"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <TextView android:id="@+id/android:empty"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="No Notes!"/>
- <TextView
- android:id="@+id/text123"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="40px"/>
- </LinearLayout>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
The notes_row.xml
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <TextView
- android:id="@+id/text122"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="40px"/>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
Please help me.
Thanks,
Leslie.

