EditText inside ExpandableListView is not expanding the childs - android

I have created an ExpandableListView following this tutorial http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/ which is working fine. There have 2 TextView in the parent group. But when I am adding an EditText in parent group it's showing in the listview but after clicking on the parent it's not expanding. I assume it's due to the focus on EditText.
Please help to find a way to focus on parent item so that by clicking it expands and shows child items. On the other hand when I will try to write something in EditText it should focus on that without expanding the ListView.
Here is my code:
package com.sifb.icms.sit.application.adapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.sifb.icms.sit.R;
import com.sifb.icms.sit.object.Object_CateringMenu;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.TextView;
public class ExpandableListAdapter_CateringMenu extends BaseExpandableListAdapter {
private Context _context;
private List<Object_CateringMenu> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, String> _listDataChild;
public ExpandableListAdapter_CateringMenu(Context context, ArrayList<Object_CateringMenu> listDataHeader,
HashMap<String, String> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition).getCategoryChoice());
}
#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_cateringmenu_description, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.tvDescription);
txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return 1;
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this._listDataHeader.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Object_CateringMenu objGroup = (Object_CateringMenu) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_cateringmenu, null);
}
TextView lblListHeaderCat = (TextView) convertView
.findViewById(R.id.tvCategoryChoice);
lblListHeaderCat.setTypeface(null, Typeface.BOLD);
lblListHeaderCat.setText(objGroup.getCategoryChoice());
TextView lblListHeaderRatio = (TextView) convertView
.findViewById(R.id.tvRatio);
lblListHeaderRatio.setTypeface(null, Typeface.BOLD);
lblListHeaderRatio.setText(objGroup.getRatio());
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/svParent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tvTitle"
style="#style/tvTitleText"
android:text="#string/cateringmenu_title" />
<include
android:id="#+id/include_cateringmenu_header"
layout="#layout/list_cateringmenu_header" />
<ExpandableListView
android:id="#+id/elvCateringMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:groupIndicator="#android:color/transparent"
android:descendantFocusability="beforeDescendants"
android:layout_marginTop="10dp" />
</LinearLayout>
Parent ListView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rlParent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >
<TextView
android:id="#+id/tvCategoryChoice"
style="#style/tvCateringMenu"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_weight="0.5" />
<TextView
android:id="#+id/tvRatio"
style="#style/tvCateringMenu"
android:layout_weight="0.2" />
<EditText
android:id="#+id/etMealUplift"
style="#style/etCateringMenuMealUplift"
android:layout_weight="0.3" />
</LinearLayout>

Related

ExpandableListView with ViewPager as child-elements executes getChildView twice

