Android: Put SQL data into array - android

i have two mehods, one to collect SQL data and one to put it into a listview.. it seems i can't get the array to form correctly in the SQL part so that it can be passed tot the listview.
SQL act:
public String[] getDataInArray() { // get data for list and return in array form
// TODO Auto-generated method stub
String[] columms = new String[]{ KEY_ROWID, KEY_NAME};
String[] return_colums = null;
Cursor c = currentdatabase.query(DATABASE_TABLE, columms, null, null, null, null, null);
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int rowcount = 0;
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{
return_colums[rowcount] = c.getString(iName) + "," + c.getString(iRow);
rowcount = rowcount + 1;
}
return return_colums;
}
And the listview act:
public ListView whiskeylist;
public String[] DataArryWhiskey;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Start db view of whiskey
DBConfig whiskeyrows = new DBConfig(this);
whiskeyrows.open();
DataArryWhiskey = whiskeyrows.getDataInArray();
whiskeyrows.close();
Toast.makeText(MainScreen.this, result, Toast.LENGTH_LONG).show();
whiskeylist = (ListView)findViewById(R.id.listofWhiskey);
whiskeylist.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , DataArryWhiskey));
// End db view of whiskey
}// end onCreate
I keeps on crashing, can anybody help met a bit? Thx in advance

Moved return_colums defination and declaration.
public String[] getDataInArray() { // get data for list and return in array form
// TODO Auto-generated method stub
String[] columms = new String[]{ KEY_ROWID, KEY_NAME};
Cursor c = currentdatabase.query(DATABASE_TABLE, columms, null, null, null, null, null);
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int rowcount = 0;
String[] return_colums = new String[c.getCount()];
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{
return_colums[rowcount] = c.getString(iName) + "," + c.getString(iRow);
rowcount = rowcount + 1;
}
return return_colums;
}

Move whiskeyrows.close(); from onCreate() to onDestroy(). More details in this link

Related

I need to display my data from database to listview. I'm using arrayadapter with listview

This is my method to get the data.Here its showing "change the type result to string".
public List<String> getData() {
// TODO Auto-generated method stub
String[] coloumn = new String[]{KEY,NAME,DETAIL};
Cursor c=ourDB.query(TABLE_NAME, coloumn, null, null, null, null, null);
List<String> result =" ";
List<String> results = new ArrayList<String>();
int iRow =c.getColumnIndex(KEY);
int iName =c.getColumnIndex(NAME);
int iDetails =c.getColumnIndex(DETAIL);
for(c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
result = result + c.getString(iRow)+" "+ c.getString(iName)+" = "+ c.getString(iDetails)+"\n";
}
return result;
}
This is my class to view the data.
public class View extends Activity {
SimpleCursorAdapter adaptr;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
ListView lv =(ListView) findViewById(R.id.listView1);
Remind info = new Remind(this);
info.open();
List<String> data =info.getData();
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, data));
}
}
please any one can suggest any ideas. thanks..!
From the error that you have mentioned, this is an obvious problem.
The following line in getData() is assigning a string literal to a List.
List<String> result =" ";
Change that to
String result = " ";
Use this:
public List<String> getData() {
// TODO Auto-generated method stub
String[] coloumn = new String[]{KEY,NAME,DETAIL};
Cursor c=ourDB.query(TABLE_NAME, coloumn, null, null, null, null, null);
List<String> result =" ";
List<String> results = new ArrayList<String>();
int iRow =c.getColumnIndex(KEY);
int iName =c.getColumnIndex(NAME);
int iDetails =c.getColumnIndex(DETAIL);
for(c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
result = result + c.getString(iRow)+" "+ c.getString(iName)+" = "+ c.getString(iDetails)+"\n";
results.add(result);
}
return results;
}
Directly add your database data to List :
results.add(" "+ c.getString(iRow)+" "+ c.getString(iName)+" = "+ c.getString(iDetails)+"\n");
Return String List instead String :
return results;
Remove this code which is no longer required :
List result =" ";

android separate rows of a table from database

I am trying to make a simple android app that get info of user from database, and I want to separate these info by line between them.
like this:
here is my code:
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ID2, KEY_NAME, KEY_EMAIL};
Cursor c = ourDbase.query(TABLE_SCORE, columns, null, null, null, null, null + " DESC");
String result ="";
int iRow = c.getColumnIndex(KEY_ID);
int iName = c.getColumnIndex(KEY_NAME);
int iEmail= c.getColumnIndex(KEY_EMAIL);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iEmail) + " " + c.getString(iName) + " -" + c.getString(iRow)+ "\n";
}
return result;
}
Do you mean you want something like :
1 Jason j#yahoo.com-2 Mark m#yahoo.com-3 Freya f#yahoo.com
You can do something like :
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iEmail) + " " + c.getString(iName) + " -" + c.getString(iRow)+ "\n";
result+="-"; //add "-" for each person
}
Please tell me if i miss-understood your question.
UPDATE
Looking from your example, you probably want to use a List of person. If you want this, try :
public List<Person> getData() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ID2, KEY_NAME, KEY_EMAIL};
Cursor c = ourDbase.query(TABLE_SCORE, columns, null, null, null, null, null + " DESC");
List<Person> people = new ArrayList<Person>();
int iRow = c.getColumnIndex(KEY_ID);
int iName = c.getColumnIndex(KEY_NAME);
int iEmail= c.getColumnIndex(KEY_EMAIL);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
Person p = new Person();
p.setEmail(c.getString(iEmail));
//set other info, like id, name
people.add(person);
}
return people;
}
Where person is a new public class with private id,name and email.
UPDATE
public class Person {
private String name, id, email;
//create the name,id, email setter - getter or just make those variables public
}
This is what class Person should look like :
Feel free to ask if you dont understand or i miss understood you.

