Retrieving Particular column from db into Spinner - android

My SqliteDB
In the above link am having my db screenshot
How to load that fiGoods column values into Spinner. Since am new to android kindly help me with full code

Write Query to get fiGoods
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select fiGoods from fiGoodsDetail", null);
String[] figoodslist;
res.moveToFirst();
int i=0;
while(!res.isAfterLast())
{
figoodslist[i] = res.getString(res.getColumnIndex("fiGoods"));
i++;
}
Get fiGoods value in String array.Then you have to bind array to spinner like this...
ArrayAdapter<String> adaper = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, YOUR_STRING_ARRAY);
spinner.setAdapter(adaper);

To link sqlite values to an adapter, use the handy CursorAdapter class.

Related

Android populate Listview from SQLite List

I'm able to populate my SQLite DB and to do all CRUD operations.
Now I would populate a listview starting from:
List<Starred> data = DBAdapter.getAllUserData();
By log I can read my list:
for (Starred st : data)
Log.d("Title: ", "Title: " + st.getName());
I'm pretty sure I'm missing the Adapter but don't well know how start.
This is my getAllUserData() function:
public static List<Starred> getAllUserData() {
List<Starred> starredList = new ArrayList<Starred>();
// Select All Query
String selectQuery = "SELECT * FROM " + USER_TABLE;
final SQLiteDatabase db = open();
Cursor cursor = db.rawQuery ( selectQuery, null );
if (cursor.moveToFirst()) {
do {
Starred data = new Starred();
data.setID(Integer.parseInt(cursor.getString(0)));
data.setName(cursor.getString(1));
data.setLabel(cursor.getString(2));
// Adding contact to list
starredList.add(data);
} while (cursor.moveToNext());
}
return starredList;
I tried with
ListView lv = (ListView) findViewById(R.id.list);
ArrayAdapter<Starred> arrayAdapter = new ArrayAdapter<Starred>(
this, android.R.layout.simple_list_item_1, data);
lv.setAdapter(arrayAdapter);
but I get nullpointer exception.
Could anyone give me suggestion? Any help would be much appreciated.
try to use the cursor adapter pal
https://github.com/codepath/android_guides/wiki/Populating-a-ListView-with-a-CursorAdapter

Is that possible Bind Spinner Without _id Column?

public void DatabaseConn() {
DataBaseHelper myDbHelper = new DataBaseHelper(this.getApplicationContext());
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
SQLiteDatabase db = myDbHelper.getReadableDatabase();
//SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.abc2/databases/DB_BusData", null, 0);
Cursor c = db.rawQuery("SELECT * FROM Tbl_Driver", null);
startManagingCursor(c);
//create an array to specify which fields we want to display
String[] from = new String[]{"Driver_Name"};
//create an array of the display item we want to bind our data to
int[] to = new int[]{android.R.id.text1};
//create simple cursor adapter
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
//get reference to our spinner
Spinner s = (Spinner) findViewById( R.id.DriverSpin);
s.setAdapter(adapter);
db.close();
}
Is that possible Bind Spinner Without _id Column? any idea?
Edited : Except SimpleCursorAdapter, any others adapter able to do this? I mean without _id column
From the android documentation for CursorAdapter which is the superclass of the SimpleCursorAdapter you are using:
The Cursor must include a column named "_id" or this class will not work.
So, no.
Edit:
Actually, there is a way to get around this issue. If you read carefully, it says that the Cursor must have a column named _id, which we can get by changing your query string to:
"SELECT ROWID AS _id, someColumn, anotherColumn FROM Tbl_Driver"
You will have to manually type out all of the columns you want though since doing ROWID AS _id will not work at the same time as using the wildcard *.

Android query sqlite database

What am I doing wrong!?!??
I am trying to get a set of data to a listview.
First I Open the database and then I trying to get the set,
I get a response but only 1 row from the database and I am getting at least 10 when I trying it in sqlite browser.....
Anyway I don't know if this make any sense, here is the code: (i am new at this, please don't laugh to much)
And btw I use the same methods/functions in another listview but then I don't have any WHERE in my query and that works fine....
So I want to get all the rows from the database and i only get the first one :)
Thanks mates!
db.openDataBase();
Cursor c = db.getCoursesFromCountyID(countyID);
BindsimpleCursorAdapter(c);
db.close();
the getCoursesFromCountyID =
int id = Integer.parseInt(county);
return db.query("Courses", new String[]{KEY_course_ID, KEY_course}, KEY_county_ID + " = " + id, null, null, null, null);
And the BindsimpleCursorAdapter looks like this =
startManagingCursor(c);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.simpleadapter_courses, c, new String[]{DataBaseHelper.KEY_course_ID, DataBaseHelper.KEY_course}, new int[]{R.id.courseID, R.id.course});
ListView lv = new ListView(this);
lv = (ListView)findViewById(R.id.listViewCourses);
lv.setAdapter(adapter);
I don't see any problems on your code, try adding 'KEY_county_ID' to you projection argument and removing the selection argument to check if all the rows are really there.

How to add distinct items into spinner from DB in android

i need to upload unique data from DB into spinner list in android.Ex.In my DB i have 4 records as "Jan","Feb","Mar","Jan".i need to upload those data in to spinner by distinct.i.e.only jan,feb,mar.
How could i do this?
My code...
//...
Spinner spin = (Spinner) findViewById(R.id.spin);
AdapterCountries = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item);
AdapterCountries.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(AdapterCountries);
spin.setOnItemSelectedListener(new OnItemSelectedListener());
db1.open();
// long id;
Cursor cursor = db1.getAllTitles1();
while (cursor.moveToNext()){
results=cursor.getString(2);
AdapterCountries.add(results);
}
db1.close();
In My DBAdapter class..
public Cursor getAllTitles1(){
return db.query(DATABASE_TABLE1, new String[] {
KEY_ROWID,DISHNAME,CATEGORY,DESCRIPTION},null,null,null,null,null);
}
You should use the DISTINCT clause in the the sql query, and it will return unique data already.
Use raw queries in execute method, instead of using the current approach.
Check this method for getting distinct values.

Autocompeletextview & customtable

HI i have a autocompletextview ,when am typing in autocomplete textview i need to directely query the custom table ,insted put it in array, My custom table contain list of names, how can i do this?
You should use SimpleCursorAdapter
SQLiteDatabase db // reference to database
// note that your query result should have an id field with name "_id"
Cursor cursor = db.rawQuery("select name_id as _id, name from table_names", new String[]{});
// create adapter over cursor, returned from database
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.simple_dropdown_item_1line, cursor, new String[]{"name"}, new int[]{android.R.id.text1});
AutoCompleteTextView autocomplete = (AutoCompleteTextView)findViewById(R.id.autocomplete_id);
autocomplete.setAdapter(adapter);
Also you should look at SimpleCursorAdapter.CursorToStringConverter to convert data from cursor to String, which will be displayed. Maybe this link helps you with SimpleCursorAdapter.CursorToStringConverter

Categories

Resources