将垃圾值从Database加载到Listview

时间:2016-03-27 10:53:11

标签: android arraylist android-sqlite android-arrayadapter

我想将数据库数据加载到arraylist,然后在按钮单击时从arraylist加载到listview,但是当我单击按钮时,它会将垃圾数据加载到listview。这是我的代码。我究竟做错了什么?

        save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(Bookmarks.this, android.R.layout.simple_list_item_1, list);
                    String query = "Insert into WEB values('" + title.getText().toString() + "','" + address.getText().toString() + "')";
                    db.execSQL(query);
                    Toast.makeText(getApplicationContext(), "Bookmark Inserted", Toast.LENGTH_LONG);
               try {
                    Cursor c = db.rawQuery("SELECT Title FROM WEB", null);
                    c.moveToFirst();
                do {
                    String check = c.getString(c.getColumnIndex("Title"));
                    list.add(check);
                    }
                while (c.moveToNext());
                    lv.setAdapter(adapter);
                    }
                catch (Exception ex) {
                    Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG);
            }
        }
    });

这是布局的截图...

enter image description here

0 个答案:

没有答案