Cannot retrieve List<String> from an SQLIte query method

In my android project ,I have successfully created an SQLite Database , Insertion operations are also successful.
I cannot retrieve the List into a method .
here is my calling method:
protected void getTheServerList() {
ServerManger info = new ServerManger(this);
try {
info.open();
servers = info.getData();
info.close();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "EXCEPTION !",
Toast.LENGTH_SHORT).show();
} finally {
}
}
and here is my SQLIte method
public List<String > getData() {
String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_UNAME,
KEY_PASSWORD };
List<String> sItems=null;
Cursor c = ourDatabase.query(TABLE_NAME, columns, null, null, null,
null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iUname = c.getColumnIndex(KEY_UNAME);
int iPass = c.getColumnIndex(KEY_PASSWORD);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
//sItems.add(KEY_NAME);
result =c.getString(iName);
sItems.add(result);
}
c.close();
return sItems;
}
and I get an Exception
03-12 12:19:15.798: E/Database(390): close() was never explicitly called on database '/data/data/com.example.proj/databases/PROJ_DB'
I can retrieve the String , but not the- List
Please help !
public List<String > getData() {
String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_UNAME,
KEY_PASSWORD };
List<String> sItems=new ArrayList<String> // define instance Here you create listview but not creat this instance
Cursor c = ourDatabase.query(TABLE_NAME, columns, null, null, null,
null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iUname = c.getColumnIndex(KEY_UNAME);
int iPass = c.getColumnIndex(KEY_PASSWORD);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
//sItems.add(KEY_NAME);
result =c.getString(iName);
sItems.add(result);
}
c.close();
return sItems;
}
initiate your list.. list is null
List<String> sItems=new ArrayList<String>();

How do i query SQLite table by string?

I'm a noob to android and im am trying to query a sqlite table. I have read several tutorials and know how to query a table to return an entire column of values, but i can't seem to figure out how to query a just a single value from a column by string. Any help is greatly appreciated. here is my code:
My successful code to query and return values for entire column:
public String getValue() {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iQuantity = c.getColumnIndex(KEY_QUANTITY);
int iOunces = c.getColumnIndex(KEY_OUNCES);
int iValue = c.getColumnIndex(KEY_VALUE);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + /*c.getString(iRow) + " " +*/ c.getString(iValue) + "\n";
}
return result;
}
My attempt to query column for single value by string:
public String getSingleValue(String aCoin) throws SQLException{
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_NAME + "=" + aCoin, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iQuantity = c.getColumnIndex(KEY_QUANTITY);
int iOunces = c.getColumnIndex(KEY_OUNCES);
int iValue = c.getColumnIndex(KEY_VALUE);
if (c != null){
c.moveToFirst();
result= c.getString(1);
return result;
}
return null;
}
Change this
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE };
to
String[] columns = new String[]{ KEY_NAME};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_NAME + "=" + aCoin, null, null, null, null);
String result = "";
if (c != null){
c.moveToFirst();
result= c.getString(0);
return result;
}
return null;
If you want to only get the name column.

how to get the id of the given row text in android from database

Hello friends i am new to android.well i have created a database table in that i have few rows as follows
_id name city
1 maddy0 xyz0
2 maddy1 xyz1
3 maddy2 xyz2
now what i want to pass a city to the function and get the row id of that city
say i pas xyz0 then it should give me that out put as 1 .for that i tried this
public String getDatatonumber(String s) {
Cursor c = null;
String result = new String();
// TODO Auto-generated method stub
// String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_SCORE};
try {
c = ourDatabase.rawQuery(
"select _id from quotes_internal where text ='"+s+"';", null);
// Cursor c = ourDatabase.query(DATABASE_TABLE_LOGICAL, columns,
// null, null, null, null, null);
int iName = c.getColumnIndex(KEY_ROWID);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result=(" " + c.getInt(iName));
}
} catch (Exception e) {
// TODO: handle exception
} finally {
c.close();
}
return result;
}
I put this code in the database helper class and from the main activity I am calling it as
results =new String();
results=db.getDatatonumber(texts).trim();
text.settext(results);
I am not able to understand where I am going wrong I am passing the same values which are there in the database.
c = ourDatabase.rawQuery(
"select _id from quotes_internal where city='"+s+"';", null);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result=(" " + c.getColumnIndex("_id"));
}
this will return your row id
You need to user LIKE Operator while comparing with Text
c = ourDatabase.rawQuery("select _id from quotes_internal where text LIKE '" + s.trim() + "';", null );

Categories

Resources