Expandable ListView in Fragment - android

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.

Related

Size limit on the size of strings in an ExpandableListView?

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);
}
}

Greendroid GDExpandableListActivity example needed

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);
}
}

Database results in a listView is not working

i m trying to get data from my database and present them to a listView.i m using two classes for my database:
1st class is the DBAdapter and the method i use to get data is:
public String[] getData()
{
// String[] columns =new String[]{DBHelper.ROWID, DBHelper.TITLE , DBHelper.AUTHOR, DBHelper.ISBN };
String[] columns =new String[]{DBHelper.TITLE , DBHelper.AUTHOR, DBHelper.ISBN };
Cursor c=ourDatabase.query(DBHelper.DATABASE_TABLE, columns, null, null, null, null, null);
String result="";
String sa = null;
String sb = null;
String sc = null;
//int iRow=c.getColumnIndex(DBHelper.ROWID);
int is1=c.getColumnIndex(DBHelper.TITLE);
int is2=c.getColumnIndex(DBHelper.AUTHOR);
int is3=c.getColumnIndex(DBHelper.ISBN);
for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
//result=result+c.getString(is1)+" "+c.getString(is2)+" "+c.getString(is3)+"\n";
sa=c.getString(is1);
sb=c.getString(is2);
sc=c.getString(is3);
}
//Toast.makeText(HotOrNot.this, sa, Toast.LENGTH_SHORT).show();
return new String[] {sa,sb,sc};
}
2.The secind class in SQLView and thats the way i m trying to create my list
public class SQLView extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
HotOrNot entry2=new HotOrNot(this);
entry2.open();
String[] data2=entry2.getData();
entry2.close();
Toast.makeText(SQLView.this, data2[1].toString(), Toast.LENGTH_SHORT).show();
ListView list = (ListView) findViewById(R.id.list);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map = new HashMap<String, String>();
map.put("name",data2[0].toString());
map.put("address", data2[1].toString());
map.put("address2", data2[2].toString());
mylist.add(map);
// ...
ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
data2, new int[] {R.id.rtextView1,R.id.rtextView2,R.id.rtextView3});
list.setAdapter(mSchedule);
What i have to do in order to work?This is my first try to use database in android!
Now i just get an empty background...
EDIT:
With my toast i m getting the last item in position 1 that i added to db
Could anybody help me with the return statement in my DBHelper please?
Below is the fixed code. Try this out.
ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
new String[] { "name", "address", "address2"},
new int[] {R.id.rtextView1,R.id.rtextView2,R.id.rtextView3});
list.setAdapter(mSchedule);
Basically you have to specify the columns/key of the map that you added. So it will map key from map to the corresponding view.
For looping issue, this will do the job:
for(int i=0; i<data2.length; i+=3) {
HashMap<String, String> map;
map = new HashMap<String, String>();
map.put("name",data2[i].toString());
map.put("address", data2[i+1].toString());
map.put("address2", data2[i+2].toString());
mylist.add(map);
}
The getData fix:
ArrayList<String> arrForData = new ArrayList<String>();
//int iRow=c.getColumnIndex(DBHelper.ROWID);
int is1=c.getColumnIndex(DBHelper.TITLE);
int is2=c.getColumnIndex(DBHelper.AUTHOR);
int is3=c.getColumnIndex(DBHelper.ISBN);
for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
//result=result+c.getString(is1)+" "+c.getString(is2)+" "+c.getString(is3)+"\n";
arrForData.add(c.getString(is1));
arrForData.add(c.getString(is2));
arrForData.add(c.getString(is3));
}
//Toast.makeText(HotOrNot.this, sa, Toast.LENGTH_SHORT).show();
return arrForData.toArray();

Expandable listview child not showing whole text.+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.

how to get value from database into Expandable List via cursor

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

Categories

Resources