I want to create an expandable list with viewpagers as subviews. The problem is now that with my code it calls the getChildView method twice and thus creates my viewpager twice.
I also tried it with the TextView (which is the sample code I posted here for convenience) to simplify the code but the same issue arose.
My best guess is it has something to do with the height property of the layouts but no matter how i changed it, I couldn't resolve the issue.
Please help. I am totally lost. I used a tutorial for this code and I seem to be the only who has that issue judging from the youtube comment section. (https://www.youtube.com/watch?v=jZxZIFnJ9jE)
(If you need any other part of the code please let me know.)
delete.xml file looks like this
<?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:id="#+id/sukahead">
<TextView
android:id="#+id/suka"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="suka"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingStart="?android:attr/expandableListPreferredChildPaddingLeft"
/>
</LinearLayout>
sublist.xml
<?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="wrap_content"
android:padding="8dp" android:background="#color/white"
android:id="#+id/sublist">
<TextView
android:id="#+id/dict_entry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingStart="?android:attr/expandableListPreferredChildPaddingLeft"
android:textSize="16sp"
android:textColor="#color/colorAccent"
android:text="test"
/>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#color/colorPrimary">
<EditText
android:id="#+id/search_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/search_hint"
android:layout_margin="15dp"
android:padding="5dp"
app:layout_constraintTop_toTopOf="parent"
android:background="#color/white"
android:adjustViewBounds="true"
android:maxLines="1"
android:inputType="text"
android:onClick="lookup_word"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/search_bar"
android:orientation="horizontal">
<ExpandableListView
android:id="#+id/search_result_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#333"
android:dividerHeight="1dp"
android:background="#color/white"
android:layout_margin="15dp"/>
</LinearLayout>
</RelativeLayout>
ExpandableListAdapter.java
package com.lunaticcoding.linguodict;
import android.content.Context;
import android.graphics.Typeface;
import android.support.v4.view.ViewPager;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class ExpendableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> data;
private HashMap<String, String[]> listHashMap;
private LayoutInflater inflater;
private String pageData[];
public ExpendableListAdapter(Context context, List<String> list, HashMap<String, String[]> hashMap) {
this.context = context;
this.data = list;
this.listHashMap = hashMap;
}
#Override
public int getGroupCount() {
return data.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return listHashMap.get(data.get(groupPosition)).length;
}
#Override
public Object getGroup(int groupPosition) {
return data.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return listHashMap.get(data.get(groupPosition))[childPosition];
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.sublist, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.dict_entry);
textView.setText(data.get(groupPosition));
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String)getChild(groupPosition, childPosition);
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.delete, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.suka);
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
MainActivity (only OnCreate)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchBar = (EditText) findViewById(R.id.search_bar);
searchResults = (ExpandableListView) findViewById(R.id.search_result_list);
list_results = new ArrayList<>();
examples_results = new HashMap<>();
list_results.add("test1");
list_results.add("test2");
list_results.add("test3");
String pageData1[] = new String[]{"si", "siiii"};
String pageData2[] = new String[]{"jifasfa", "sfasdfasiii"};
String pageData3[] = new String[]{"jifasfa", "sfasdfasiii"};
examples_results.put(list_results.get(0), pageData1);
examples_results.put(list_results.get(1), pageData2);
examples_results.put(list_results.get(2), pageData3);
searchResultsAdapter = new ExpendableListAdapter(this, list_results, examples_results);
searchResults.setAdapter(searchResultsAdapter);
lastExpandedPosition = -1;
searchResults.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
if(lastExpandedPosition != -1 && (lastExpandedPosition != groupPosition)){
searchResults.collapseGroup(lastExpandedPosition);
}
lastExpandedPosition = groupPosition;
}
});
}
In the sub_list.xml the layout_height is "wrap_content". Change it to a fixed height (some dp) or match_parent and it will work.
Reason:
wrap_content has to calculate the height of the layout content it has to match thus reloading the layout afterwards -> creating the list more than once.

How put image in Expandable List View in different mood

to view sample image , please click
I want to use expandable list view With the following codes . but I want put 2 different image in header 1 and 2 different image in header 2 , also put 3 different image in sub item 1 ans 2 different image in sub item 2 . I'm use it in eclips Android Programing
Code ExpandableListAdapter:
package com.expand.test;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.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 txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this._listDataHeader.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 lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
MainActivity Code :
package com.expand.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Test1");
listDataHeader.add("Test2");
listDataHeader.add("Test3");
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("SubItem1");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("SubItem1 ");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("SubItem1 ");
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), comingSoon);
}
}
activity_main.xml
<?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"
android:background="#f4f4f4" >
<ExpandableListView
android:id="#+id/lvExp"
android:layout_height="match_parent"
android:layout_width="match_parent" android:divider="#null"/>
</LinearLayout>
list_itme:
<?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="55dip"
android:orientation="vertical" >
<TextView
android:id="#+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:textSize="17dip"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" android:textColor="#000000"/>
</LinearLayout>
list_group.xml:
<?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="wrap_content"
android:orientation="vertical"
android:padding="8dp"
android:background="#000000">
<TextView
android:id="#+id/lblListHeader"
android:layout_width="fill_parent"
android:layout_height="32dip"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="17dp"
android:textColor="#f9f93d" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" android:gravity="right">
<ImageView
android:id="#+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>

Custom ExpandableListView is not working

