Android SimpleCursorAdapter - android

I'm using a ListView a with SimpleCursorAdapter like this:
final ListAdapter adapter = new SimpleCursorAdapter
(this,android.R.layout.simple_list_item_1,c,new String[] {providerclass.tablename}, new int[] {R.id.listview1});
I'm not sure whether from is correct, what should I put in from?
When I put this it says URI not found.

Do like this:
String[] from = new String[] { "CUSTOMER" } // assuming your cursor have field "CUSTOMER"
int[] to = { android.R.id.text1 } // because android.R.layout.simple_list_item1 has such field

Related

setListAdapter multiple items?

Is it possible to have SimpleCursorAdapter retrieve multiple items and have them display? This is from the android Notepad tutorial(edited a bit) and when I try to have it pull AScoresDbAdapter.KEY_COL2_NAME, AScoresDbAdapter.KEY_COL1_DATE it only displays the first one in the TextView
Is there a reason for this or am I missing something?
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = scoresDbAdapter.fetchAllNotes();
startManagingCursor(c);
String[] from = new String[] { AScoresDbAdapter.KEY_COL2_NAME, AScoresDbAdapter.KEY_COL1_DATE};
int[] to = new int[] { R.id.text1 };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.shots_row, c, from, to,0);
setListAdapter(notes);
}
Ok so I guess it slipped pass me at first that they are arrays and that it is probably putting one from with one int so I added another TextView and edited the above to this:
String[] from = new String[] { AScoresDbAdapter.KEY_COL1_DATE,AScoresDbAdapter.KEY_COL2_NAME};
int[] to = new int[] { R.id.text1, R.id.text2 };
And now it displays [Date][Name] instead of just [Date] or [Name]
Not sure if this this the correct way, but if there is a better one I would be glad to know of it.

Convert from string[] to string Android

How can I convert from String[] to String only? So I can assign orderan to items and i can put it into my listview.
Here's my code.
MyClass class= new MyClass();
String orderan =class.getName();
String[] items = orderan; **the problem here
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
list.setAdapter(adapter);
It appears that you simply want this:
String orderan = class.getName();
String[] items = {orderan};
Or even:
String[] items = {class.getName()};
ArrayAdapter wants an array, but you're trying to assign the string orderan into the array items. Instead, make a new array, and add orderran into it. Something like:
String[] items = new String[1];
items[0] = orderan;
You have to create an Array and add the string to it.
String[] items = new String[1];
items[0] = orderan;
you can do it in one line
String[] items = {orderan};

Populate spinner with different cursors

I have a spinner that I populate with a cursor from database sqlite column, this works OK, but isn't ideal. I added another spinner to select a column and show it in spinner, but this is my problem when I build the adapter. This is my code:
cursor = myDB.obtenerColumna(getBaseContext(),elemento);
String[] columns = new String[] { "_id", "columna", "columnb", "columnc", "columnd" };
menudesplegable.setPrompt(getText(R.string.seleccionaEtiqueta));
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item,cursor,
columns, new int[] {android.R.id.text1});
adapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
I get columns from database correctly, but if I pass a cursor from other column the app fail building adapter. I try pass only name column in String[] but fail too.
Thanks.
I resolved this:
/**
* Crea el menĂº desplegable para seleccionar tiqueta.
*/
public void construirMenuDesplegable(String elemento) {
// get items of database using selected element in other spinner
cursor = myDB.obtenerColumna(getBaseContext(), elemento);
menudesplegable.setPrompt(getText(R.string.seleccionaEtiqueta));
// Create adapter
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item, cursor,
new String[] { elemento }, new int[] { android.R.id.text1 });
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Indicate adapter
menudesplegable.setAdapter(adapter);
try {
myDB.close();
} catch (Exception e) {
Log.e("miError", "Error al cerrar db", e);
}
}
/**
* Build spinner for select a element for show in other spinner.
*/
public void construirMenuDesplegable2() {
// Create adapter
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
this, R.array.listables, R.layout.custom_spinner);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Indicate adapter
menudesplegable2.setAdapter(adapter2);
}
No need to do the whole thing.
once the first spinner is populated just close the cursor.
mycursor.close();
then repeat same logic to the second spinner.

here is my code for spinner with dropdownlist:

I have spinner in my application .The spinner have drop down list.I want to take the value of the dropdown list from the database .how can i do this ?
here is my code for spinner with dropdownlist:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, selectdefault);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
You must create an array to specify which fields you want to display.
You could try this.
DatabaseProfileHelper dbhelper = new DatabaseProfileHelper(context);
// create an array to specify which fields we want to display
String[] from = new String[] { DatabaseProfileHelper.colNama };
int[] to = new int[] {R.id.txtProfileNama};
Cursor c = dbhelper.getAllProfiles();
dbhelper.UpdateProfile(null);
c.moveToFirst();
startManagingCursor(c);
// create simple cursor adapter
SimpleCursorAdapter ca=new SimpleCursorAdapter(context,R.layout.profile_spinner_row, c, from, to);
// get reference to our spinner
spinProfile.setAdapter(ca);

Android Gallery Text Only

I've looked into the API Demos and they have an example of the gallery where they show just text, the code just uses a Cursor , however I want to use a String array. How can I achieve using it? This is the code for the example in API Demos:
Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(c);
SpinnerAdapter adapter = new SimpleCursorAdapter(this,
// Use a template that displays a text view
android.R.layout.simple_gallery_item,
// Give the cursor to the list adatper
c,
// Map the NAME column in the people database to...
new String[] {People.NAME},
// The "text1" view defined in the XML template
new int[] { android.R.id.text1 });
If you just want to display a static list of String objects, an ArrayAdapter should do:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, new String[] {People.NAME});
However, if you want to be able to add more String objects to the list later, you should use a List.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, new ArrayList<String>());
You need to replace the CursorAdapter with an ArrayAdapter.

Categories

Resources