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.
Related
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
I have an activity page which has textview fields(name , phoneNo) and some buttons. I query the database to get the details of a single customer(1 record). A cursor is returned. I want to show the values(name , phoneNo) returned in the textviews of this activity. How can I do that without using a listView. I don't need a list because only a single record is returned. It's like a profile page. But to use a cursor I think I need somekind of listView. How to do this ???? Thankz in advance
String[] fromColumns = {TakenJobDataBaseOpenHelper.TAKENJOBS_COLUMN_RequestID,TakenJobDataBaseOpenHelper.TAKENJOBS_COLUMN_Destination,TakenJobDataBaseOpenHelper.TAKENJOBS_COLUMN_CustomerName};
int[] toViews = {R.id.textView1, R.id.textView3,R.id.CustomerName};
TakenJobDataBaseOpenHelper jobDatabaseHelper = new TakenJobDataBaseOpenHelper(this);
Cursor cursor = jobDatabaseHelper.getSingleRecord(id);
SimpleCursorAdapter cursoradapter = new SimpleCursorAdapter(this,
R.layout.activity_job_details, cursor, fromColumns, toViews, 0);
you don't have to use a listview. simply use it like this:
if (cursor.moveToFirst()) {
String name = cursor.getString(cursor.getColumnIndex('NAME'));
String phoneNo = cursor.getString(cursor.getColumnIndex('phoneNo'));
((TextView)findViewById(R.id.textView1)).setText(name);
((TextView)findViewById(R.id.textView2)).setText(phoneNo);
} else {
// nothing found!
}
I get some data from the database and try to fill them in a listview.
i have checked through cursor.count() there is data exist but the listview show anything :/
I use a simpleCursorAdapter:
rubDb.open();
cur = rubDb.getRubParent();
startManagingCursor(cur);
Log.d("cursor length",Integer.toString(cur.getCount()));
String[] from = new String[] { RubriqueDbAdapter.RUB_NOM, RubriqueDbAdapter.RUB_VISUEL };
int[] to = new int[] {R.id.title, R.id.icon};
listAdapter = new SimpleCursorAdapter(this, R.layout.itemgauche, cur, from, to);
listeGauche.setAdapter(listAdapter);
public Cursor getRubParent() {
Cursor cursorResults = mDb.rawQuery("SELECT * FROM " + TABLE_RUBRIQUE + " WHERE rub_id_parent = 0 ORDER BY rub_ordre ASC", null);
return cursorResults;
}
how can I get a picture from the sdcard and put it on the ImageView in the listview ?
Thanks for your help !
First, you should be using a CursorAdapter, perhaps a SimpleCursorAdapter, instead of manually copying all of the data into objects in an array, just to use ArrayAdapter.
Second, if your list will be anything other than a simple piece of text, you must teach the adapter how to do whatever else you want. With SimpleCursorAdapter, you could associate a ViewBinder with it, or subclass SimpleCursorAdapter and override setViewImage(), or subclass SimpleCursorAdapter and override bindView().
I am storing information on the phone's contacts using a sqlite database.
The fields I am storing are: _id, contact_id, body where _id is the row's id, contact_id is the phone contact's id and body is just a simple text which is the actual information I am storing.
I'm using a SimpleCursorAdapter to map the data to the view, like so:
Cursor cursor = database.fetchInformation(); // fetch info from DB
String[] from = new String[] { CONTACT_ID, BODY };
int[] to = new int[] { R.id.contact, R.id.body };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
getListView().setAdapter(adapter);
What I would actually want is to get the contact name that is associated with this CONTACT_ID, and have that be shown in the view.
I can't seem to find a simple solution. I've tried implementing my own SimpleCursorAdapter and rewriting setViewText(TextView, String) but it didn't work. Also, it just seems overly complicated for such a simple task.
Any help? Thanks.
You really have two options here:
1) You could create a MatrixCursor that contains all the information you wish to bind to your ListView. While its not very difficult to do this, its probably not the most efficient use of memory because you would need to get the name of every contact in your cursor.
2) You could subclass from CursorAdapter. Its pretty simple to implement the appropriate newView and bindView methods. Inside of bindView you would simply query the phone's contact content provider to get the name of the contact currently being bound.
try this
String where=PEOPLE.CONTACT_ID+"="+requiredId;
String[] projection={People.Name};//u only need name right?
int[] to = new int[] {R.id.body };//if body is ur text view
Cursor cursor = getContentResolver().query(People.CONTENT_URI,projection,where,null, null);
ListAdapter adapter=new SimpleCursorAdapter(this,R.layout.row,result,projection,to);
setListAdapter(adapter);
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.