How to implement a MatrixCursor for a Spinner? - android

I have a SQLite query that returns a Cursor. I want to add some extra rows to the Cursor by implementing a MatrixCursor(to try to keep the first item of real data from automatically selected when clicked). Then I want to map them to a SimpleCursorAdapter. I kept reading the posts(and codes) but still remain fuzzy to me how to code it to my existing code listed below.
Cursor cursor = myDB.query(DATABASE_TABLE_NAME, resultColumns, whereClause,
whereArgs, null, null, null, null);
// Create Spinner View object from layout resource
Spinner spinner = (Spinner) findViewById(R.id.spinner);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item, // Use a template
// that displays a
// text view
cursor, // Give the cursor to the adapter
new String[] {"ename"}, // Map the NAME column in the
// people database to...
new int[] {android.R.id.text1});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

If you want to build a MatrixCursor from a simple Cursor you'll have to parse the entire initial Cursor and append the rows you want:
//...
MatrixCursor mc = new MatrixCursor(resultColumns);
// add extra rows, this will probably not work if you want to insert them
// between the initial cursor rows because of the _id column that need autoincrement values
mc.addRow(new Object[] { new Long(-2), "Extra name1" });
mc.addRow(new Object[] { new Long(-1), "Extra name2" });
// I don't know what your cursor holds, I assumed you have the _id column(long value)
// and a name(String value)
int size = cursor.getCount();
for (int i = 0; i < size; i++) {
cursor.moveToPosition(i);
mc.addRow(new Object[] {
cursor.getLong(cursor.getColumnIndex(/*the _id column*/)),
cursor.getString(cursor.getColumnIndex(/* the name column(ename?!?)*/)) });
}
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item, mc, new String[] {"ename"}, new int[] {android.R.id.text1});
//...
If you're doing this just to avoid the OnItemSelectedListener from firing when the Spinner is shown, maybe you could have another approach. For example in your listener:
//boolean status = true; flag in MyOnItemSelectedListener
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if (status) {
status = false;// this is the first listener trigger so you probably want to ignore it
return;
}
// do stuff here
}
Note: I don't know how good is the above solution. If you look there are probably much better solution to this Spinner related issue.

Related

How to query data for edit before show in list view?

I query database and show in list view like this.
public void onResume() {
super.onResume();
db = dbHelper.getWritableDatabase();
String[] queryColumns = new String[]{"_id", DBHelper.COL_VEHICLE_TYPE, DBHelper.COL_OPTION_NAME,DBHelper.COL_DATE };
cursor = db.query(DBHelper.TABLE_NAME, queryColumns,null,null,
null,null,null);
String[] showColumns = new String[]{DBHelper.COL_VEHICLE_TYPE, DBHelper.COL_DATE};
int[] views = new int[] {android.R.id.text1, android.R.id.text2};
adapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item, cursor, showColumns, views);
lv_driver.setAdapter(adapter);
}
I keep data 0, 1 in DBHelper.COL_VEHICLE_TYPE. If data equal 0 I want to show string "car" if equal 1 show string "motobike".
How to write condition (if, else) for change integer to string and show in list view id lv_driver.
instead of using SimpleCursorAdapter, you can use extends CursorRecyclerAdapter for recyclerView, this CursorRecyclerAdapter extended RecyclerView.Adapter, you can create your cursor fetch logic on CursorRecyclerAdapter class.
this website will help you to customized each view item:
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter

Android listview with database and fixed values

I'm quite beginner with android and I have problem with my listview.
So I'm getting listview values from database and thats working fine. But I want to edit one of those values before showing it to user and after many failures I kindly ask your advice.
Here is my code for fetching data and showing it in listview
listSquad=(ListView)findViewById(R.id.listview);
String[] arrayColumns = new String[]{"sq_pos", "sq_name", "sq_born", "sq_gam", "sq_goal"};
int[] arrayViewID = new int[]{R.id.pos,R.id.name,R.id.age,R.id.games,R.id.goals};
DBAdapter db = new DBAdapter(this);
db.open();
Cursor c;
c = db.doQuery("squad", null, null, null, null, null, "sq_name");
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.team_list_lay, c, arrayColumns, arrayViewID);
listSquad.setAdapter(adapter);
So I would like to take value of "sq_born" column and alter it and then show up in listview. That column is integer.
Like suggested by Aditya Rai, I used viewbinder like below:
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int column) {
if( view.getId() == R.id.age){
//do something here
TextView text = (TextView)view;
//set text in here
text.setText("blaah");
return true;
}
return false;
}
});

