I am successfully able to add data to sqlite database,
now when i click on a listview item,
i want to get data from sqlite table ID which is same as listview position
:
My Database looks like this
You need to separately query for getting raw at particular position clicked in LisView.
int id = 0; // id of clicked item
StringBuilder query = new StringBuilder("SELECT * FROM table_name WHERE id = ").append(id);
Cursor cursor = db.rawQuery(query.toString(), null);
if (cursor.moveToFirst()) {
Intent intent = new Intent(context, IntentClass.class);
intent.putExtra("id",id);
intent.putExtra("link",cursor.getString(4));
intent.putExtra("lid",cursor.getString(5));
intent.putExtra("lpass",cursor.getString(6));
intent.putExtra("laddInfo",cursor.getString(7));
context.startActivity(intent);
}
cursor.close();
Related
I have setup an application which currently can lookup an input id with one on the database to then give a single result. E.g. user enters id = 1 , database contains a record with an id of 1 then returns the name or number etc...
Now I want to improve the system slightly by querying my database with an arraylist which contains a range of id's e.g. 3, 456, 731 etc... which I want my database to search for. I have also grouped multiple values to certain id's for example the database might search for an id of 3 it will then find 5 results I want it to return the telephone number of each one of those results into another arraylist which I can print to the logs.
I hope I have explained this enough, but please ask questions if you require more information.
The code below demonstrates the modified version of the query used to gain a single result, but I cannot see what I'm doing wrong to gain multiple results.
Activity....
// New array list which is going to be used to store values from the database
ArrayList<String> contactsList;
// This arrayList has been received from another activity and contains my id's
ArrayList<String> contacts = intent.getStringArrayListExtra("groupCode");
// The database which i'm using
ContactDBHandler contactDBHandler = new ContactDBHandler(getApplicationContext(), null, null, 1);
//getAllValues is used to pass my arraylist id's to the database.
contactsList = contactDBHandler.GetAllValues(contacts);
// Simple log statement to loop and display results
for (int i = 0; i < contactsList.size(); i++){
Log.i("Numbers", contactsList.get(i));
}
ContactDBHandler
Query
// I'm telling it to get the contact number from the contact_list
// when the groupcode matches the code recieved.
public ArrayList<String> GetAllValues(ArrayList groupCode)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
String alarmName = "";
ArrayList<String> list = new ArrayList<>();
cursor = db.rawQuery("SELECT contact_number FROM contact_list WHERE grp_code=?", new String[]{groupCode+ ""});
if (cursor.moveToFirst())
{
do
{
list.add(cursor.getString(0));
}
while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed())
{
cursor.close();
}
return list;
}
Thanks
Can you see where I have gone wrong?
Try this:
cursor = db.rawQuery("SELECT contact_number FROM contact_list WHERE grp_code IN (" + TextUtils.join(",", Collections.nCopies(groupCode.size(), "?")) + ")", groupCode.toArray(new String[groupCode.size()]));
Your current code fails to pass the list in the sql-format: = does only support single values, for lists you have to use IN.
Your code would result in a query like this:
SELECT contact_number FROM contact_list WHERE grp_code=["some_id","other_id"]
But what you need (and my code produces) is:
SELECT contact_number FROM contact_list WHERE grp_code IN ('some_id','other_id')
References:
SQL query to find rows with at least one of the specified values
WHERE IN clause in Android sqlite?
IN clause and placeholders
https://developer.android.com/reference/android/text/TextUtils.html#join(java.lang.CharSequence,%20java.lang.Iterable)
You cannot pass an ArrayList to an SQLQuery. To check for multiple values in the same field you have to use the 'in' keyword.
Ex:
SELECT * FROM `table1` where column in ( 'element1', 'element2', 'element3')
In your case,
String str = "";
for(String s: groupCode){
str = str+","+"\'"+s+"\'";
}
//to remove the extra ' in the begining
str = str.substring(1);
return str;
cursor = db.rawQuery("SELECT contact_number FROM contact_list WHERE grp_code IN (?)", new String[]{str});
I find some tutorials, to create databaseHelper class, which help me to fetch data from database, the data was fetch using List, but somehow i dont know how to populate that list into ArrayList for my gridview and list view, when i try, it says List cannot converted into ArrayList. How can i do that? here my code snippet
databaseHelper.java
public List<Seat> getAllSeats() {
List<Seat> seats = new ArrayList<Seat>();
String selectQuery = "SELECT * FROM " + TABLE_SEATS + "WHERE status=0";
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
Seat seat = new Seat();
seat.set_id(c.getInt((c.getColumnIndex("id"))));
seat.set_table_no(c.getString(c.getColumnIndex("table_no")));
// adding to list
seats.add(seat);
} while (c.moveToNext());
}
return seats;
}
index.java
/* LIST SEAT */
final ArrayList<Seat> list_seat = db.getAllSeats();
final GridView gridview_seat = (GridView) findViewById(R.id.gridviewSeat);
gridview_seat.setAdapter(new SeatListAdapter(this, list_seat));
Just convert your list data into array list with following code...
List<Seat> list = db.getAllSeats();
ArrayList<Seat> list_seat = new ArrayList<Seat>(list.size());
list_seat .addAll(list);
//all judge name
in label's we get different judge-name from judge table in index of get indexes of judge-name means ids....
Code :
public List<String> getAlljudge(){
List<String> labels = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT " + KEY_ID_JUDGE + "," +KEY_NAME_JUDGE + " FROM " + TABLE_CONTACTS_JUDGE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.indexOf(cursor.getString(0));
labels.add(cursor.getString(1));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return labels;
}
further in another table i want to store this id's and selection of judge name through drop down.. actually another layout where a spinner is used where spinner data comes from this "labels" when selection of any judge i want to save corresponding id in another table...how to use both id and value both in labels???? help me...
Pass labels via Intent
Intent ipass = new Intent(this,Your next Class);
ipass.putStringArrayListExtra("key", labels);
startActivity(ipass);
Then In that layout
List<String> labels = new ArrayList<String>();
Intent iget = getIntent();
labels = iRes.getStringArrayListExtra("key");
Hope it work
For spinner operation use this http://www.mkyong.com/android/android-spinner-drop-down-list-example/
I have an activity which has two edit text fields: one for the title and the other for the story.
The entries made two text fields are saved in the database, also the database has a ROW_ID,TITLE_ID and STORY_ID. The entries are displayed in a list view.
When an item is long clicked, I get the row id corresponding to that item.
How should I get the TITLE_ID and STORY_ID from this?
Say, you have the corresponing row id of item which is long clicked in a variable rid. Then run the following query:
String q = "SELECT * FROM TABLE_NAME where(ROW_ID like '"+rid+"')";
Cursor c = db.rawQuery(q, null); // db is a object of SQLiteDatabase type
Then retrieve TITLE_ID and STORY_ID like:
if(c.getCount() == 0)
{
//No entry found
}
else {
c.moveToFirst();
do {
String titleid = c.getString(c.getColumnIndex("TITLE_ID"));
String storyid = c.getString(c.getColumnIndex("STORY_ID"));
} while (c.moveToNext());
c.close();
Then use them as you like :)
You could query the data base for the TITLE_ID and STORY_ID that correspond to the ROW_ID.
Post what code you have and we will be able to give more specific answers.
I am exctracting data from sqlite database and i need to transfer a column exctracted from select statment to other activity on a textview. How do i do it. Here's my code.
db=database.getReadableDatabase();
Cursor c = db.rawQuery("SELECT lusername, lpwd, lname FROM login WHERE
lusername=userfield.getText().toString(),lpwd=pwdfield.getText().toString()",null);
if(c!=null)
{
Intent intent = new Intent(this, HomePage.class);
intent.putExtra("TITLEBAR", lname);
}
i want lname(column) to go to next activity's textview.
First of all I doubt on your select query whether its worked or not..
But you told "I am extracting data from sqlite database" fine,
then just make a String[] of lname column's values from your database cursor then pass that String[] to other activity using intent like,..
Create intent:
Intent intent_name = new Intent(this, intent_name.class);
intent_name.putStringArray("key",string_array);
Then in your intent_name.class do:
String[] array = getIntent().getStringArray("key");