ExpandableListAdapter add/delete group - android

I need to be able to create and delete groups in my expandableListAdapter dynamically. I have looked through all that I can find and am stuck. I do not need specific code, but just to be pointed in the right direction.

First, we need some data structures (keep a reference to them for later use).
headerData = new ArrayList<HashMap<String, String>>();
childData = new ArrayList<ArrayList<HashMap<String, Object>>>();
headerData is the list of groups - HashMap is used since each group can have multiple display values, each of which is mapped onto the layout by key.
childData is the list of items belonging to each group. Its a list of lists, each containing HashMaps - similar to the groups, each child can have multiple display values which are mapped by key.
We supply these datastructures to our ExpandableListAdapter on creation. We're also telling the adapter how the display values should be mapped; in this example both groups and children have two display values, keys "name" and "fields" which are mapped onto text1 and text2 in the supplied layouts.
adapter = new SimpleExpandableListAdapter( SearchLogs.this,
headerData, R.layout.customlayout_group,
new String[] { "name", "fields" }, new int[] { R.id.text1, R.id.text2 },
childData, R.layout.customlayout_child,
new String[] { "name", "fields" }, new int[] { R.id.text1, R.id.text2 } );
setListAdapter(adapter); // assuming you are using ExpandableListActivity
Up to this point we have an empty ExpandableList. We can populate it dynamically (using an AsyncTask for example) by creating HashMaps that supply values for keys we are using and then adding them to our lists.
For example, to add a group with a couple children we might ...
HashMap<String, String> group = new HashMap<String, String>();
group.put("name", "whatever...");
group.put("fields", "...");
ArrayList<HashMap<String, Object>> groupChildren = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> child1 = new HashMap<String, Object>();
child1.put("name", "child name");
child1.put("fields", "...");
HashMap<String, Object> child2 = new HashMap<String, Object>();
child2.put("name", "another child");
groupChildren.add(child1);
groupChildren.add(child2);
headerData.add(group);
childData.add(groupChildren);
Each HashMap in headerData corresponds (by order) to an ArrayList in childData, which contains additional HashMaps that define the actual children. SO even if you are adding an empty group, remember to add a corresponding (empty) ArrayList to childData.
We just added a group to the end of the list - we can just as easily insert as long as we are mindful to insert into the same position in both headerData and childData. Removing groups is the same - be certain to remove from the same position of both headerData and childData.
Finally, notify the adapter that the data has changed, which will cause the List to refresh. If using an AsyncTask, this must be done outside of doInBackground (use onProgressUpdate in this case).
adapter.notifyDataSetChanged();
I hope this helps you move in the right direction. ExpandableList, in terms of how data is stored, is definitely one of the more complicated Android views.

Related

Split TreeMap<List<>,List<>> into two ArrayList