How to get values from a spinner populated from a cursor

I've populated my spinner as this:
DatabaseConnector dbConnector = new DatabaseConnector(this);
dbConnector.open();
Cursor c = dbConnector.raw("SELECT _id, nombrecasa FROM casa");
startManagingCursor(c);
String[] from = new String[]{"nombrecasa"};
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
casaSpinner = (Spinner) findViewById( R.id.casaspinner);
casaSpinner.setAdapter(adapter);
dbConnector.close();
}
And when I try to get the value as:
String idcasa = casaSpinner.getSelectedItem().toString();
it only returns this:
android.database.sqlite.SQLiteCursor#40539880
Your spinner has taken a SimpleCursorAdapter as an adapter, of which its dataset is a Cursor. When you select an item on the spinner, you've only moved the cursor to a specific row and when you call getSelectedItem() you're still requesting for the dataset object - which is still a cursor object. to do what you're trying you can simply call:
c.getString(1);
once your selection has been made. For something a little more fundamentary illustrative of what i said.
((Cursor) casaSpinner.getSelectedItem()).getString(1);

How to add an item to SimpleCursorAdapter?

I have a simple database table with 2 columns "_id" and "title".
and I'm displaying the data in a spinner, and it works well.
but I need to add one more item at the top of the spinner list that is not from the database with id = 0 and title = "not specified";
Spinner list = (Spinner) findViewById(R.id.spinner);
Cursor cursor = database.getAll(); // returns cursor with objects
String[] columns = new String[] {"title"};
int[] to = new int[] {R.id.title};
list.setAdapter(new SimpleCursorAdapter(this, R.layout.object_item_simple, cursor, columns, to));
I need to know the selected item id from the database, i can do this with list.getSelectedItemId();
so I can't use ArrayAdapter instead of SimpleCursorAdapter, because i don't think that there is a method for setting the id for each item on the adapter.
is there a way to do this?
Thanks.
You could create an object out of your id and title and build a list of these objects with the cursor. Then insert your artificial entry at the top top of that list.
Then when you construct your Adapter pass in this list.
Alternatively you could put a dummy value into your database, although that would be weird and maybe not possible depending on your query and data. The ArrayAdapter is much more sensible
How to Do This With SimpleCursorAdapter
This method:
Is Efficient
Works with standard CursorLoader and SimpleCursorAdapter idioms
Great with ContentProvider data
I create the item i want to insert into the cursor as a static MatrixCursor
private static final MatrixCursor PLATFORM_HEADER_CURSOR = new MatrixCursor(
//These are the names of the columns in my other cursor
new String[]{
DataContract.ReflashPackage._ID,
DataContract.ReflashPackage.COLUMN_PLATFORM
});
static {
PLATFORM_HEADER_CURSOR.addRow(new String[]{
"0",
"Select a Platform")
});
}
Here is my implementation of onLoadFinished which merges the cursor and passes it to the adapter.
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
switch (loader.getId()) {
case PLATFORM_CURSOR_LOADER_ID:
Cursor mergedCursor = addPlatformHeaderToCursor(data);
mPlatformAdapter.swapCursor(mergedCursor);
break;
}
}
#NonNull
private static Cursor addPlatformHeaderToCursor(Cursor platforms) {
Cursor[] cursorToMerge = new Cursor[2];
cursorToMerge[0] = PLATFORM_HEADER_CURSOR;
cursorToMerge[1] = platforms;
return new MergeCursor(cursorToMerge);
}
One technique that I use often is I will define an object (such as EntryObject) that has the variables I am going to need from the cursor to display. Once I have this I can iterate through the cursor and place the information into those EntryObjects and place them in an ArrayList or an array.
Then you can build a customer ArrayAdapter that will work with your new object to pull as much data as you need and display it how you want to.

