将数据库显示到listview中

时间:2017-04-26 12:47:26

标签: android database sqlite listview

我正在使用Android上的记事本应用程序。 所以我创建了一个数据库,然后保存了笔记。 这是数据库的代码:

public class Database extends SQLiteOpenHelper {


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE Note (id INTEGER PRIMARY KEY AUTOINCREMENT, Title TEXT NOT NULL, Content TEXT NOT NULL ");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXIST Note");

        onCreate(db);
    }

    public Database(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }




    public void addNote(String title, String content) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues cv = new ContentValues();
       // cv.put(, title);
        cv.put( "Title",title);
        cv.put("Content", subtitle);

       // db.insert("Note", null, cv);
        db.insert("Note",null,cv);

        db.close();


    }




    public Cursor getNotes2(SQLiteDatabase db) {
        /*Cursor c = db.query("Note", new String[] {"id", "Title"}, null, null, null, null, "id DESC");
        c.moveToFirst();
        return c;*/
         db = this.getWritableDatabase();
        String[]Pro={"id","Title","Content"};
        String Selection="Title" +"=?";
        String []SelectionsArgs={"My title "};
        String sort= id+" DESC";

        Cursor c= db.query("Note", Pro, Selection, SelectionsArgs, null, null, sort);
c.moveToFirst();
return c;
    }

    public Cursor getNote(SQLiteDatabase db, int id) {
        Cursor c = db.query("Note", new String[] {"Title", "Content"}, "id" + " = ?", new String[] { String.valueOf(id) }, null, null, null);
        c.moveToFirst();
        return c;
    }

    public void removeNote(int id) {
        SQLiteDatabase db = getWritableDatabase();
        db.delete("Note", "id" + " = ?", new String[] { String.valueOf(id) });
        db.close();
    }

    public void updateNote(String title, String content, String editTitle) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues cv = new ContentValues();
        cv.put("noteTitle", title);
        cv.put("noteContent", content);
        db.update("Note", cv, "id" + " LIKE '" +  editTitle +  "'", null);

        db.close();


    }
}

...

之后我在Main Activity上创建了一个listview,我想将数据库中的Notes显示到这个列表视图中..我有这个代码但它不起作用,当我运行应用程序时我觉得运行时发生错误,因此无法打开。我不知道问题出在哪里!? 这是:

public class Item {
    private int id;
    private String title;

    public Item(int id, String title) {
        this.id = id;
        this.title = title;
    }

    public int getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

}

..........................

 public class MainActivity extends AppCompatActivity {

        private ListView lv=null;
       private ArrayAdapter<String> ad;
        private Cursor notes;
        private Database dbhelper;

        private ArrayList<String> titles;
        private ArrayList<Item> items;

        private int position = 0;



        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            lv= (ListView) findViewById(R.id.lv);
            dbhelper = new Database(getApplicationContext(), "Note", null, 2);
           setNotes();
            this.registerForContextMenu(lv);


                }

            });



        }

        public void setNotes() {

            titles = new ArrayList<String>();
            items = new ArrayList<Item>();

           SQLiteDatabase db = dbhelper.getReadableDatabase();
           notes = dbhelper.getNotes2(db);
            db.close();

           if (notes.moveToFirst()) {
                do {
                    items.add(new Item(notes.getShort(0), notes.getString(1)));
                } while (notes.moveToNext());
            }


            for (Item i : items) {
                titles.add(i.getTitle());
            }

            ad = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, titles);
            lv.setAdapter(ad);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(getApplicationContext(), "Item clicked"+id, Toast.LENGTH_SHORT).show();
                }
            });

        }

0 个答案:

没有答案