i have two ArrayLists:
List<String> names = new ArrayList<String>(10);
List<Drawable> drawables = new ArrayList<Drawable>(10);
which i want to sort alphabetically.
For this i created a TreeMap:
Map<List<String>, List<Drawable>> myMapToSort = new TreeMap<List<String>, List<Drawable>>();
myMapToSort.put(names, drawables);
First two Question
Is the map now sorted in lexicographical order? Or do i Need to do something additional?
After i have sorted them, if they are yet, i want to split them back again to List<String> and List<Drawable>. And i tried like this:
List<String> sortedNames = new ArrayList<String>(myMapToSort.keySet());
Of course it doesn't work because myMapToSort.keySet() Returns a Set and not List.
And List doesn't have an Constructor for a Set.
So how can i accomplish that and what i'm misunderstanding?
Any help is appreciated. Thanks in advance!
I figured it out by my own.
The key was to create a TreeMap with not two list but two single Objects:
Map<String, Drawable> myTreeMap = new TreeMap<String, Drawable>;
Then add the Items from the Arraylists one by one to the Map:
for(int i = 0; i<names.size(); i++) {
myTreeMap.put(names.get(i), drawables.get(i));
}
Now the Map is automatically sorted Lexicographical in relation with the Drawables.
That means Names and Drawables are sorted in lexicographical order.
If you want to retrieve the keys and the values and put them back in seperate ArrayLists simply type:
List <String> mySortedNames = new ArrayList<String>(myTreeMap.keySet());
List <Drawables> mySortedDrawables = new ArrayList<Drawables>(myTreeMap.values());
That's it. ;)
You can use SortedSet (or SortedList) to have sorted elements. The elements are ordered using their natural ordering, or by a Comparator typically provided at sorted set creation time.
For splitting a Map in two lists :
List<String> sortedNames = new ArrayList<String>();
List<Drawable> drawables = new ArrayList<Drawable>();
for (Map.Entry<String, Drawable> entry : map.entrySet())
{
sortedNames.add(entry.getKey()
drawables.add(entry.getValue());
}

How to navigate through data via Sqllite and ListActivity

I'm displaying data from SQLite using
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
employees,
new String[] {"FirstName","LastName"},
new int[] {android.R.id.text1,android.R.id.text2},0);
getListView().setAdapter(adapter);
empoyees - my Cursor.
Data is showing correctly, but how to get what row I used from SQLite duaring OnClick on my item from List?
For example I have 2 tables
1)Category
_id Integer
Name Text
2)Articles
_id Integer
Name text
CategoryId (foreing key)
So on first screen I'm displaying all Categorys, then on list item click I want to display Articles that are from specified category, how to do that?
Don't use your cursor in your adapter. Build up an array and send in the array instead. In that array, typically something like List<MyDataRecordHolder> (where MyDataRecordHolder is an object you've created and filled with data from the database), you will have to set a reference to a row id.
UPDATE:
Create your MyDataRecordHolder: (Pseudo code)
List<MyDataRecordHolder> list = new List<MyDataRecordHolder>();
in your loop where you fetch data:
MyDataRecordHolder record = new MyDataRecordHolder();
record.setId(cursor.get("rowid"))
//set any other pertinent data
list.add(record);
For your adapter:
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
list,
new String[] {"FirstName","LastName"},
new int[] {android.R.id.text1,android.R.id.text2},0);
Note, you are now supplying the List (list) to the adapter.
In your OnClick for your row, in the adapter, you will get the current MyDataRecordHolder:
final MyRecordHolder record = getItem(position);
String id = record.getId();
//make new db query based on your id

HashMap Iteration only shows last element

I'm trying to implement an expandable listview with data coming from a database. I've already tried having the data added within the codes and it works so I'm now trying to have dynamic data from the database populate the listview.
The data is grouped in two, the main category and the member. For example if the main category is fruit, it's members may include mango, avocado, apples, etc. If animals, it may have horses, eagle, shark. The data always comes in pairs such as Animal, Horses; Animal, Eagle; Fruit, Apples. The expandable listview should appear as:
Animal
-- Horses
-- Eagle
Fruit
-- Apples
My code below can already determine the main group so the headers already display the main groupings of Fruit and Animal. By using
group_member.put(family_name, member_name);
I linked the member to the main category. My problem now is how to code for the iteration to group the members to the main category. So far I've already tried using iterator based on the sample codes given here in stackoverflow and from other sites however only the last elements of each group is display. I've also tried using the for-each loop bur still no success.
String json = jsonParser.makeHttpRequest(URL_COMPONENTS, "POST", params);
JSONArray components = null;
ArrayList<HashMap<String, String>> componentList;
List<String> main_group = new ArrayList<String>();
HashMap<String, String> group_member = new HashMap<String, String>();
try {
components = new JSONArray(json);
if (components != null) {
for (int i = 0; i < components.length(); i++) {
JSONObject c = components.getJSONObject(i);
String family_name = c.getString(TAG_FAMILY_NAME);
String member_name = c.getString(TAG_MEMBER_NAME);
main_group.add(family_name);
group_member.put(family_name, member_name);
if (!listDataHeader.contains(main_group))
listDataHeader.add(main_group);
componentList.add(group_member);
}
}
}
Please help. Thanks in advance!
If you're using HashMap you can't have a key more than once in your list. Each key must be unique.
This could be a solution:
HashMap<String, ArrayList<String>> group_member = new HashMap<String, ArrayList<String>>();
... using the Hashmap as a directory and the key is returning an Arraylist of the family members.

