Akhilesh Chobey Akhilesh Chobey - 5 months ago 11
Android Question

Android SQLite: Data being added again and again

If I type

1234
in the EditText, the
ListView
in
MainActivity
displays as:
1
12
123
1234
instead of just 1234.

MainActivity:

public class MainActivity extends AppCompatActivity {

ListView notesListView;
static ArrayList<String> notesArrayList;
static ArrayAdapter<String> adapter;


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

notesListView = (ListView) findViewById(R.id.notesListView);
notesArrayList = new ArrayList<String>();

Main2Activity.myDb = new DatabaseOperations(this);

if (Main2Activity.myDb != null) {
notesArrayList.clear();
Cursor result = Main2Activity.myDb.getData();
if (result.getCount() == 0) {
Log.i("Error", "error");
return;
}
while (result.moveToNext()) {
notesArrayList.add(result.getString(result.getColumnIndex("text")));
}
}

//notesArrayList.add("Akhilesh Chobey");
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, notesArrayList);
notesListView.setAdapter(adapter);

notesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
intent.putExtra("notePosition", i);
startActivity(intent);
}
});

notesListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {


return false;
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();
if (id == R.id.add_button) {
notesArrayList.add("");
}
Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
intent.putExtra("notePosition", notesArrayList.size() - 1);
startActivity(intent);


return true;

}


Main2Activity(EditText Activity):

public class Main2Activity extends AppCompatActivity implements TextWatcher {

static DatabaseOperations myDb;
EditText editNote;
int position;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
editNote = (EditText) findViewById(R.id.noteEditText);

myDb = new DatabaseOperations(Main2Activity.this);

Intent intent = getIntent();
position = intent.getIntExtra("notePosition", -1);
if(position != -1){

editNote.setText(MainActivity.notesArrayList.get(position));

}
editNote.addTextChangedListener(this);
}

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (myDb != null) {
boolean isInserted = myDb.insertData(editNote.getText().toString());
MainActivity.notesArrayList.set(position, String.valueOf(charSequence));
MainActivity.adapter.notifyDataSetChanged();
}

}


Database Helper Class:

public class DatabaseOperations extends SQLiteOpenHelper {

public static final String DatabaseName = "notes.db";
public static final String TableName = "notes";
public static final String Col1 = "text";

public DatabaseOperations(Context context) {
super(context, DatabaseName, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TableName + " (text TEXT PRIMARY KEY) ");
}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {

db.execSQL("DROP TABLE IF EXISTS " + TableName);
//onCreate(db);

}

public boolean insertData(String note){

SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Col1, note);

long result = db.insert(TableName, null, contentValues);
if(result == -1){
return false;
}else {
return true;
}
}

public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("select * from " + TableName, null);
return result;
}
}

Answer

This is happening because you're inserting the contents of the EditText during onTextChanged(). This means that every time the text changes, it makes a new database entry.

I recommend adding a button that confirms the contents as a database entry.