Android Binding SQLite to GridView in Eclipse

I have made the following code to retrive data from SQLite database.
public Cursor fetchAllScores() {
return database.query(DATABASE_TABLE, new String[] { KEY_ROWID,
KEY_PLAYDATE, KEY_NUMVALA, KEY_NUMVALB }, null, null, null,
null, null);
}
Then I call this function in my main.java file using the following
cursor = dbHelper.fetchAllScores();
startManagingCursor(cursor);
After having cursor I manage to populate myGridView with some data using following code
GridView myGV = (GridView)findViewById(R.id.gridView1);
String[] cols = new String[] { scoreDbAdapter.KEY_PLAYDATE, scoreDbAdapter.KEY_NUMVALA, scoreDbAdapter.KEY_NUMVALB};
int[] views = new int[] { android.R.id.text1, android.R.id.text2, android.R.id.text2 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor, cols, views);
Log.w("NumRows",adapter.getCount() + "");
myGV.setAdapter(adapter);
Now the problem is only first row is populated and first two columns, I want data like in forst row (2011-10-27 , 5 , 6 ) and second row like (2011-10-26 , 3 , 2 ) but i m getting is only forst row like (2011-10-27, 2011-10-26 ).
Can this be fixed in GridView?
For your question, take a look here, Binding to Data with AdapterView.
Note: Below decription is just example for how to bind cursor to an adapter.
Filling the Layout with Data
Inserting data into the layout is typically done by binding the AdapterView class to an Adapter, which retrieves data from an external source (perhaps a list that the code supplies or query results from the device's database).
The following code sample does the following:
1.Creates a Spinner with an existing View and binds it to a new ArrayAdapter that reads an array of colors from the local resources.
2.Creates another Spinner object from a View and binds it to a new SimpleCursorAdapter that will read people's names from the device contacts (see Contacts.People).
// Get a Spinner and bind it to an ArrayAdapter that
// references a String array.
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter = ArrayAdapter.createFromResource( this, R.array.colors,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
// Load a Spinner and bind it to a data query.
private static String[] PROJECTION = new String[] { People._ID, People.NAME};
Spinner s2 = (Spinner) findViewById(R.id.spinner2);
Cursor cur = managedQuery(People.CONTENT_URI, PROJECTION, null, null);
SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item,cur,new String[] {People.NAME}, new int[] {android.R.id.text1});
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(adapter2);
Note that it is necessary to have the People._ID column in projection used with CursorAdapter or else you will get an exception.
If, during the course of your application's life, you change the underlying data that is read by your Adapter, you should call notifyDataSetChanged(). This will notify the attached View that the data has been changed and it should refresh itself.
For more info look at this,
Custom CursorAdapters.
Thanks.
Make a new layout which contains textViews. Find the layout as shown in first line of the code below and attach your data to the listView. ListView loads the layout containing textView.
private void populateGrid()
{
ListView lv = (ListView)findViewById(R.id.listView1);
Cursor c = dbHelper.fetchAllScores();
startManagingCursor(cursor);
String[] cols = new String[] { TodoDbAdapter.KEY_PLAYDATE, TodoDbAdapter.KEY_NUMVALA, TodoDbAdapter.KEY_NUMVALB};
int[] views = new int[] { R.id.txt_date, R.id.txt_no, R.id.txt_yes};
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.listviewtemp, c, cols, views);
Log.w("NumRows",adapter.getCount() + "");
lv.setAdapter(adapter);
}
R.layout.listviewtemp is the listView template layout (separate xml), lv is the listview found in the mainlayout.xml.
public Cursor fetchAllScores() {
return database.query(DATABASE_TABLE, new String[] { KEY_ROWID,
KEY_PLAYDATE, KEY_NUMVALA, KEY_NUMVALB }, null, null, null,
null, null);
}

Categories

Resources