How to sort the arraylist from hashmap?

Im using hashmap and arraylist...
How to sort the arraylist ? eg) In hashmap values are in order like one,two,three,four,five
but i stored these values in arraylist the order changed like three,one,five,two,four
In my code groupList,gnamelist and newList are all arraylist...
In print sts PLACES are in correct order but while print on NEWLIST PLACES the order changed
How to sort this in order?
My code
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PLACE, gname);
map.put(TAG_HOTEL,lname);
// adding HashList to ArrayList
groupList.add(map);
gnamelist.add(gname);
System.out.println("PLACES" + gnamelist);
List<String> newList = new ArrayList<String>(new LinkedHashSet<String>(gnamelist));
Collections.sort(newList,Collections.reverseOrder());
System.out.println("NEWLIST PLACES" + newList);
HashSet will store elements in an unordered fashion, and is likely the culprit of your element reordering.
List<String> newList = new ArrayList<String>(new HashSet<String>(gnamelist));
Also consider using LinkedHashMap/LinkedHashSet, which preserves the ordering of elements added to it.
Alternatively try the following:
gnamelist.add(gname);
System.out.println("PLACES" + gnamelist);
List<String> newList = new ArrayList<String>();
newList.addAll(gnamelist);
Try
Collections.sort(newList);
Edit:
If you want to sort in reverse use this
Collections.sort(newList,Collections.reverseOrder());
Important:
if you want to preserve insertion order, you need to use TreeSet instead of HashSet as HashSet doesn't preserve insertion order

How to display images stored in database as a blob, in the Listview in Android?

My question here is that, I am creating a small quiz such that I want an image at the top and its answer right below it. This is all done in a new activity where I want to show the answers for the quiz. There are about 40 questions each with an image. Hence, I tried using HashMap as follows:-
ListView lv = (ListView)findViewById(R.id.list1);
String[] from = new String[] {"ques","ans"};
int[] to = new int[] {R.id.ques, R.id.ans};
// prepare the list of all records
List<HashMap<String,Bitmap>> fillMaps = new ArrayList<HashMap<String,Bitmap>>();
Cursor c1 = db.getQues(4);
byte[] bb = c1.getBlob(0);
Bitmap image = BitmapFactory.decodeByteArray(bb, 0, bb.length);
//Cursor c2 = db.getAns(4);
// String ans1 ="Ans"+") "+c2.getString(0);
HashMap<String,Bitmap> map = new HashMap<String, Bitmap>();
// HashMap<String,String> map1 = new HashMap<String, String>();
map.put("ques",image);
// map1.put("ans",ans1);
fillMaps.add(map);
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.itemsign, from, to);
lv.setAdapter(adapter);
But I couldn't find a way to correctly implement it. This code does not work. It just shows a blank page. So, any help will greatly be appreciated. As I am new to android so please be more detailed while explaining.
Well, I don't think that the blob is your problem. Here is the javadoc for the SimpleAdapter constructor:
public SimpleAdapter (Context context, List> data, int resource, String[] from, int[] to)
Since: API Level 1
Constructor
Parameters
context The context where the View associated with this SimpleAdapter is running
data A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
resource Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
from A list of column names that will be added to the Map associated with each item.
to The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.
You can't use Bitmaps in a SimpleAdapter, it's too simple for that :)

Categories

Resources