Expandable ListView display child group view instead of group view - android

I implement an ExpandableList in my app.
when i finish it work totally wrong.
List have only one row and in it display child data in row title view,
Here is my code :
My list adpater :
public class JoinUsAdapter extends BaseExpandableListAdapter {
public static final String DESCRIPTION_TG = "description";
public static final String TITLE_TG = "title";
private Context mContext;
private List<Map<String, String>> mData;
private LayoutInflater inflater;
private ViewHolder vholder;
public JoinUsAdapter(Context context, List<Map<String, String>> data) {
this.mContext = context;
this.mData = data;
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getGroupCount() {
return mData.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return 1;
}
#Override
public Map<String , String> getGroup(int groupPosition) {
return mData.get(groupPosition);
}
#Override
public String getChild(int groupPosition, int childPosition) {
return mData.get(groupPosition).get(DESCRIPTION_TG);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 1;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_title_join_us, null);
vholder = new ViewHolder();
vholder.txtTitle = (TextView) convertView.findViewById(R.id.txtNewsTitle);
vholder.txtShortDesc = (TextView) convertView.findViewById(R.id.txtNewsShortDesc);
convertView.setTag(vholder);
}
else {
vholder = (ViewHolder) convertView.getTag();
}
Map item = getGroup(groupPosition);
vholder.txtTitle.setText(item.get(TITLE_TG).toString());
vholder.txtTitle.setText(truncate(item.get(DESCRIPTION_TG).toString(),35)+" ...");
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_detail_join_us, null);
}
TextView txtLongDesc =(TextView) convertView.findViewById(R.id.txtLongDescription);
txtLongDesc.setText(getChild(groupPosition,childPosition));
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
And here is my Layouts :
title(as list row view) :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtNewsTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TITLE"
android:textStyle="bold"
android:textColor="#color/white"/>
<TextView
android:id="#+id/txtNewsShortDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="news description news description news description news description"
android:textStyle="bold"
android:textColor="#color/white"/>
</LinearLayout>
Detail: (as child of rows)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtLongDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/white"
android:text="long description long description long description long description long description long description "/>
</LinearLayout>
I cant find where is my mistake :/
Update:
My method for create fake data :
public void getData() {
dataList = new ArrayList<Map<String, String>>();
Map<String, String> item = new HashMap<String, String>();
String fake = "We asked respondents how satisfied they are with their current job or jobs. 76% of developers report being at least satisfied with their job, and 36% love their job. Developers are generally more fulfilled by work than most employees.\n" +
"\n" +
"And developers in Iran are more satisfied with their jobs than developers anywhere else. Stack Overflow Careers may not have any jobs available in Iran, but you can still move there and apply for one of our many available remote jobs.";
for(int i=0; i<10; i++) {
item.put(JoinUsAdapter.TITLE_TG,"Title "+i);
item.put(JoinUsAdapter.DESCRIPTION_TG,fake);
dataList.add(item);
}

Related

I want to know how to properly use adapters that connect Expandable List View and adapters that connect gridView at the same time

What I want to implement is putting a GridView in the Expandable List.
Currently, my code has two adapters called "CustomListAdapter" that connects the GridView to the Expandable List and "SiteAdapter" which connects the data to the GridView.
The code that we have built so far is well connected to the Expandable List to the GridView. However, the data inside the gridView seems to be connected to the gridView (seeing the items in the gridview and clicking on the appropriate link), but the image and text that make up the gridview's internal design do not appear on the screen.
I have used setImage and setText inside the adapter code called "siteAdapter", but I do not know why it is not configured properly on the screen.
1] SiteAdapter.java (the siteAdapter code that links the GridView and the items)
public class SiteAdapter extends ArrayAdapter<Site> {
private Site currentSite;
public SiteAdapter(Context context, ArrayList<Site> sites) {
super(context, 0, sites);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
}
currentSite = getItem(position);
ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);
imageView.setBackground(new ShapeDrawable(new OvalShape()));
imageView.setClipToOutline(true);
imageView.setImageBitmap(bitmap);
TextView name = (TextView) listItemView.findViewById(R.id.name);
name.setText(currentSite.getMsite_name());
return listItemView;
}
2] CustomListAdapter.java (links the ExpandableList and the GridView)
public class CustomListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private ArrayList<String> arrayGroup;
private HashMap<String,ArrayList<Site>> arrayChild;
public CustomListAdapter(Context context, ArrayList<String> arrayGroup, HashMap<String, ArrayList<Site>> arrayChild){
super();
this.mContext = context;
this.arrayGroup = arrayGroup;
this.arrayChild = arrayChild;
}
#Override
public int getGroupCount() {
return arrayGroup.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return arrayChild.get(arrayGroup.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return arrayGroup.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition)
{
return arrayChild.get(arrayGroup.get(groupPosition)).get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition){
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String groupName = arrayGroup.get(groupPosition);
View v = convertView;
if(v==null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = (LinearLayout)inflater.inflate(R.layout.parent_listview_title, null);
}
TextView textGroup = (TextView) v.findViewById(R.id.title_site_list);
textGroup.setText(groupName);
return v;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent){
String groupName = arrayGroup.get(groupPosition);
View v = convertView;
if(v == null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = (LinearLayout)inflater.inflate(R.layout.child_gridview_item, null);
}
SiteAdapter siteAdapter = new SiteAdapter(mContext,arrayChild.get(groupName));
GridView gridView = (GridView) v.findViewById(R.id.gridView);
gridView.setAdapter(siteAdapter);
return v;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
3] site_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<ExpandableListView
android:id="#+id/parentList"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ExpandableListView>
</LinearLayout>
4] parent_listview_title.xml(Code that designed the parent list of the Expandable List)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title_site_list"
android:layout_width="wrap_content"
android:textSize="40sp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:textColor="#000000"
android:layout_marginLeft="16dp"
android:fontFamily="#font/bm_dohyeon"
android:textStyle="bold"/>
</LinearLayout>
5] child_gridview_item.xml(Code that designed the child list of the Expandable List)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<ViewSwitcher
android:id="#+id/visibility"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginTop="20dp">
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#FFFFFF"
/>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#FFFFFF"
tools:context=".home"
android:id="#+id/gridView"
android:columnWidth="96dp"
android:numColumns="4"
android:verticalSpacing="10dp"
android:horizontalSpacing="4dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
</ViewSwitcher>
</LinearLayout>
6] list_item.xml(The code that designs the contents of the gridview, the child list of the Expandable List)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/image"
android:scaleType="centerCrop"
tools:src="#mipmap/ic_launcher"
android:layout_gravity="center"
/>
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:id="#+id/name"
tools:text="Gistsite"
android:textAppearance="?android:textAppearanceMedium"
android:textStyle="bold"
android:gravity="center_horizontal"
android:textSize="4sp"
/>
</LinearLayout>
7] Site.java
public class Site {
private String msite_name;
private String msite_url;
private String msite_urlF;
private String msite_urlY;
private String msite_urlB;
private String msite_urlW;
private String msite_urlI;
private int msite_imagesource;
public Site(String name, String url, int imagesource){
msite_name = name;
msite_url = url;
msite_imagesource = imagesource;
}
public Site(String name, String urlf, String urly, int imagesource){
msite_name = name;
msite_urlF = urlf;
msite_urlY = urly;
msite_imagesource = imagesource;
}
public Site(String name, String urlw, String urlb, String urlf, String urli,int imagesource){
msite_name = name;
msite_urlW = urlw;
msite_urlB = urlb;
msite_urlF = urlf;
msite_urlI = urli;
msite_imagesource = imagesource;
}
public String getMsite_name() {
return this.msite_name;
}
public String getMsite_url(){
return this.msite_url;
}
public String getMsite_urlW(){
return this.msite_urlW;
}
public String getMsite_urlB(){ return this.msite_urlB; }
public String getMsite_urlF(){
return this.msite_urlF;
}
public String getMsite_urlY(){
return this.msite_urlY;
}
public String getMsite_urlI(){
return this.msite_urlI;
}
public int getMsite_imagesource(){
return this.msite_imagesource;
}
In summary, the problem is that the contents of the image view and text view designed in list_item.xml are not available.

how to set the value at particular position in expandableListView

i have created a ExpandableListView.But There is a problem when i set the value of particular item same value set on all item i can not understand how to solve this.
this is my code.
direct_referal.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutfaq"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="25dp" >
<ExpandableListView
android:id="#+id/lvExp"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:padding="10dp"
android:cacheColorHint="#00000000"/>
</LinearLayout>
This is My Main Xml File.
DirectReferal.java
public class DirectReferalFragment extends Fragment
{
ExpandableDirectReferalListAdapter listAdapter;
ExpandableListView expListView;
private List<String> userID;
private List<String> joiningDate;
private HashMap<String, List<String>> slNo;
private HashMap<String, List<String>> name;
private HashMap<String, List<String>> city;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.direct_referal, container, false);
// get the listview
expListView = (ExpandableListView) view.findViewById(R.id.lvExp);
expListView.setGroupIndicator(null);
// preparing list data
prepareListData();
listAdapter = new ExpandableDirectReferalListAdapter(this.getActivity(), userID, joiningDate, slNo, name, city);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
// TODO Auto-generated method stub
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
// TODO Auto-generated method stub
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
return false;
}
});
return view;
}
/*
* Preparing the list data
*/
private void prepareListData() {
userID = new ArrayList<String>();
joiningDate = new ArrayList<String>();
slNo = new HashMap<String, List<String>>();
name = new HashMap<String, List<String>>();
city = new HashMap<String, List<String>>();
// Adding USER ID
userID.add("User ID : GLI000014");
userID.add("User ID : GLI000015");
userID.add("User ID : GLI000016");
userID.add("User ID : GLI000017");
userID.add("User ID : GLI000018");
// Adding JOINING DATE
joiningDate.add("Joining Date : 9 Nov,2015");
joiningDate.add("Joining Date : 9 Dec,2015");
joiningDate.add("Joining Date : 9 Jan,2016");
joiningDate.add("Joining Date : 9 Fab,2016");
joiningDate.add("Joining Date : 9 Mar,2016");
// Adding SL NO
List<String> no1 = new ArrayList<String>();
no1.add("SL No. : 1");
List<String> no2 = new ArrayList<String>();
no2.add("SL No. : 2");
List<String> no3 = new ArrayList<String>();
no3.add("SL No. : 3");
List<String> no4 = new ArrayList<String>();
no4.add("SL No. : 4");
List<String> no5 = new ArrayList<String>();
no5.add("SL No. : 5");
slNo.put(userID.get(0), no1); // Header, Child data
slNo.put(userID.get(1), no2);
slNo.put(userID.get(2), no3);
slNo.put(userID.get(3), no4);
slNo.put(userID.get(4), no5);
// Adding NAME
List<String> name1 = new ArrayList<String>();
name1.add("Name : Prabal Saxena");
List<String> name2 = new ArrayList<String>();
name2.add("Name : Chirag Mehta");
List<String> name3 = new ArrayList<String>();
name3.add("Name : Tarun Mehta");
List<String> name4 = new ArrayList<String>();
name4.add("Name : Prashant Gupta");
List<String> name5 = new ArrayList<String>();
name5.add("Name : Chotu");
name.put(userID.get(0), name1); // Header, Child data
name.put(userID.get(1), name2);
name.put(userID.get(2), name3);
name.put(userID.get(3), name4);
name.put(userID.get(4), name5);
// Adding CITY
List<String> city1 = new ArrayList<String>();
city1.add("City : Noida");
List<String> city2 = new ArrayList<String>();
city2.add("City : Gurgaun");
List<String> city3 = new ArrayList<String>();
city3.add("City : Delhi");
List<String> city4 = new ArrayList<String>();
city4.add("City : Pune");
List<String> city5 = new ArrayList<String>();
city5.add("SL No. : Delhi");
city.put(userID.get(0), city1); // Header, Child data
city.put(userID.get(1), city2);
city.put(userID.get(2), city3);
city.put(userID.get(3), city4);
city.put(userID.get(4), city5);
}
}
This is my main Java file. in this we set the value of all child and parent element and send the value via constructor.
list_item.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:background="#drawable/direct_referal_bg"
android:orientation="vertical" >
<TextView
android:id="#+id/lblListItemslno"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:gravity="left"
android:textColor="#000000" />
<TextView
android:id="#+id/lblListItemname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:gravity="left"
android:textColor="#000000" />
<TextView
android:id="#+id/lblListItemcity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:gravity="left"
android:textColor="#000000" />
</LinearLayout>
list_group.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp"
android:background="#333333">
<TextView
android:id="#+id/lblListHeaderuserid"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="User ID : GLI000014"
android:textSize="12sp"
android:gravity="center"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/lblListHeaderjoindate"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Joining Date : 9 Nov, 2015"
android:textSize="12sp"
android:gravity="center"
android:textColor="#FFFFFF" />
</LinearLayout>
ExpandableDirectReferalListAdapter.java
public class ExpandableDirectReferalListAdapter extends BaseExpandableListAdapter {
private Context _context;
List<String> _userID;
List<String> _joiningDate;
HashMap<String, List<String>> _slNo;
HashMap<String, List<String>> _name;
HashMap<String, List<String>> _city;
public ExpandableDirectReferalListAdapter(Context context, List<String> userID,List<String> joiningDate,
HashMap<String, List<String>> slNo,HashMap<String, List<String>> name,
HashMap<String, List<String>> city) {
this._context = context;
this._userID = userID;
this._joiningDate = joiningDate;
this._slNo = slNo;
this._name = name;
this._city = city;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._slNo.get(this._userID.get(groupPosition))
.get(childPosititon);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChildslno = (TextView) convertView.findViewById(R.id.lblListItemslno);
TextView txtListChildname = (TextView) convertView.findViewById(R.id.lblListItemname);
TextView txtListChildcity = (TextView) convertView.findViewById(R.id.lblListItemcity);
txtListChildslno.setText(childText);
txtListChildname.setText(childText);
txtListChildcity.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this._slNo.get(this._userID.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this._userID.get(groupPosition);
}
#Override
public int getGroupCount() {
return this._userID.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater=(LayoutInflater)this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeaderuserid = (TextView) convertView.findViewById(R.id.lblListHeaderuserid);
TextView lblListHeaderjoindate = (TextView) convertView.findViewById(R.id.lblListHeaderjoindate);
lblListHeaderuserid.setText(headerTitle);
lblListHeaderjoindate.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
This is my Adapter class.in this we want to set diffrent value of all child and parent field but same value set on all field.
you can see the the output in this image..
enter image description here
How can i solve this problem, I can not understant.
please help.
Please correct getChildView method gand etGroupView method. The code will be as follows.
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
String slNo = _slNo.get(_userID.get(groupPosition)).get(0);
String childName = _name.get(_userID.get(groupPosition)).get(0);
String childCity = _city.get(_userID.get(groupPosition)).get(0);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChildslno = (TextView) convertView.findViewById(R.id.lblListItemslno);
TextView txtListChildname = (TextView) convertView.findViewById(R.id.lblListItemname);
TextView txtListChildcity = (TextView) convertView.findViewById(R.id.lblListItemcity);
txtListChildslno.setText(slNo);
txtListChildname.setText(childName);
txtListChildcity.setText(childCity);
return convertView;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater=(LayoutInflater)this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeaderuserid = (TextView) convertView.findViewById(R.id.lblListHeaderuserid);
TextView lblListHeaderjoindate = (TextView) convertView.findViewById(R.id.lblListHeaderjoindate);
lblListHeaderuserid.setText(headerTitle);
lblListHeaderjoindate.setText(_joiningDate.get(groupPosition));
return convertView;
}
Hope that you can understand what was your mistake..
I tested the code, it will work.

ExpandableListView is not expanding

I m creating ExpandableList view in Fragment, the Group item display in list, but when I touch the group item, it is not expanding.
Below is my fragment
public class InstituteFragment extends Fragment {
ExpandableListView expandableListView;
ArrayList<String> subjectName;
HashMap<String,ArrayList<String>> topicName;
public InstituteFragment() {
}
public static InstituteFragment newInstance(){
InstituteFragment fragment = new InstituteFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
subjectName = new ArrayList<>();
topicName = new HashMap<>();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_institute, container, false);
prepData();
expandableListView = (ExpandableListView)view.findViewById(R.id.expd_list_view);
SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName);
expandableListView.setAdapter(adapter);
return view;
}
private void prepData() {
subjectName.add("Physics");
subjectName.add("Chemistry");
subjectName.add("Biology");
ArrayList<String> physics = new ArrayList<>();
physics.add("Demo");
physics.add("Demo1");
physics.add("Demo2");
physics.add("Demo3");
ArrayList<String> chemistry = new ArrayList<>();
chemistry.add("Demo");
chemistry.add("Demo1");
chemistry.add("Demo2");
chemistry.add("Demo3");
ArrayList<String> biology = new ArrayList<>();
biology.add("Demo");
biology.add("Demo1");
biology.add("Demo2");
biology.add("Demo3");
topicName.put(subjectName.get(0), physics);
topicName.put(subjectName.get(1),chemistry);
topicName.put(subjectName.get(2),biology);
}
}
below is adapter
public class SubjectAdapter extends BaseExpandableListAdapter {
Context context;
ArrayList<String> subjectName;
HashMap<String,ArrayList<String>> topicName;
public SubjectAdapter(Context context, ArrayList<String> subjectName, HashMap<String, ArrayList<String>> topicName) {
this.context = context;
this.topicName = topicName;
this.subjectName = subjectName;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return this.topicName.get(this.subjectName.get(groupPosition))
.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.chapter_name, null);
TextView txtListChild = (TextView) convertView
.findViewById(R.id.chapter_name);
txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this.topicName.get(this.subjectName.get(groupPosition)).size();
}
#Override
public int getGroupCount() {
return this.subjectName.size();
}
#Override
public Object getGroup(int groupPosition) {
return this.subjectName.get(groupPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String subjectName = (String)getGroup(groupPosition);
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.subject_name,null);
TextView textView = (TextView)convertView.findViewById(R.id.subject_name);
textView.setText(subjectName);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
below is institute_fragment.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragment.InstituteFragment">
<ExpandableListView
android:id="#+id/expd_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ExpandableListView>
</RelativeLayout>
below is my groupview
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/_10sdp"
android:layout_margin="#dimen/_5sdp"
android:background="#android:color/white"
android:id="#+id/subject_name_cardview">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/subject_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subject Name"
android:textAppearance="?android:textAppearanceMedium"
android:padding="#dimen/_10sdp"
android:layout_marginTop="#dimen/_10sdp"
android:layout_marginBottom="#dimen/_10sdp"
android:layout_marginLeft="#dimen/_10sdp"
android:textColor="#android:color/black" />
<ImageButton
android:id="#+id/subject_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_keyboard_arrow_down_24dp"
android:background="#00000000"
android:layout_marginRight="#dimen/_20sdp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
below is childView xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/_5sdp"
android:background="#color/colorPrimary"
android:id="#+id/chapter_name_cardview">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/chapter_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subject Name"
android:textAppearance="?android:textAppearanceSmall"
android:padding="#dimen/_10sdp"
android:layout_marginTop="#dimen/_5sdp"
android:layout_marginBottom="#dimen/_5sdp"
android:layout_marginLeft="#dimen/_10sdp"
android:textColor="#android:color/black" />
<ImageButton
android:id="#+id/chapter_name_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_keyboard_arrow_right_24dp"
android:background="#00000000"
android:layout_marginRight="#dimen/_20sdp"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
Well, easy one.
You have imagebutton in groupview that is intercepting touch event. Add these parameters:
android:focusable="false"
android:focusableInTouchMode="false"
And do same for childview. You have exactly the same case there.
P.S. why are you using imagebuttons anyway? you could use imageviews instead
You need to do this one.
expandableListView = (ExpandableListView)view.findViewById(R.id.expd_list_view);
SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName);
expandableListView.setAdapter(adapter);
expandableListView .setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int groupPosition, long l) {
expandableListView.expandGroup(groupPosition);
return false;
}
});
Hope this will help you.
Try to pass your ExpandableListView in the adapter:
SubjectAdapter adapter = new SubjectAdapter(getActivity(),subjectName,topicName, expandableListView);
Then youe adapter should look like:
...
ExpandableListView expandableListView;
public SubjectAdapter(Context context, ArrayList<String> subjectName, HashMap<String, ArrayList<String>> topicName, ExpandableListView expandableListView) {
this.context = context;
this.topicName = topicName;
this.subjectName = subjectName;
this.expandableListView = expandableListView;
}
In your group view add following:
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expandableListView.expandGroup(groupPosition);
}
});

