lundi 12 octobre 2015

Rating bar + android sqlite

Currently my codes is working except for 2 issues.

Issue : 1. Only able to add name and rating once

2. Can only update the ratings not the name

Hi all if anyone can take sometime to take a lot will appreciate it.

If not give me some links that is related, I have been trying the code for 2 days already and it still gives me the same problem, now I can only come here and hope somebody can at least help me

MainActivity.java

        lvInfo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                txtName.setText(results.get(position).getName());
               ratingBar.setRating(results.get(position).getRating());

            }
        });
        DisplayAll();
    }

    public void DisplayAll() {
        results = db.getAllResults();
        ArrayofName.clear();
        for (Result rs : results) {
            ArrayofName.add(rs.getId() + ".\t" + rs.getName() + ".\t" + rs.getRating());
        }
        dbAdapter.notifyDataSetChanged();
        txtName.setText("");
        ratingBar.setRating(5);
    }

    public void AddResult(View view) {
        if ((txtName.getText().length() == 0) || (ratingBar.getRating() == 0) )
            Toast.makeText(this, "Please enter the name and rating to be added!",  Toast.LENGTH_LONG).show();
        else {
            Result tmp = new Result(txtName.getText().toString(),
                    ratingBar.getRating() );

            db.addResult(tmp);
            DisplayAll();
            Toast.makeText(this, "Results Added!", Toast.LENGTH_LONG).show();
        }
    }

    public void UpdateResult(View view) {
        if ((txtName.getText().length() == 0) || (ratingBar.getRating() == 0) )
            Toast.makeText(this, "Please select result to be updated!", Toast.LENGTH_LONG).show();
        else {
            Result tmp = new Result(txtName.getText().toString(),ratingBar.getRating() );

            db.updateResult(tmp);
            DisplayAll();
            Toast.makeText(this, "Result Updated!", Toast.LENGTH_LONG).show();
        }
    }
    }

DatabaseHandler.java

        //creating Tables
        @Override
        public void onCreate(SQLiteDatabase db){
            String CREATE_RESULTS_TABLE = "CREATE TABLE " + TABLE_RESULT + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME +
                    " TEXT,"+ KEY_RATING + " REAL)";
            db.execSQL(CREATE_RESULTS_TABLE);
        }

        public void addResult(Result result){
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(KEY_NAME, result.getName());  //get name
            values.put(KEY_RATING, result.getRating());  //get rating
            db.insert(TABLE_RESULT, null, values);
            db.close();
        }

     public Result getResult(int id) {
            SQLiteDatabase db = this.getReadableDatabase();

         //build query
            Cursor cursor = db.query(TABLE_RESULT, new String[]{KEY_ID,
                            KEY_NAME, KEY_RATING}, KEY_ID + "=?",
                    new String[]{String.valueOf(id)}, null, null, null, null);

            if (cursor != null)  //if get results get the first one
                cursor.moveToFirst();
         //build result project
         Result result;
          result = new Result(Integer.parseInt(cursor.getString(0)),
                    cursor.getString(1), Float.parseFloat(cursor.getString(2)));
            return result;
        }

      public List<Result> getAllResults() {
            List<Result> resultList = new ArrayList<Result>();
            String selectQuery = "SELECT * FROM " + TABLE_RESULT;
            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);
            //looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {
                    Result result = new Result();
                    result.setId(Integer.parseInt(cursor.getString(0)));
                    result.setName(cursor.getString(1));
                    result.setRating(Float.parseFloat(cursor.getString(2)));
                    resultList.add(result);
                } while (cursor.moveToNext());
            }
            return resultList;
        }

        public int updateResult(Result result){
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(KEY_RATING ,result.getRating());
            values.put(KEY_NAME , result.getName());

            //updating row
            return db.update(TABLE_RESULT, values, KEY_NAME + " = ?", new String[]{String.valueOf(result.getName())});
        }

        public int getResultsCount(){
            String countQuery = "SELECT * FROM " + TABLE_RESULT;
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor = db.rawQuery(countQuery, null);
            cursor.close();
            return cursor.getCount();
        }

    }

Aucun commentaire:

Enregistrer un commentaire