SQLite
 sql >> Teknologi Basis Data >  >> RDS >> SQLite

Menampilkan data dari database menggunakan adaptor dasar dan tampilan daftar

Buat basis data

 public class DatabaseHandler extends SQLiteOpenHelper {

        //Database Version
        private static final int DATABASE_VERSION = 1;
        //Database Name
        private static final String DATABASE_NAME = "Test";
        //Table Name
        private static final String TABLE_TEST = "TestTable";
        //Column Name
        private static final String KEY_ID = "id";
        private static final String KEY_NAME = "name";
        private static final String KEY_AGE = "age";

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

        //Create Table
        @Override
        public void onCreate(SQLiteDatabase db) {
            String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_TEST + "("
                    + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                    + KEY_AGE + " TEXT" + ")";
            db.execSQL(CREATE_CONTACTS_TABLE);
        }

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

        //Insert Value
        public void adddata(Context context,String movieId,String songId) {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(KEY_NAME, movieId);
            values.put(KEY_AGE, songId); 
            db.insert(TABLE_TEST, null, values);
            db.close(); 
        }

        //Get Row Count
        public int getCount() {
            String countQuery = "SELECT  * FROM " + TABLE_TEST;
            int count = 0;
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor = db.rawQuery(countQuery, null);
            if(cursor != null && !cursor.isClosed()){
                count = cursor.getCount();
                cursor.close();
            }   
            return count;
        }

        //Delete Query
        public void removeFav(int id) {
            String countQuery = "DELETE FROM " + TABLE_TEST + " where " + KEY_ID + "= " + id ;
            SQLiteDatabase db = this.getReadableDatabase();
            db.execSQL(countQuery);
        }

        //Get FavList
        public List<FavoriteList> getFavList(){
            String selectQuery = "SELECT  * FROM " + TABLE_TEST;
            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);
            List<FavoriteList> FavList = new ArrayList<FavoriteList>();
            if (cursor.moveToFirst()) {
                do {
                    FavoriteList list = new FavoriteList();
                    list.setId(Integer.parseInt(cursor.getString(0)));
                    list.setName(cursor.getString(1));
                    list.setAge(cursor.getString(2));
                    FavList.add(list);
                } while (cursor.moveToNext());
            }
            return FavList;
        }
}

dan buat tampilan daftar khusus menggunakan adaptor dasar seperti di bawah ini

public class ViewAdapter extends BaseAdapter {

        LayoutInflater mInflater;

        public ViewAdapter() {
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return favoriteList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.listitem,null);
            }

            final TextView nameText = (TextView) convertView.findViewById(R.id.nameText);
            nameText.setText("Name : "+favoriteList.get(position).getName());
            final TextView ageText = (TextView) convertView.findViewById(R.id.ageText);
            ageText.setText("Age : "+favoriteList.get(position).getAge());

            final Button edit = (Button) convertView.findViewById(R.id.edit);
            edit.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    final Dialog dialog = new Dialog(context);
                    dialog.setContentView(R.layout.row);
                    dialog.setTitle("Add Data to Database");
                    final EditText name = (EditText) dialog.findViewById(R.id.name);
                    final EditText age = (EditText) dialog.findViewById(R.id.age);
                    Button Add = (Button) dialog.findViewById(R.id.Add);
                    Add.setText("Add");
                    Add.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(name.getText().toString() != null && name.getText().toString().length() >0 ){
                                if(age.getText().toString() != null && age.getText().toString().length() >0 ){
                                    db.updateRow(favoriteList.get(position).getId(), name.getText().toString(), age.getText().toString());
                                    favoriteList = db.getFavList();
                                    listView.setAdapter(new ViewAdapter());
                                    dialog.dismiss();
                                }else{
                                    Toast.makeText(getApplicationContext(), "Please Enter the Age", Toast.LENGTH_LONG).show();  
                                }
                            }else{
                                Toast.makeText(getApplicationContext(), "Please Enter the Name", Toast.LENGTH_LONG).show(); 
                            }
                        }
                    });
                    dialog.show();  
                }
            });
            final Button delete = (Button) convertView.findViewById(R.id.delete);
            delete.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    db.removeFav(favoriteList.get(position).getId());
                    notifyDataSetChanged();
                    favoriteList = db.getFavList();
                    listView.setAdapter(new ViewAdapter());
                }
            });
            return convertView;
        }
    }

Dapatkan data dari basis data:

favoriteList = db.getFavList();

akhirnya tambahkan adaptor

listView.setAdapter(new ViewAdapter());

Jika Anda membutuhkan bantuan lebih lanjut, silakan lihat Url di bawah ini

http://mylearnandroid.blogspot.in/2014/04/android-sqlite-with-custom-listview.html




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Ubah Nilai NULL ke Nilai Default Kolom saat Memasukkan Data di SQLite

  2. Bagaimana menangani nilai boolean di SQLite menggunakan Proksi JavaScript

  3. Tambahkan Kolom ke Tabel yang Ada di SQLite

  4. Haruskah ada satu SQLiteOpenHelper untuk setiap tabel dalam database?

  5. Peringatan waktu kompilasi Ruang Android tentang kolom dalam kunci asing bukan bagian dari indeks. Apa artinya?