setListAdapter multiple items? - android

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.

Related

Is there a better way of handling this list of strings?

I am developing an app for Android
I have different categories:
String[] cat = { "Category", "Books", "Clothing", "Shoes", "Hobbies",
"General", "Electronics", "Transportation", "Medicine",
"Sports", "Education", "Outdoor"};
The UI asks the user to select a category, Name, and some description which is then written to a db. When the user presses the List button, every category that is filled is shown and the items under that category is also show using an ExpandableListActivity.
List<List<String>> children = new ArrayList<List<String>>();
/**********************
I have a list for each category!!! But I don't like this solution. :(
**********************/
List<String> books = new ArrayList<String>();
List<String> clothing = new ArrayList<String>();
...
...
public void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = mDbHelper.fetchAllItems();
startManagingCursor(c);
// for all rows
for(int i=0; i<c.getCount(); i++)
{
// next row
c.moveToNext();
// pick a category
String t = c.getString(c.getColumnIndex("category"));
switch (getCat(t))
{
case Books:
books.add(c.getString(c.getColumnIndex("name")));
break;
case Clothing:
// do stuff for Clothing
break;
/**
And there are other cases for the rest of the categories but i am not listing
them here, since it is the same process
*/
default:
break;
} // end switch
} // end for loop
//for each category that has been filled up
children.add(category);
} // end function
My Question:
is it possible to not have to create a list for each category? I tried with one list but the results do not look very good. All categories show the same items and not the individual items, which makes sense.
You do not need to create the lists yourself.
You may want to have 2 tables - one listing the categories names and their ids (which will be primary table key) another one listing the stuff where each row has an item's category id, an item name and whatever else for a single item.
Then you can have SimpleCursorTreeAdapter like this:
SimpleCursorTreeAdapter (Context context, Cursor cursor, int groupLayout, String[] groupFrom, int[] groupTo, int childLayout, String[] childFrom, int[] childTo).
For groupLayout you will have android.R.layout.simple_expandable_list_item_1. For childLayout you will have android.R.layout.simple_list_item_1
For groupFrom you will have
String[] groupFrom = { FLD_CATEGORY_NAME };
int[] groupTo = { android.R.id.text1 };
For childFrom you will have
String[] childFrom = { FLD_ITEM_NAME };
int[] childTo = { android.R.id.text1 };
or rather your own layout if you want to display more than 1 or 2 elements
And you will have
protected Cursor getChildrenCursor(Cursor groupCursor) {
int categoryId = groupCursor.getColumnIndex(FLD_CATEGORY_ID);
// and here you a Cursor from your items table WHERE CTAEGORY_ID == categoryId
}
i just created a list of lists, and worked :)

Android SimpleCursorAdapter

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

How to display only some part of the data from DB into a list view

Here is my code to retriev the data from db to a listview.
private void fillData() {
Cursor c = viewDB.fetchAllRows();
startManagingCursor(c);
String[] from = new String[] { ViewComplaintsDataDB.KEY_NUMBER,
ViewComplaintsDataDB.KEY_DESCRIPTION,
ViewComplaintsDataDB.KEY_LOGGEDBY }; //these three are the fields in my db
int[] to = new int[] { R.id.com_num, R.id.com_desc , R.id.user }; //these three are the textview id's in my listitem.xml file
SimpleCursorAdapter simple = new SimpleCursorAdapter(this,
R.layout.listitem, c, from, to);
setListAdapter(simple);
}
This is working.
There is a lot of data in each row under the second column in my DB. How can i display only 50 characters of data from the second field to the listview. Please help me.
You could just set the maxLength atribute from the textView to 50.
<TextView
android:text="TextView"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLength="50"></TextView>
for that you need to ArrayAdapter instead of SimpleCursorAdapter and fetch the data required to show from the DB and set it to the adapter....

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