I created custom ExpandableListView but it does not expand when I click on it.
activity_main.xml
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/exp_lv"
></ExpandableListView>
group_items.xml
<?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" >
<TextView
android:layout_width="270sp"
android:layout_height="30sp"
android:layout_marginLeft="30sp"
android:id="#+id/title"
android:textIsSelectable="true"
/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/chk"
/>
child_items.xml
<?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="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/child"
android:textIsSelectable="true"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="120sp"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:id="#+id/dd"
android:textStyle="bold"
/>
<TextView
android:layout_width="200sp"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/dd"
android:textIsSelectable="true"
android:id="#+id/date"
/>
</RelativeLayout>
group.java
package com.example.expandablelistview;
import java.util.ArrayList;
public class group {
private String Name;
private boolean isSelected;
private ArrayList<child> Items;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
public ArrayList<child> getItems() {
return Items;
}
public void setItems(ArrayList<child> items) {
Items = items;
}
}
child.java
package com.example.expandablelistview;
public class child {
String child_title;
String dd;
String date;
public String getChild_title() {
return child_title;
}
public void setChild_title(String child_title) {
this.child_title = child_title;
}
public String getDd() {
return dd;
}
public void setDd(String dd) {
this.dd = dd;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
MyBaseAdapter.java
package com.example.expandablelistview;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
public class MyBaseAdapter extends BaseExpandableListAdapter{
Context context;
ArrayList<group> group_al;
public MyBaseAdapter(Context context,ArrayList<group> group_al) {
this.context=context;
this.group_al=group_al;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<child> chList = group_al.get(groupPosition).getItems();
return chList.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) {
child ch = (child) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_items, null);
}
TextView child = (TextView) convertView.findViewById(R.id.child);
TextView dd = (TextView) convertView.findViewById(R.id.dd);
TextView date= (TextView) convertView.findViewById(R.id.date);
child.setText(ch.getChild_title().toString());
dd.setText(ch.getChild_title().toString());
date.setText(ch.getChild_title().toString());
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
ArrayList<child> chList = group_al.get(groupPosition).getItems();
return chList.size();
}
#Override
public Object getGroup(int groupPosition) {
return group_al.get(groupPosition);
}
#Override
public int getGroupCount() {
return group_al.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
group gr = (group) getGroup(groupPosition);
long group_id = getGroupId(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_items, null);
}
TextView title = (TextView) convertView.findViewById(R.id.title);
title.setText(gr.getName());
CheckBox chk=(CheckBox) convertView.findViewById(R.id.chk);
chk.setFocusable(false);
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int arg0, int arg1) {
return false;
}
}
MainActivity.java
package com.example.expandablelistview;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ExpandableListView;
public class MainActivity extends Activity {
private MyBaseAdapter ExpAdapter;
private ArrayList<group> ExpListItems;
private ExpandableListView ExpandList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandList = (ExpandableListView) findViewById(R.id.exp_lv);
ExpListItems = SetStandardGroups();
ExpAdapter = new MyBaseAdapter(MainActivity.this, ExpListItems);
ExpandList.setAdapter(ExpAdapter);
}
private ArrayList<group> SetStandardGroups() {
ArrayList<group> list = new ArrayList<group>();
group gru= new group();
gru.setName("Tax 1");
ArrayList<child> ch_list= new ArrayList<child>();
child ch = new child();
ch.setChild_title("Information of tax 1");
ch.setDd("Due Date:");
ch.setDate("22/02/2016");
ch_list.add(ch);
gru.setItems(ch_list);
list.add(gru);
return list;
}
}
When I run the application I can see the first row of ExpandableListView i.e. Tax1 with CheckBox beside it but when I click on the row then it does not expanded.
Please guys help me.
Thanks in advance.
please add:
android:focusable="false"
android:focusableInTouchMode="false"
to your checkbox
also remove from textView:
android:textIsSelectable="true"
**
<?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" >
<TextView
android:layout_width="270sp"
android:layout_height="30sp"
android:layout_marginLeft="30sp"
android:id="#+id/title"
****android:textIsSelectable="true"**// remove this line in group layout**
/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/chk"
/>
**
In MybaseAdapter please change method like this
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
child ch = (child) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_items, null);
}
TextView child = (TextView) convertView.findViewById(R.id.child);
TextView dd = (TextView) convertView.findViewById(R.id.dd);
TextView date= (TextView) convertView.findViewById(R.id.date);
child.setText(ch.getChild_title().toString());
dd.setText(ch.getDd().toString());
date.setText(ch.getDate().toString());
return convertView;
}
When I add
android:focusable="false"
android:focusableInTouchMode="false"
to your checkbox and
android:textIsSelectable="true"
to TextView then It works great
but now I want to store the checked CheckBox value (only group_title and value of Date TextView ) to the database so, where I add clickListener on Checkbox

