Adding List separators in an existing list - android

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

Related

Android Spinner select Characteristics

I got a very annoying problem here and I hope you can help me.
I have a List of Plants (Self defined Class). These Plants got a Name, an ID and some characteristics. The characteristics are saved in Strings.
F.e. Characteristic1 is the size of a plant, characteristic2 is the design of the leafs and so on..
I have some Spinners now, and in each of these Spinners a Selection of one characteristic should be available (Spinner1 got all characteristics from the variable characteristics1 from all plants, ...)
The goal is that I can use these Spinners to give the User the Plants, which suits to the User's Selection of the characteristics.
Any ideas how I can implement that in a non-spaghetti way? I'm a bit new to programming so I could really need your help.
I Hope I explained it well, it's quite hard for me explaining it in my second language.
My Code:
Plant - Class
private int pd_id;
private String pd_plantname;
private String pd_longinfo;
private String pd_infochar1;
private String pd_infochar2;
private String pd_infochar3;
private String pd_infochar4;
private String pd_infochar5;
Image-Class
private int i_id;
private int i_p_id;
private String i_path;
private Calendar i_date;
In my Activity i got a List of Images and a List of Plants.
To Fill a Spinner with the characteristics i use this Code.
ArrayList<String> sInfo1 = new ArrayList<String>();
while (i < plantlist.size()) {
sInfo1.add(plantlist.get(i).getPd_infochar1());
i++;
}
ArrayAdapter<String> spInfo2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, sInfo1);
spArt.setTag(1);
spArt.setAdapter(spInfo1);
spAlter.setOnItemSelectedListener(new MyOnItemSelectedListener(spAlter));
I got this 5 times for 5 spinners. Now to the OnItemSelectedListener
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if (sp.getTag() == spAlter.getTag()) {
int i2 = 0;
while (i2 < plantlist.size()) {
if (plantlist.get(i2).getPd_infochar1()
.equalsIgnoreCase((String) sp.getSelectedItem())) {
}
else{
if(((String) sp.getSelectedItem()).equalsIgnoreCase("--Bitte auswählen--")){
}
else{
int pid = plantlist.get(i2).getPd_id();
plantlist.remove(i2);
ArrayList<Image> localIlist = getImageListFromPlantID(pid);
int x = 0;
while (x < localIlist.size()){
Toast.makeText(entscheidungsbaum.this, localIlist.get(x).getI_path(), Toast.LENGTH_SHORT).show();
imageList.remove(localIlist.get(x));
x++;
}
}
}
i2++;
}
}
So I delete for each characteristic which is wrong the images from the imagelist (which is the source for a gridview).
Firstly, this works not very well, I get some wrong images...
Secondly, that code is very bad, I know that. Can you help me find a better way to adapt the imagelist to the Spinner Selection?

Multidimensional array in Android from SQLite

Could anyone please explain how I can achieve the same from SQLite query in Java? The following is the code:
static final String[] AUTHORS = new String[] { "Roberto Bola–o","David Mitchell"};
private static final String[][] BOOKS =
new String[][] { "The Savage Detectives", "2666" },{ "Ghostwritten",
"number9dream", "Cloud Atlas","Black Swan Green",
"The Thousand Autumns of Jacob de Zoet" } };
This code is from the following class to implement sectioned gridview:Sectioned Gridview

Android SQLite and string resources

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

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 :)

how to sort arrays/list and return the result in android?

I'm using an expandable list view to display folders and files that are in my app. However, due to my app allowing user to add files, the list would be jumbled up. So, my intention is to set the sorted list (in names) as my child list.
I know that there is Arrays.sort and Collections.sort, but both the method are void, rather then returning the list.
So how do I do it ?
My list:
private File recordImageFolder = new File (Environment.getExternalStorageDirectory(),"/Record/Image");
private File recordSoundFolder = new File (Environment.getExternalStorageDirectory(),"/Record/Sound");
private String[] groups = { "Image", "Sound" };
private String[][] children = { recordImageFolder.list(), recordSoundFolder.list() };
Instead of initializing these field members in their declarations you could initialize them in the constructor or an init method, allowing you to use Arrays.sort(Object[]):
private File recordImageFolder;
private File recordSoundFolder;
private String[] groups;
private String[][] children;
public MyClass() {
recordImageFolder = new File (Environment.getExternalStorageDirectory(),"/Record/Image");
recordSoundFolder = new File (Environment.getExternalStorageDirectory(),"/Record/Sound");
String[] recordImages = recordImageFolder.list();
String[] recordSounds = recordSoundFolder.list();
Arrays.sort(recordImages);
Arrays.sort(recordSounds);
groups = new String[] { "Image", "Sound" };
children = new String[][] { recordImages, recordSounds };
}

Categories

Resources