How to detect a click event of a button outside another method - java

Please find below the code I have implemented.
I need to detect the click event of the button outside another method.
public List<CorrectAnswer> passSessionIdTogetResults(String id) {
db = this.getReadableDatabase();
List<CorrectAnswer> correctAnswerList = new ArrayList<>();
CorrectAnswer correctAnswer = new CorrectAnswer();
Cursor cursor2 = db.rawQuery("SELECT * FROM table_test_start WHERE test_start_id ="+id,null);
if(cursor2.moveToFirst()){
do{
String question_number= cursor2.getString(cursor2.getColumnIndex(ID_TEST));
String question_id = cursor2.getString(cursor2.getColumnIndex(QUESTION_ID_TEST_START));
String selected_option = cursor2.getString(cursor2.getColumnIndex(SELECTED_OPTION_TEST_START));
Cursor cursor = db.rawQuery("SELECT * FROM practice_questions WHERE question_content_id='"+question_id+"' AND question_answer='"+selected_option+"'", null);
if (cursor.moveToFirst()) {
String green = "green";
correctAnswer.setCorrect_anwer_green(green);
Log.d("DbHelper","Question ID "+question_id);
Log.d("DbHelper","Correct Answer "+green);
}else {
String red = "red";
correctAnswer.setWrong_answer_red(red);
Log.d("DbHelper","Question ID "+question_id);
Log.d("DbHelper","Wrong Answer " +red );
}
correctAnswer.setCorrect_id(question_number);
correctAnswer.setQuestion_number_id(question_id);
correctAnswer.setSelected_set_options(selected_option);
correctAnswerList.add(correctAnswer);
}while(cursor2.moveToNext());
}
return correctAnswerList;
}
Getting last value of and inserted for n times.

Try this
String question_number= cursor2.getString(cursor2.getColumnIndex(ID_TEST));
String question_id = cursor2.getString(cursor2.getColumnIndex(QUESTION_ID_TEST_START));
String selected_option = cursor2.getString(cursor2.getColumnIndex(SELECTED_OPTION_TEST_START));
Cursor cursor = db.rawQuery("SELECT * FROM practice_questions WHERE question_content_id='"+question_id+"' AND question_answer='"+selected_option+"'", null);
cursor.moveToFirst()
do{
String green = "green";
correctAnswer.setCorrect_anwer_green(green);
Log.d("DbHelper","Question ID "+question_id);
Log.d("DbHelper","Correct Answer "+green);
correctAnswer.setCorrect_id(question_number);
correctAnswer.setQuestion_number_id(question_id);
correctAnswer.setSelected_set_options(selected_option);
correctAnswerList.add(correctAnswer);
} while(cursor2.moveToNext());

Related

SQL: Can't get count(*) to work - why so?

I'm trying to query a database for the count of entries that match the given ID, but I can't come up with a working solution.
There are two of each type of account that correspond to the userID of 1, for some reason the first method outputs "1" and the second "0". I can show the mainactivity and the database shchema if needed as well.
public int numberOfDebitAccounts(int userID){
int numOf_accs = 0;
String query = "select count(*) from debitAccount where userID = '" +userID+"'";
openDatabase();
Cursor cursor = db.rawQuery(query,null);
if(cursor.moveToFirst()) {
numOf_accs = cursor.getInt(0);
System.out.println("Number of debit accs: " + numOf_accs);
}
cursor.close();
closeDatabase();
return numOf_accs;
}
public int numberOfCreditAccounts(int userID){
int numOf_accs = 0;
String query = "select count(accountID) from creditAccount where userID = '" +userID+"'";
openDatabase();
Cursor cursor = db.rawQuery(query,null);
if(cursor.moveToFirst()) {
numOf_accs = cursor.getInt(0);
System.out.println("number of credit accounts: " + numOf_accs);
}
closeDatabase();
return numOf_accs;
}

ContentValues does not insert values into table

I have this:
public void addNewNote(Note n) {
ContentValues values = new ContentValues();
values.put(COLUMN_TITLE, n.getNote_title());
values.put(COLUMN_TEXT, n.getNote_text());
values.put(COLUMN_FK, n.getNote_ForeignKey()); //this column is integer type
values.put(COLUMN_PT, String.valueOf(n.getNote_PersonType())); //this column is char type
SQLiteDatabase db = this.getWritableDatabase();
db.insert("tbl_Notes",null, values);
}
as my add method, as I traced it, the record is added successfully. but when I try to get my note with below code, findNote always returns null and cnt is always 0.
public Note findNote(int note_id){
String query = "select * from tbl_Notes"; // where " + COLUMN_ID + " = " + note_id;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
int cnt = cursor.getCount();
Note N = new Note();
if (cursor.moveToFirst()) {
cursor.moveToFirst();
N.setNote_title(cursor.getString(1)); //title column
N.setNote_text(cursor.getString(2)); //text body column
N.setNote_ForeignKey(Integer.parseInt(cursor.getString(3))); //foreign key column
N.setNote_PersonType(cursor.getString(4).charAt(0)); //person type column
cursor.close();
}else {
N = null;
}
return N;
}
and here is my onClick which fires addNewNote:
Note myNote = new Note(
txt_Title.getText().toString(),
foreignKey,
personType,
txt_Body.getText().toString()
);
tbl_notes.addNewNote(myNote);
what am I doing wrong?!

SQLite Check if column exist or not

