Fill spinner from cursor in android - android

i have searched for a awnser for this for a while today. It all looks so easy but i never get it to work. I want to fill a spinner with my cursor. I have been trying to use SimpleCursorAdapter for this as a lot of sites say i shall but i never get it to work. Show me just how easy it is :)
Thanks for your time!
My cursor
Cursor cursor = db.query(DATABASE_TABLE_Clients, new String[] {"_id", "C_Name"}, null, null, null, null, "C_Name");
My spinner
(Spinner) findViewById(R.id.spnClients);
My Code
Cursor cursor_Names = SQLData.getClientNames();
startManagingCursor(cursor_Names);
String[] columns = new String[] { "C_Name" };
int[] to = new int[] { R.id.txt_Address };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_dropdown_item, cursor_Names, columns, to);
Spinner spnClients = (Spinner) findViewById(R.id.spnClients);
spnClients.setAdapter(mAdapter);

The following code solved my problem. I was missing .setDropDownViewResource. After that i used simple_spinner_dropdown_item so i don't have to make my own layout.
Cursor cursor_Names = SQLData.getClientNames();
startManagingCursor(cursor_Names);
String[] columns = new String[] { "C_Name" };
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cursor_Names, columns, to);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spnClients = (Spinner) findViewById(R.id.spnClients);
spnClients.setAdapter(mAdapter);

I don't see a View for your dropdown in your code. Something like:
mAdapter.setDropDownViewResource(R.layout.spinner_view_dropdown);
Of course you need to have a spinner_view_dropdown.xml file in your res/layout directory.

I have done it
empresasSpinner = (Spinner) findViewById(R.id.empresasSpinner);
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, empresasAll.toArray(new EntidadObject[0]));
empresasSpinner.setAdapter(spinnerArrayAdapter);
A simple DTO
public class EntidadObject {
private int id;
private String nombre;
//GETTES and SETTERS
}
The part DAO
public class EntidadDao {
//...
public List<EntidadObject> getEmpresas() {
Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM empresas", null);
List<EntidadObject> entidadObjects = new ArrayList<EntidadObject>();
cursor.moveToFirst();
do {
EntidadObject entidadObject = new EntidadObject();
entidadObject.setId(cursor.getInt(0));
entidadObject.setNombre(cursor.getString(1));
entidadObjects.add(entidadObject);
} while (cursor.moveToNext());
return entidadObjects;
}
}
So then I can catch the ID of the select item with
EntidadObject eo = (EntidadObject)empresasSpinner.getSelectedItem();
eo.getId();

Related

using arraylist to populate the existing data in sqlite into spinner

Hi guys i have trouble to bind the existing data from sqlite to spinner. Do i have to apply arraylist ? If so how do i do it? Please help me i'm still new and this is for my project.
this is how the data taken from database :
public Cursor alldata()
{
ArrayList<String> values = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("Select * from shop", null);
return cursor;
}
this is the coding that supposedly to receive the data and show it on spinner but i have no idea to do it :
SpListofShop = (Spinner) findViewById(R.id.SpListofShop);
Cursor cursor = db.alldata();
if(cursor.getCount()==0)
{
Toast.makeText(getApplicationContext(), "NO DATA", Toast.LENGTH_SHORT).show();
}
else
{
while(cursor.moveToNext())
{
}
}
i really hope any of you can help me i'm too desperate
Coding to call the public arraylist function
this is the coding to call the public arraylist in databasehelper.java :
Spinner SpListofShop = findViewById(R.id.SpListofShop);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, db.alldata());
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
SpListofShop.setAdapter(adapter);
1.change alldata as follows:
public ArrayList<String> alldata()
{
ArrayList<String> values = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("Select * from shop", null);
for (int i=0;i<cursor.getCount();i++){
values.add(cursor.getString(cursor.getColumnIndex("row_name")));
//edit, change row_name with your row
}
cursor.close();
return values;
}
2.change spinner code like this:
Spinner SpListofShop = findViewById(R.id.SpListofShop);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, alldata());
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
SpListofShop.setAdapter(adapter);

Android Simple Cursor adapter and Cursor