Losing focus when notifyDataSetChanged() is called in ExpandableListView

I have an ExpandableListView which contains 2 TextViews and an EditText for entering amount for each child and it allows inline edit.
I need to to change the amount value for the group instantly when one of its child's amount changed.
I have done that but the problem is that when I called the notifyDataSetChanged() method for showing the change in group I lost the focus from the current edittext. That is, if I changed the amount field in the first child of a group and then click on the amount field of the second child, the focus is still on the first child because of the notifyDataSetChanged().
How can I solve this?
My code is below.
ExpandableListAdapter.java:
package com.example.empandroid.view;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import com.fort.empandroid.R;
import com.example.empandroid.model.Employee;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private final Context context;
private List<Employee> listDataHeader;
private HashMap<Integer, List<Employee>> listDataChild;
DecimalFormat df = new DecimalFormat("#.00");
int s;
Float value = 0.0f, change = 0.0f;
public ExpandableListAdapter(Context context, List<Employee> listDataHeader, HashMap<Integer, List<Employee>> listChildData) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listDataChild = listChildData;
//this.context=mainContxt.getApplicationContext();
}
public static class ListHolder {
Employee expCoa;
TextView txtName;
TextView txtAmount;
TextView txtMonthTotal;
EditText edtAmount;
}
#Override
public int getGroupCount() {
return this.listDataHeader.size();
}
#Override
public int getChildrenCount(int groupPosition) {
int childcount = this.listDataChild.get(this.listDataHeader.get(groupPosition).getiD()).size();
return childcount;
}
#Override
public Employee getGroup(int groupPosition) {
return this.listDataHeader.get(groupPosition);
}
#Override
public Employee getChild(int groupPosition, int childPosition) {
return this.listDataChild.get(this.listDataHeader.get(groupPosition).getiD()).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) {
df.setMinimumIntegerDigits(1);
ListHolder expHolder = new ListHolder();
expHolder.expCoa = getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
expHolder.txtName = (TextView) convertView.findViewById(R.id.txtAccount);
expHolder.txtAmount = (TextView) convertView.findViewById(R.id.txtAmount);
expHolder.txtMonthTotal = (TextView) convertView.findViewById(R.id.txtMonthTotal);
convertView.setTag(expHolder);
expHolder.txtName.setTypeface(null, Typeface.BOLD);
expHolder.txtAmount.setTypeface(null, Typeface.BOLD);
expHolder.txtMonthTotal.setTypeface(null, Typeface.BOLD);
setupItem(expHolder);
return convertView;
}
#Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
df.setMinimumIntegerDigits(1);
final ListHolder expHolder = new ListHolder();
expHolder.expCoa = getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
expHolder.txtName = (TextView) convertView.findViewById(R.id.txtAccount);
expHolder.edtAmount = (EditText) convertView.findViewById(R.id.edtAmount);
expHolder.txtMonthTotal = (TextView) convertView.findViewById(R.id.txtMonthTotal);
convertView.setTag(expHolder);
setupChildItem(expHolder);
//expHolder.edtAmount.requestFocus();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
final AlertDialog dialog = builder.create();
//AlertDialog.Builder alert = new AlertDialog.Builder(mainContxt);
expHolder.edtAmount.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
//dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
onChange(v, groupPosition, childPosition, expHolder.expCoa);
} else {
//Toast.makeText(context, " else "+s, 68000).show();
onFocus(v);
}
}
});
return convertView;
}
private void onFocus(View v) {
final EditText Caption = (EditText) v;
value = Float.parseFloat(Caption.getText().toString());
Toast.makeText(context, "onFocus value " + value + " change " + change, 68000).show();
}
public void onChange(View views, int groupPosition, int childPosition, Employee expCoaChd) {
final int position = views.getId();
final EditText Caption = (EditText) views;
change = Float.parseFloat(Caption.getText().toString());
if (value != change) { //use database type for this loop condition
//perform change
ListHolder expHolders = new ListHolder();
expHolders.expCoa = getGroup(groupPosition);
expHolders.expCoa.setAmount(expHolders.expCoa.getAmount() - expCoaChd.getAmount() + change);
expCoaChd.setAmount(change);
notifyDataSetChanged();
//Toast.makeText(context, "onChange value "+value+" change "+change, 68000).show();
change = 0.0f;
value = 0.0f;
}
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private void setupChildItem(ListHolder expHolder) {
expHolder.txtName.setText(expHolder.expCoa.getStrName());
if (expHolder.expCoa.getiD() == -998 || expHolder.expCoa.getiD() == -999) {
expHolder.edtAmount.setText("");
expHolder.txtMonthTotal.setText("");
} else {
expHolder.edtAmount.setText(df.format(expHolder.expCoa.getAmount()));
expHolder.txtMonthTotal.setText(df.format(expHolder.expCoa.getExpenseMTD()));
}
}
private void setupItem(ListHolder expHolder) {
expHolder.txtName.setText(expHolder.expCoa.getStrName());
if (expHolder.expCoa.getiD() == -998 || expHolder.expCoa.getiD() == -999) {
expHolder.txtAmount.setText("");
expHolder.txtMonthTotal.setText("");
} else {
expHolder.txtAmount.setText(df.format(expHolder.expCoa.getAmount()));
expHolder.txtMonthTotal.setText(df.format(expHolder.expCoa.getExpenseMTD()));
}
}
}
list_group.xml:
<?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"
>
<TextView
android:id="#+id/txtAccount"
android:layout_width="0dp"
android:layout_height="50dp"
android:textSize="16sp"
android:gravity="center_vertical"
android:layout_weight="3"/>
<TextView
android:id="#+id/txtAmount"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="end"
android:textSize="15sp"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<TextView
android:id="#+id/txtMonthTotal"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="end"
android:textSize="15sp"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</LinearLayout>
list_item.xml:
<?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"
>
<TextView
android:id="#+id/txtAccount"
android:layout_width="0dp"
android:layout_height="50dp"
android:textSize="15sp"
android:gravity="center_vertical"
android:layout_weight="3"/>
<EditText
android:id="#+id/edtAmount"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="end"
android:textSize="15sp"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<TextView
android:id="#+id/txtMonthTotal"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="end"
android:textSize="15sp"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</LinearLayout>