I want to check columns (not value) at agregasi table, if columns exist do something, but if columns does not exist show/print message 'Column does not exist'.
I could run code below while columns exist:
String keywords1={'pesawat','terbang'};
String sql1 = "SELECT " + keywords + " FROM agregasi"; //columns exist at agregasi table
Cursor c1 = myDbHelper.rawQuery(sql1, null);
if (c1.moveToFirst()) {
// i can do something (no problem)
}
But i have problem when columns name i change on purpose (to check). What should i do to print error message (in android/java way)?
**String keywords2={'psawat','terang'};**
String sql2 = "SELECT " + keywords + " FROM agregasi"; //columns does not exist at table
Cursor c2 = myDbHelper.rawQuery(sql2, null);
// what should i do get error message and show/print using toast
You're looking for getColumnIndex(String columnName).
int index = c2.getColumnIndex("someColumnName");
if (index == -1) {
// Column doesn't exist
} else {
...
}
Here's a general method you can use to check whether a particular column exists in a particular table:
public boolean isColumnExists(SQLiteDatabase sqliteDatabase,
String tableName,
String columnToFind) {
Cursor cursor = null;
try {
cursor = sqLiteDatabase.rawQuery(
"PRAGMA table_info(" + tableName + ")",
null
);
int nameColumnIndex = cursor.getColumnIndexOrThrow("name");
while (cursor.moveToNext()) {
String name = cursor.getString(nameColumnIndex);
if (name.equals(columnToFind)) {
return true;
}
}
return false;
} finally {
if (cursor != null) {
cursor.close();
}
}
}
Try this it works fine :)
Cursor res = db.rawQuery("PRAGMA table_info("+tableName+")",null);
int value = res.getColumnIndex(fieldName);

String array throws index out of bounds exception

Im making an android app and this part is where a cursor will go through a database and store the 'title' section of the table into a string array. This is then called in another class and is used to dynamically show buttons based on the entries. The code for putting the titles into an array is as follows:
public String[] getTitles()
{
SQLiteDatabase db =getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, SPORTS_TABLE_NAME);
String title;
String[] titleArray = new String[100];
String sql = "SELECT Title FROM Sports;";
Cursor cursor = db.rawQuery(sql, null);
int i = 1;
while (cursor.moveToNext()) {
if(cursor != null && cursor.moveToFirst()) {
title = cursor.getString(0);
titleArray[i] = title;
i++;
}
if(cursor != null)
db.close();
}
cursor.close();
return titleArray;
}
Then it is called with the following code:
int i = 1;
String[] titlesArray = db.getTitles();
for(String titles: titlesArray){
Button btn = new Button(this);
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btn.setId(i);
btn.setText(titlesArray[i]);
ll.addView(btn);
i++;
}
Been looking for a while and think it needs a fresh pair of eyes.. any ideas?
what if your query returns more than 100 rows? If think it would be safer to use something like
String sql = "SELECT Title FROM Sports;";
Cursor cursor = db.rawQuery(sql, null);
if (cursor == null)
return;
String[] titleArray = new String[cursor.getCount()];
while (cursor.moveToNext()) {
title = cursor.getString(0);
titleArray[i] = title;
i++;
}
of, better, a Collection.
String sql = "SELECT Title FROM Sports;";
Cursor cursor = db.rawQuery(sql, null);
if (cursor == null)
return;
ArrayList<String> titleArrayList = new ArrayList<String>();
while (cursor.moveToNext()) {
title = cursor.getString(0);
titleArrayList.add(title);
}

query to get email , contact name , and number in 1 line of code

nullim using Cursor cursor = getContentResolver()
.query(contactUri, projection, null,null,null); to get the number of clicked contact. Is it possible to get the email, number and name using this query. at the same time?
can we do it this way? as shown below?
String[] projection = {Phone.NUMBER};
String[] projection1 = {Email.DATA};
String[] projection2 = {Contacts.DISPLAY_NAME};
Cursor cursor = getContentResolver().
query(contactUri, projection, null,projection1,projection2);
cursor.moveToFirst();
int column = cursor.getColumnIndex(Phone.NUMBER);
int column1 = cursor.getColumnIndex(Email.DATA);
int column2= cursor.getColumnIndex(Contacts.DISPLAY_NAME);
String number = cursor.getString(column);
String email1 = cursor.getString(column1);
String name1 = cursor.getString(column2)
is this possible?
Is this correct now?
String[] projection = {Phone.NUMBER, Email.ADDRESS, Contacts.DISPLAY_NAME};
Cursor cursor = getContentResolver().
query(contactUri, projection, null,null,null);
cursor.moveToFirst();
int column = cursor.getColumnIndex(Phone.NUMBER);
int column1 = cursor.getColumnIndex(Email.ADDRESS);
int column2= cursor.getColumnIndex(Contacts.DISPLAY_NAME);
String number = cursor.getString(column);
String email1 = cursor.getString(column1);
String name1 = cursor.getString(column2)
editText3 = (EditText) findViewById(R.id.editText3);
editText17 = (EditText) findViewById(R.id.editText17);
editText3.setText(number);
editText17.setText(email1);
only the phone number is getting set in the text box
edittext3 and edittext17 both have mobile number? what an i doing wrong?
thank you
You can put more than one value in an Array:
String[] projection = {Phone.NUMBER, Email.DATA, Contacts.DISPLAY_NAME};
All together:
String[] projection = {Phone.NUMBER, Email.DATA, Contacts.DISPLAY_NAME};
Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null);
int column = cursor.getColumnIndex(Phone.NUMBER);
int column1 = cursor.getColumnIndex(Email.DATA);
int column2= cursor.getColumnIndex(Contacts.DISPLAY_NAME);
if(cursor.moveToFirst()) { // Check if data exists
String number = cursor.getString(column);
String email1 = cursor.getString(column1);
String name1 = cursor.getString(column2)
}

Categories

Resources