Can I store in DB string resource name, not value?
For example,R.string.title, not "Some title". But when I want to using something like this:
private static final String[] FROM = { Items.name };
private static final int[] TO = { R.id.text1 };
adapter = new SimpleCursorAdapter(getActivity().getApplicationContext(), R.layout.item_list, null, FROM, TO, 0);
And in R.id.text1 I want to see"Item 3" or, depends on current locale, "Пункт 3" (in russian), not "R.string.item3".
And in R.id.text1 I want to see "Item 3" or, depends on current
locale, "Пункт 3" (in russian), not "R.string.item3".
Each column placed in String[] from will be assigned its value to int[] to in same order. In other words, every Items.name value will be assigned to TextView with R.id.text1
Related
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.
Right now I am almost done writing my app. All I need is a bit help on getting the selected String value from a spinner populated from my database by a simple cursor adapter. I am not sure how I can get the String from my spinner and pass it to a different cursor and use the string in a query, that will populate depending on the first choice of the spinner, and so on with other spinners.
Here is the code I am using for one of my spinners.
vType = (Cursor) DataBaseHelper.getPowersportsType();
this.startManagingCursor(vType);
SimpleCursorAdapter scaType = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item,
vType,
new String [] {DataBaseHelper.POWERSPORTS_TYPE},
new int[] {android.R.id.text1});
scaType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
vTypeSpinner = (Spinner) findViewById(R.id.typeSpinner);
vTypeSpinner.setAdapter(scaType);
And this is my query for the next spinner in my xml layout
static String MakeWhere = "POWERSPORTS_TYPE=?";
public static Cursor getPowersportsMake(){
return myDataBase.query(POWERSPORTS_TABLE,
new String [] {POWERSPORTS_ID, POWERSPORTS_MAKE},
MakeWhere,
null,
POWERSPORTS_MAKE,
null,
null);
}
Any comments or suggestions are welcomed.
to get the item selected you need to set the onItemSelectedListener
then in your onItemtSelected all you would do is
String selection = vTypeSpinner.getSelectedItem().toString();
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 :)
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
Been playing around with Android and using various resources have come up with this list:
public class TestingList extends ListActivity {
private static final String ICON_KEY = "icon";
private static final String TITLE_KEY = "title";
private static final String DETAIL_KEY = "detail";
private static final int[] ICONS = new int[] { R.drawable.lista,
R.drawable.listb, R.drawable.listc, R.drawable.listd };
private static final String[] TITLES = new String[] { "List 1", "List 2",
"List 3", "List 4" };
private static final String[] DETAILS = new String[] {
"List 1 description, a little more text here please, just testing how it reacts to more.",
"List 2 description, a little more text here please, just testing how it reacts to more.",
"List 3 description, a little more text here please, just testing how it reacts to more.",
"List 4 description, a little more text here please, just testing how it reacts to more." };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
for (int i = 0; i < ICONS.length; i++) {
rows.add(createListItemMap(ICONS[i], TITLES[i],
DETAILS[i]));
}
// set up SimpleAdapter for icon_detail_list_item
String[] fromKeys = new String[] { ICON_KEY, TITLE_KEY, DETAIL_KEY };
int[] toIds = new int[] { R.id.icon, R.id.text1, R.id.text2 };
setListAdapter(new SimpleAdapter(this, rows,
R.layout.icon_detail_list_item, fromKeys, toIds));
}
I'd like to some how add list separators to this list, what would the best way to get that done. The final list will have a lot more items, but I'd like named separators at certain points.
if you want to add a single divider that will be the same for each row than that is simple. you just call setDivider(Drawable) on your list and add the drawable that will represent your divider.
If you want to divide the list by categories like divider then things get more complicated.
#CommonsWare has a nice demo and a library of how to achieve this in his cwac-Merge project.
here is a link: Cwac
have a look at SeperatedListAdapter or android-section-list