Android: CheckBox tree

In my application, I have a list and a button, in my list i want when i select parent item all of the child-item be selected indeed I want a Check-box tree. I use an Expandable List View but i have a problem in my code:
My code is:
list_group.xml:
<?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="wrap_content"
android:orientation="vertical"
android:padding="8dp"
android:background="#000000">
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_marginLeft="30dp"
android:layout_marginRight="2dp"
android:focusable="false"/>
<TextView
android:id="#+id/lblListHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="17dp"
android:textColor="#ffffff" />
</LinearLayout>
list_item2.xml:
<?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="55dip"
android:orientation="horizontal"
android:background="#ffffff">
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_marginLeft="30dp"
android:layout_marginRight="2dp"
android:focusable="false"/>
<TextView
android:id="#+id/lblListItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17dip"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_marginLeft="-40dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"/>
</LinearLayout>
backup_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<ExpandableListView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/expandableListView"/>
<Button
android:id="#+id/backup"
android:layout_gravity="center_horizontal"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="BackUp"
android:clickable="true" />
</LinearLayout>
How can i fix it?
ExpandableListAdapter:
package com.mCloud.android.ui.activity;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import com.mCloud.android.R;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.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_item2, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this._listDataHeader.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 lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition){
return true;
}
}
ParentActivity:
package com.mCloud.android.ui.activity;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ExpandableListView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.mCloud.android.Log_OC;
import com.mCloud.android.R;
import com.mCloud.android.filters.MediaFilenameFilter;
public class ParentActivity extends SherlockFragmentActivity {
private static final String TAG = "mCloudPreferences";
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.backup_list);
ActionBar actionBar = getSherlock().getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.expandableListView);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
super.onMenuItemSelected(featureId, item);
Intent intent;
switch (item.getItemId()) {
case android.R.id.home:
intent = new Intent(getBaseContext(), DisplayInfoActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
default:
Log_OC.w(TAG, "Unknown menu item triggered");
return false;
}
return true;
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataHeader.add("Files");
listDataHeader.add("Apps");
listDataHeader.add("SMS");
listDataHeader.add("Contacts");
listDataHeader.add("Call Logs");
listDataHeader.add("Calender");
listDataHeader.add("Setting");
List<String> files = new ArrayList<String>();
File file = Environment.getExternalStorageDirectory();
String[] list = file.list(new MediaFilenameFilter());
if (list != null) {
for (String name : list)
files.add(name);
}
files.add("Advance Selection");
List<String> apps = new ArrayList<String>();
apps.add("All Apps");
List<String> sms = new ArrayList<String>();
sms.add("Inbox");
sms.add("Draft");
sms.add("Send");
List<String> contacts = new ArrayList<String>();
contacts.add("All Contacts");
List<String> calllogs = new ArrayList<String>();
calllogs.add("Resived");
calllogs.add("Missed");
calllogs.add("Dailed");
List<String> calender = new ArrayList<String>();
calender.add("All calender");
List<String> setting = new ArrayList<String>();
setting.add("All setting");
listDataChild = new HashMap<String, List<String>>();
listDataChild.put(listDataHeader.get(0), files);
listDataChild.put(listDataHeader.get(1), apps);
listDataChild.put(listDataHeader.get(2), sms);
listDataChild.put(listDataHeader.get(3), contacts);
listDataChild.put(listDataHeader.get(4), calllogs);
listDataChild.put(listDataHeader.get(5), calender);
listDataChild.put(listDataHeader.get(6), setting);
}
}
but you do understand that ExpandableListView work like what I asked you..?
when you press on parent the parent expands (unless you state otherwise) and when you clikc on it again the parent collapses..
if you want to be able to expand the parent and also be able to check and uncheck the parent checkbox you need to give two views in the same row of the parent, the first is the checkbox and the second is the parent title (which can be empty like "")
they should be side by side.
you can do somthing like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Rl11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/desserts_main_expand"
android:orientation="horizontal" >
<ImageView
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:clickable="true"
android:cropToPadding="true"
android:padding="4dp"
android:scaleType="fitXY"
android:src="#android:drawable/ic_menu_add" />
<TextView
android:id="#+id/ParentTextI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_toLeftOf="#+id/addIng"
android:layout_toRightOf="#+id/Side_image"
android:gravity="center_vertical"
android:scrollHorizontally="true"
android:text="dummy text"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold|italic" />
</RelativeLayout>
and then in the adapter:
declare a global variable
CheckBox checkbox;
**this is only for the parent checkbox part
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
checkbox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(checkbox.isChecked()=true)
{checkbox.setChecked(false);}
else checkbox.setChecked(true);
ParentActivity.checkall();
}
});
and then in ParentActivity:
add a function that checks every child of the same parent:
public static void checkall()
{
for(int j=0 ; j<files.size() ; j++)
{if (listAdapter.checkbox.isChecked()==true)
{files.get(j).setChecked(true)};
}
I have made the code specially for you so there might be some error cause i havnt checked it..
but it supposed to be somthing like this.

Categories

Resources