I created a database for my application. I am trying to insert 3 values. But my problem is when trying to insert second values,they go to first column. And this value from user"s phone numbers( so from content provider). When I trying to launch my code all column is set up at first column except or a column. I want to set theses values to 3 different column. It's showing me 10 result but I selected 5 name. I want to show 5 results. Other 5 results go to column 2. I am using custom listview at second activity. On the other hand, they may loaded at first column but I can not see my number values I am beginner at database.
I want to just like :
ID name number oran
1 Ahmet 5555 50
2 Mehmet 6666 50
3 Veli 3333 50
4 Can 2222 50
5 Zehra 11111 50
etc.
My databaseHelper :
class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";
private static final String TABLE_NAME = "people_table";
private static final String COL1 = "ID";
private static final String COL2 = "name";
private static final String COL3 = "number";
private static final String COL4 = "oran";
public DatabaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + COL2 + " TEXT,"+
COL3 + " TEXT,"+ COL4 + " TEXT)";
db.execSQL(createTable);
}
// COL2 +" TEXT)0" col3 yerine 2 koyulması gerekiyor.
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item , String number ) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item );
contentValues.put(COL3 , number);
contentValues.put(COL4, "50");
Log.d(TAG, "addData: Adding " + item + " to " + TABLE_NAME);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
When user clicked saving data button :
for (String newEntry: selectedlist)
AddData(newEntry,null); {
}
for (String number : selectedlistarama)
AddData(null , number); {
}
Adding values :
private void AddData(String newEntry ,String number) {
boolean insertData = mDatabaseHelper.addData(newEntry ,number);
if (insertData) {
Cursor data = mDatabaseHelper.getData();
sayı = data.getCount();
// toastMessage("Data Successfully Inserted!");
Toast.makeText(this,String.valueOf(sayı), Toast.LENGTH_SHORT).show();
} else {
toastMessage("Something went wrong");
}
}
Getting values from database at Second Activity:
while(data.moveToNext()) {
listData.add(data.getString(1));
listDatanumber.add(data.getString(2));
listDataoran.add(data.getString(3));
}
My Custom_adapter :
namesbox.setText(listData.get(position));
Assumptions:
selectedlist
is the ArrayList of String
for COL_2 (name).
selectedlistarama
is the ArrayList of String
for COL_3 (number).
Note: if for number you really want to keep numbers use ArrayList<Integer>
and store int values.
I also assume that there is the same number of items in both lists so the first item of selectedlist corrsponds with the first item of selectedlistarama and so on.
Then, to call addData(String String)
you would do like this:
for(int i=0; i<selectedlist.size(); i++){
String name = selectedlist.get(i);
String number = selectedlistarama.get(i);
addData(name, number);
}