I have build an app that take data with different attribute, adds them in the database and then shows it through a ListView.
I have done the part where data are added, but I Can't figure out how to fetch it(for now I just want the name) from the database and populate it in the ListView.
Here is the part in the database class.
public Cursor getCursor() {
Cursor c = null;
sqLiteDatabase = this.getReadableDatabase();
String query = "SELECT * FROM tbl_customer";
String where = null;
c = sqLiteDatabase.query("tbl_customer", new String[]{"Name"}, where, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
here is the part of code in activity which I want to show the ListView in.
private void populateListView(){
Cursor cursor = db.getCursor();
String []From = new String[]{"Name"};
int [] to = new int[R.id.textView];
SimpleCursorAdapter adapter;
adapter = new SimpleCursorAdapter(this,R.layout.listview_items,cursor,From,to,0);
ListView listView = (ListView)findViewById(R.id.ShowDataListView);
listView.setAdapter(adapter);
}
Please guide me, where I have gone wrong, and correct me.
you are using R.id.textView as a size of array
int [] to = new int[R.id.textView];
it should be like this
int [] to = new int[]{R.id.textView};
You've to specify the From and To params of the SimpleCursorAdapter.
adapter = new SimpleCursorAdapter(this,R.layout.listview_items,cursor,
new String[] { "Name" },to,0);
As for to, you need to put the id of your textview from R.layout.listview_items. Let's assume the id of your TextView is R.id.text1 then the adapter will look like following,
adapter = new SimpleCursorAdapter(this,R.layout.listview_items,cursor,
new String[] { "Name" },
new int[] { android.R.id.text1 },
0);
Here, have a look at the documentation to grasp it more clearly.

Append text to data returned from SQLite query before displaying in ListView

I am retrieving a set of results from an SQLite database and displaying it in a ListView.
What I want to do is to append some text before the result value before showing it in the actual ListView.
For example, if I am getting a value 5 from the database, I want to display it as Value = 5 in the ListView.
Here is the code that I am using for updating the ListView:
private void displayListView() {
Cursor cursor = dbHelper.fetchAllSubjects();
String[] columns = new String[]{
getSubjectsDbAdapter.KEY_SUBJECT,
getSubjectsDbAdapter.KEY_SEMESTER,
getSubjectsDbAdapter.KEY_ATTENDED,
getSubjectsDbAdapter.KEY_TOTAL,
getSubjectsDbAdapter.KEY_PERCENT
};
int[] to = new int[]{
R.id.sub_out,
R.id.semester_out,
R.id.attended_out,
R.id.total_out,
R.id.percent_out
};
dataAdapter = new SimpleCursorAdapter(this, R.layout.list_layout, cursor, columns, to, 0);
subject_list.setAdapter(dataAdapter);
}
What I had tried was the following modification to the String[] array:
private void displayListView() {
Cursor cursor = dbHelper.fetchAllVals();
String[] columns = new String[]{
"Val1" + getSubjectsDbAdapter.KEY_1,
"Val2: " + getSubjectsDbAdapter.KEY_2,
"Val3: " + getSubjectsDbAdapter.KEY_3,
"Val4: " + getSubjectsDbAdapter.KEY_4,
"Val5: " + getSubjectsDbAdapter.KEY_5
};
int[] to = new int[]{
R.id.one,
R.id.two,
R.id.three,
R.id.four,
R.id.five
};
dataAdapter = new SimpleCursorAdapter(this, R.layout.list_layout, cursor, columns, to, 0);
subject_list.setAdapter(dataAdapter);
}
But that gave me the following error:
java.lang.IllegalArgumentException: column 'Val2: val2Val3: val3' does not exist
I got my answer here at SO.
Cursor allTaskcursor = databaseHelper.getAllTasks();
String[] from = {"name", "date"};
int[] to = new int[] {android.R.id.text1, android.R.id.text2};
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(context, android.R.layout.simple_list_item_2, allTaskcursor, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
#Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == android.R.id.text2) {
int getIndex = cursor.getColumnIndex("date");
int date = cursor.getInt(getIndex);
TextView dateTextView = (TextView) view;
dateTextView.setText(date + " days");
return true;
}
return false;
}
});
allTaskListView.setAdapter(cursorAdapter);

ListView with SimpleCursorAdapter

in my app i am using a listview to display a menu of dishes with their Dish Names
This is the code where i bind the data from the query to the listview using the adapter:
private void getDishes() {
DBAdapter db = new DBAdapter(this);
db.open();
Cursor cursor = db.getAllDishes();
cursor.moveToFirst();
Log.w("ppp","kk - " + cursor.moveToFirst());
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.list_item, cursor, new String[] { DBAdapter.DishName },
new int[] {R.id.dishName });
setListAdapter(adapter);
db.close();
}
My Query code looks like this:
public Cursor getAllDishes()
{
return db.query(DATABASE_TABLE, new String[] {DishName},null,null,null,null,null);
}
Really appreciate any help/comments as i have been stuck on this for almost a week now.
Can you explain explicitely what's your problem?
My suggest change
public Cursor getAllDishes()
{
return db.query(DATABASE_TABLE, new String[] {DishName},null,null,null,null,null);
}
to
public Cursor getAllDishes()
{
Cursor dishes = db.query(DATABASE_TABLE, new String[] {DishName},null,null,null,null,null);
if(dishes != null)
dishes.moveToFirst();
return dishes;
}
Then
in getDishes() method write like this
private void getDishes() {
DBAdapter db = new DBAdapter(this);
db.open();
Cursor cursor = db.getAllDishes();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(YourActivity.this,
R.layout.list_item, cursor, new String[] { DBAdapter.DishName },
new int[] {R.id.dishName });
setListAdapter(adapter);
db.close();
}

Trying to list Android Bookmarks showing History as well

I'm trying to list the system default bookmarks but when I run the app, it list not only the bookmarks but also show History URLs and Most Visited URLs.
How can I avoid this and show only the bookmarks?
here is the method: (imported everything necessary + permissions valid)
public class CLASSNAME extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] projection = new String[] {Browser.BookmarkColumns._ID,
Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL};
String[] displayFields = new String[] {Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL};
int[] displayViews = new int[] { android.R.id.text1,
android.R.id.text2 };
Cursor cur = managedQuery(android.provider.Browser.BOOKMARKS_URI,
projection, null, null, null);
setListAdapter(new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, cur,
displayFields, displayViews));
}
}
Solution:
Replace the first "null" to "android.provider.Browser.BookmarkColumns.BOOKMARK"
Cursor cur = managedQuery(android.provider.Browser.BOOKMARKS_URI, projection, null->android.provider.Browser.BookmarkColumns.BOOKMARK, null, null);
Hope this helps anyone else :)

Categories

Resources