0

I'm still fairly new to Android Studio and I have looked at this question in different forums but I haven't exactly found one that pertains to my problem specifically (or I'm not experienced enough to recognize it at least.)

I have created an activity where some one enters 3 different words into 3 different EditText views and those 3 separate values are supposed to be inserted into "column 1" or "COL_1" of the database upon creation, and of course create another intent and move to the next activity after checking/proving that the values were inserted to the database.

My problem is that ONLY THE THIRD word is entered as a row (or record) into the database and not the first 2. Sorry like I said I am novice, what am I missing?:

EDIT TEXT ACTIVITY

package com.example.jonathan.om11;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ContributionsActivity extends AppCompatActivity {

    Button goToMainPageBtn;
    ContribDatabase contribDatabase;
    EditText contribution1,contribution2,contribution3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contributions);

        contribDatabase = new ContribDatabase(this);
        contribution1 = (EditText) findViewById(R.id.contribution1);
        contribution2 = (EditText) findViewById(R.id.contribution2);
        contribution3 = (EditText) findViewById(R.id.contribution3);

        goToMainPageBtn = (Button) findViewById(R.id.goToMainPageBtn);
        goToMainPageBtn.setOnClickListener(new View.OnClickListener(){
            public void onClick (View v){
                boolean isInserted = contribDatabase.insertData(
                        contribution1.getText().toString(),
                        contribution2.getText().toString(),
                        contribution3.getText().toString());
                Toast.makeText(ContributionsActivity.this, "Thank you for contributing, enjoy!", Toast.LENGTH_SHORT).show();
                if(isInserted == true) {
                    toContribPage(v);
                } else {
                    Toast.makeText(ContributionsActivity.this, "Oops! Contributions did not save, try again please!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    public void toContribPage(View v) {
        Intent i = new Intent("android.intent.action.TabbedView");
        startActivity(i);
    }
}

SQLITEOPENHELPER CLASS

package com.example.jonathan.om11;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by Jonathan on 2/10/18.
 */

public class ContribDatabase extends  SQLiteOpenHelper {

    public static final String DATABASE_NAME = "Contributions.db";
    public static final String TABLE_NAME = "Contributions_table";
//    public static final String COL_1 = "ID";
    public static final String COL_1 = "CONTRIBUTIONS";

    public ContribDatabase(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
//        db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT, CONTRIBUTIONS TEXT)");
          db.execSQL("create table " + TABLE_NAME +" (CONTRIBUTIONS TEXT)");

    }

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

    public boolean insertData(String contribution1, String contribution2, String contribution3) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues =  new ContentValues();
        contentValues.put(COL_1,contribution1);
        contentValues.put(COL_1,contribution2);
        contentValues.put(COL_1, contribution3);

        //Check for if data is inserted or not
        long result = db.insert(TABLE_NAME,null,contentValues);
        if(result == -1) {
            return false;
        } else {
            return true;
        }
    }

//    public Cursor getAllData() {
//        SQLiteDatabase db = this.getWritableDatabase();
//        Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
//        return res;
//    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Jonathan
  • 487
  • 1
  • 9
  • 19
  • `ContentValues contentValues = new ContentValues(); contentValues.put(COL_1,contribution1); contentValues.put(COL_1,contribution2); contentValues.put(COL_1, contribution3);` you are adding 3 values in same coloumn. create 3 coloumn for each contributions . – KuLdip PaTel Feb 12 '18 at 08:22

3 Answers3

0

Only third is inserting because you are overriding same key for 3 different values.

    contentValues.put(COL_1,contribution1);
    contentValues.put(COL_1,contribution2);
    contentValues.put(COL_1, contribution3);

Here you are overriding COL_1 3 times.

Your have to create another 2 columns in Database table or concatenate other 2 values with some separator string so that you can later split them.

Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
0

There is a problem in your code. The method insertData has some problems. Replace your code with below one

public boolean insertData(String contribution1, String contribution2, String contribution3) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues =  new ContentValues();
    ContentValues contentValues2 =  new ContentValues();
    ContentValues contentValues3 =  new ContentValues();
    contentValues.put(COL_1,contribution1);
    contentValues2.put(COL_1,contribution2);
    contentValues3.put(COL_1, contribution3);

    //Check for if data is inserted or not
    long result = db.insert(TABLE_NAME,null,contentValues);
    long result2 = db.insert(TABLE_NAME,null,contentValues2);
    long result3 = db.insert(TABLE_NAME,null,contentValues3);
    if(result == -1 || result2 == -1 || result3 == -1) {
        return false;
    } else {
        return true;
    }
}

It was happening because rather inserting 3 different values you were mapping different values in a same object. Hope that helps.

Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
0

You are entering data 3 times to the same key entry, and that is why you get the last entered data.. you have 3 different data entries - each for every Edittext so you will need to have 3 different keys, like in my example:

 contentValues.put(COL_1,contribution1);
 contentValues.put(COL_1,contribution2);
 contentValues.put(COL_1, contribution3);

You will need to have something like this:

contentValues.put(COL_1,contribution1);
contentValues.put(COL_2,contribution2);
contentValues.put(COL_3, contribution3);
DPE
  • 218
  • 5
  • 15