I'm searching for any simple implementation of that activity class.
I will answer myself here my solution
import greendroid.app.GDExpandableListActivity;
import greendroid.widget.ActionBarItem.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleExpandableListAdapter;
public class ExpListViewActivity extends GDExpandableListActivity {
private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";
DataBaseHelper dbHelper;
private ExpandableListAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addActionBarItem(Type.Add);
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < 20; i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "Item " + i);
curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
for (int j = 0; j < 5; j++) {
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
// curChildMap.put(NAME, "Child " + j);
curChildMap.put(IS_EVEN, (j % 2 == 0) ? "Hello " + j: "Good Morning "+ j);
}
childData.add(children);
}
// Set up our adapter
mAdapter = new SimpleExpandableListAdapter(
this,
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 },
childData,
android.R.layout.simple_expandable_list_item_2,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 }
);
setListAdapter(mAdapter);
}
}
Related
I need to display 5 different groups of strings. Some of the items in these strings are one or two sentences long. I was considering using an expandablelistview. The user could click one of the 5 categories and it would open up with all the strings.
Is there a string cut off for strings in an expandablelistview and listview or a way to display more then a sentence of information? I don't want the strings to get cut off. Note, I am not asking about the number of elements in the list.
public class HappyStuff extends ExpandableListActivity {
private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";
private ExpandableListAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.somelayout);
List<Map<String, String>> someData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> someotherData = new ArrayList<List<Map<String, String>>>();
//FROM HERE
for (int i = 0; i < 1; i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
someData.add(curGroupMap);
curGroupMap.put(NAME, "It's a wonderful life");
curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
List<Map<String, String>> children3 = new ArrayList<Map<String, String>>();
{
Map<String, String> curChildMap = new HashMap<String, String>();
children3.add(curChildMap);
curChildMap.put(IS_EVEN, "And it's a beautiful day in the neighborhood");
}
someotherdata.add(children3);
}
//TO HERE
//YOU CAN JUST KEEP ON ADDING NEW INSTANCES
supermagnificentAdapter = new SimpleExpandableListAdapter(
this,
someData,
R.layout.somespecialtypeoflayout,
new String[] { NAME, IS_EVEN },
new int[] { R.id.text1, R.id.text2 },
childData,
R.layout.rowsimpleitemlist2,
new String[] { NAME, IS_EVEN },
new int[] { R.id.text1, R.id.text2 }
);
setListAdapter(supermagnificentAdapter);
}
}
I have a working expandablelistview code, which works fine as a standalone.
I have another working code for sliding tab views which I wrote separately.
Both these were written after going thru a number of blogs, android dev and stackoverflow questions.
When I try to combine and put the expandable listview in one of the fragments, I end up with an error, I am unable to resolve.
Here's the code from the Fragment piece:
public class Fragment1 extends Fragment {
private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";
private ExpandableListAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < 10; i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "Group " + i);
curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
for (int j = 0; j < 2; j++) {
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, "Child " + j);
curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
}
childData.add(children);
}
// Set up our adapter
mAdapter = new SimpleExpandableListAdapter(
getActivity(),
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 },
childData,
android.R.layout.simple_expandable_list_item_2,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 }
);
setListAdapter(mAdapter);
}
}`
I keep getting error on the last line: 'The method setListAdapter(ExpandableListAdapter) is undefined for the type Fragment1'.
The rest of the code shows no error.
Please help. How can I correct this problem.
I was able to solve the problem myself.. For future reference, here's the code. Might be messy and needs cleanup, but it works for now!
public class HotDeals extends Fragment {
private ExpandableListAdapter mAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.saved_tabs, null);
return v;
}
private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < 10; i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "Group " + i);
curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
for (int j = 0; j < 2; j++) {
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, "Child " + j);
curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
}
childData.add(children);
}
ExpandableListView lv = (ExpandableListView) getActivity().findViewById(R.id.list);
// Set up our adapter
mAdapter = new SimpleExpandableListAdapter(
getActivity(),
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 },
childData,
android.R.layout.simple_expandable_list_item_2,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 }
);
lv.setAdapter(mAdapter);
}
}
#Divers & #Dixit - Thanx.
Fragment do not contains method setListAdapter. ListFragment do. You have to extend your fragment from this class to solve this problem.
This post is almost 2 years old, but no answer in this page is a relevant one.
to answer PO question,
The method setListAdapter(ExpandableListAdapter) is undefined for the type Fragment1
It's because setListAdapter is a method of ListFragment or ListActivity which is waiting for a ListAdapter or its subclasses as argument. You get this error because you gave it a ExpandableListAdapter which does not implement the ListAdapter interface.
Hope this will help someone.
In my ListView, I save my data like this:
public void listview_fuellen(){
DBHelper db = new DBHelper(this);
ListView lv = (ListView) findViewById(R.id.lvKinder);
Cursor c = db.select();
int count = c.getCount();
TextView tv = (TextView) findViewById(R.id.secondLine);
List<String> auswahl = new ArrayList<String>();
List<String> auswahl1 = new ArrayList<String>();
int i = 0;
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$1" + c.getCount());
while(c.moveToNext())
{
auswahl.add(c.getString(c.getColumnIndex("name")));
auswahl1.add(" " + i);
System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS" + auswahl.get(i).toString());
i++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_black_text,R.id.firstLine, auswahl);
//ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.list_black_text,R.id.secondLine, auswahl1);
lv.setAdapter(adapter);
}
Now, I tried the layout for my listview from here:
http://android-developers.blogspot.co.at/2009/02/android-layout-tricks-1.html
Now, how can I set values for the second line in each ListView-Item or the icon?
EDIT:
I tried it like this now, but there are no entries in my listview then.
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();;
int i = 0;
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$1" + c.getCount());
while(c.moveToNext())
{
//map = new HashMap<String, String>();
map.put("name",c.getString(c.getColumnIndex("name")));
map.put("datum", c.getString(c.getColumnIndex("zeit")));
i++;
}
mylist.add(map);
SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.list_black_text, new String[] { "datum",
"name" }, new int[] { R.id.firstLine, R.id.secondLine });
lv.setAdapter(mSchedule);
setListAdapter(mSchedule);
What have I done wrong?
for that you can make a custom adapter and then you can set values for the second line in each ListView-Item.
You can check this link
http://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/
I am using an expandable listview. The code is as follows and it works fine. But the problem is in a child of a group I want to put some big information, but it is not showing that. It is just showing 1 line.
public class list1 extends ExpandableListActivity {
private static final String NAME = "name";
private static final String time = "time";
private static final String IS_EVEN = "IS_EVEN";
private ExpandableListAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
final HashMap<String, String> info1 = new HashMap<String, String>();
info1.put(NAME, "information");
groupData.add(info1);
final HashMap<String, String> tt = new HashMap<String, String>();
tt.put(NAME, "time");
groupData.add(tt);
final HashMap<String, String> loc = new HashMap<String, String>();
loc.put(NAME, "Location");
groupData.add(loc);
List<Map<String, String>> childreninfo = new ArrayList<Map<String, String>>();
List<Map<String, String>> childrentime = new ArrayList<Map<String, String>>();
List<Map<String, String>> childrenloc = new ArrayList<Map<String, String>>();
Map<String, String> infochild = new HashMap<String, String>();
childreninfo.add(infochild);
infochild.put(NAME, "asnflabfbalv j;ajf;adf ;j; asjfsap sdjosdjosjdos dosjdosjdosjdos osfjsodfjsofjso osjfosjdfosjf s jsofjsojdsod jdosjdsosod jdsofjsojdfsofd jfosjdfsofjsof odjfsojfsodjosdjsod sojdosjdso djosjdosjds djsodj");
Map<String, String> timechild = new HashMap<String, String>();
childrentime.add(timechild);
timechild.put(NAME, "Childtime " + 1);
Map<String, String> locchild = new HashMap<String, String>();
childrenloc.add(locchild);
locchild.put(NAME, "Childloc " + 1);
childData.add(childreninfo);
childData.add(childrentime);
childData.add(childrenloc);
// Set up our adapter
mAdapter = new SimpleExpandableListAdapter(
this,
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME,time },
new int[] { android.R.id.text1, android.R.id.text2 },
childData,
android.R.layout.simple_expandable_list_item_2,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 }
);
setListAdapter(mAdapter);
}
}
in this infochild.put(); it is putting very big information, but here it is showing only 1st line of information.
How can I fix this to show all the information?
This happens because android.R.layout.simple_expandable_list_item_2 defines TwoLineListItem.
You should provide a simple layout with a single TextView instead:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
...
/>
Or you may also try using android.R.layout.simple_expandable_list_item_1.
I wonder how to get value from database into Expandable List via cursor
public class SmplExpandable extends ExpandableListActivity {
private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";
private ExpandableListAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < 10; i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "Group " + i);
curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
for (int j = 0; j < 3; j++) {
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, "Child " + j);
curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
}
childData.add(children);
}
// Set up our adapter
mAdapter = new SimpleExpandableListAdapter(
this,
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 },
childData,
android.R.layout.simple_expandable_list_item_2,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 }
);
setListAdapter(mAdapter);
}
}
Why don't you use the SimpleCursorTreeAdapter : http://developer.android.com/reference/android/widget/SimpleCursorTreeAdapter.html
By the way,if anyone has a good tutorial about SimpleCursorTreeAdapter,please share with us.
Here is sample from Android API demo ExpandableList2 You can import code by create sample project