how to customize views of ExpandableList

I need an ExpandableList in my Android app. By extending ExpandableListActivity whose content view is as the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ExpandableListView
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="#+id/tv_add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/txt_add" />
</LinearLayout>
and extending BaseExpandableListAdapter I am now able to display data of groups and children in a TextView.
However, I want to customize the view of children. How may I be able to do so through another xml file?
EDIT:
Here's my row.xml for the view of children:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
/>
<TextView
android:id="#+id/description"
android:textSize="10sp"
android:textStyle="normal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
/>
</LinearLayout>
Create two layout xml files for Group and Child views respectively -
For example, *group_layout.xml* and *child_layout.xml*
These layouts are inflated and used in the custom ExpandableListAdapter as shown below.
You can customize the Adapter class and set that adapter to the ExpandableListView.
public class SampleActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
ExpandableListAdapter adapter = new ExpandableListAdapter(this, new ArrayList<String>(), new ArrayList<ArrayList<Vehicle>>());
// Set this adapter to the list view
listView.setAdapter(adapter);
}
}
Custom Adapter class can be created as shown below:
class ExpandableListAdapter extends BaseExpandableListAdapter {
public ExpandableListAdapter(Context context, ArrayList<String> groups,
ArrayList<ArrayList<Vehicle>> children) {
this.context = context;
this.groups = groups;
this.children = children;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return children.get(groupPosition).get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
// Return a child view. You can load your custom layout here.
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
Vehicle vehicle = (Vehicle) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_layout, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
tv.setText(" " + vehicle.getName());
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return children.get(groupPosition).size();
}
#Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
#Override
public int getGroupCount() {
return groups.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
// Return a group view. You can load your custom layout here.
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
String group = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_layout, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
tv.setText(group);
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}
Hope this helps you.
Thx to PRC, but according to my row.xml, the correct version of getChildView would be
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
LinearLayout layout;
Vehicle vehicle = (Vehicle) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = (LinearLayout) infalInflater.inflate(R.layout.child_layout, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
tv.setText(" " + vehicle.getName());
return layout;
}

Can't seem to figure out ExpandableListAdapter

I'm trying to write a simple example which pulls external data over the network and populates an ExpandableListView via a ExpandableListAdapter. I've tried a couple examples and am pretty much lost. I'm familiar with working with the ArrayAdapter class, but it seems that ExpandableListAdapter is WAY different. Here is my current application code that is displaying nothing:
MyExpandableListAdapter:
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
public static class GroupHolder {
TextView title;
}
public static class ChildHolder {
TextView title;
ImageView icon;
}
// groups.getChildren() returns the children
private List<Group> groups;
private Context context;
private LayoutInflater inflater;
public MyExpandableListAdapter(Context context, List<Group> groups) {
this.context = context;
this.groups = groups;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public boolean hasStableIds() {
return true;
}
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
public int getGroupCount() {
return groups.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
GroupHolder holder;
if (convertView != null) {
holder = (GroupHolder)convertView.getTag();
} else {
convertView = inflater.inflate(R.layout.group_item, parent, false);
holder = new GroupHolder();
holder.title = (TextView) convertView.findViewById(R.id.group_item_title);
convertView.setTag(holder);
}
holder.title.setText(this.groups.get(groupPosition).getName());
return convertView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public Object getChild(int groupPosition, int childPosition) {
return groups.get(groupPosition).getChildren().get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return groups.get(groupPosition).getChildren().size();
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder holder;
if (convertView != null) {
holder = (ChildHolder) convertView.getTag();
} else {
convertView = inflater.inflate(R.layout.child_item, parent, false);
holder = new ChildHolder();
holder.title = (TextView) convertView.findViewById(R.id.child_item_title);
holder.icon = (ImageView) convertView.findViewById(R.id.child_item_icon);
convertView.setTag(holder);
}
holder.title.setText(groups.get(groupPosition).getChildren().get(childPosition).getName());
// TODO add in image loading.
return convertView;
}
}
MyExpandableListActivity:
public class MyExpandableListActivity extends ExpandableListActivity {
private List<Group> groups;
private ExpandableListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.group_list);
this.groups = new ArrayList<Group>();
Group group = new Group("$1", "Colors");
Child child = new Child("$2", "Red");
groups.getChildren().add(child);
this.adapter = new MyExpandableListAdapter(this, groups);
setListAdapter(this.adapter);
}
}
R.layout.group_list:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ExpandableListView android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView android:id="#+id/android:empty"
android:text="EMPTY! DOOM!"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
R.layout.group_item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="#+id/group_item_title"/>
</LinearLayout>
R.layout.child_item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView android:id="#+id/child_item_icon"
android:layout_width="48px" android:layout_height="48px"/>
<TextView android:id="#+id/child_item_title"/>
</LinearLayout>
I, of course, am seeing "EMPTY! DOOM!" rather than the list item I added in my activity. What am I doing wrong?
Also, it seems that the SimpleExpandableListAdapter is supposed to make things easy, but I totally can't figure out how it works or how to use it. Can anyone help me figure this out?
I got bit this week with two items trying to use "fill_parent" at the same time and in the same space. It's possible your list is being shoved to the back or off screen. I see you have a simlar setup in your group_list layout. Hope that helps with the invisible list piece.
What about changing the LinearLayout to have a vertical orientation in your group_list.xml?
It doesn't quite match the layout given in the ExpandableListActivity docs.
You can also set breakpoints in places like your getView() method and see if it's being called at all.

Categories

Resources