I have a problem, I'm doing an android application and I want to do a list a question and when we click on one question it scroll a list of answer (in a fragment).
I did a Custom adapter and I want to add a footer after the list of answer for each question (I want to add a edittext to add an anwser).
And when I scroll the list of answer for one question it's working well with the footer but when I try to scroll a second time the same list, he say me that the textview is null...
Here the code :
-> adapter
public class MyExpandableAdapter extends BaseExpandableListAdapter {
private Context context;
private LinkedList<Question> groups;
public MyExpandableAdapter(Context context, LinkedList<Question> groups)
{
this.context = context;
this.groups = groups;
}
#Override
public int getGroupCount() {
return groups.size();
}
//Add 2 to childcount. The first row and the last row are used as header and footer to childview
#Override
public int getChildrenCount(int i) {
return groups.get(i).getListAnswer().size() + 1 ;
}
#Override
public Question getGroup(int i) {
return groups.get(i);
}
#Override
public Answer getChild(int i, int i2) {
return groups.get(i).getListAnswer().get(i2);
}
#Override
public long getGroupId(int i) {
return i;
}
#Override
public long getChildId(int i, int i2) {
return 0;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup)
{
Question currentQuestion = groups.get(i);
if(view == null)
{
LayoutInflater inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.listquestion_home,null);
}
((CheckedTextView) view.findViewById(R.id.listrow_group_question)).setText(currentQuestion.getQuestion());
((CheckedTextView) view.findViewById(R.id.listrow_group_question)).setChecked(true);
((TextView) view.findViewById(R.id.listrow_group_author)).setText("Anonymous" + currentQuestion.getUserId().toString());
return view;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean b, View convertView, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Here is the ListView of the ChildView
if (childPosition < getChildrenCount(groupPosition) - 1)
{
Answer answer = getChild(groupPosition, childPosition);
if (convertView == null) {
convertView = inflater.inflate(R.layout.listanswer_home, null);
}
TextView text = (TextView) convertView.findViewById(R.id.textView1);
text.setText(answer.getAnswer());
//the last row is used as footer
}
else
{
if (convertView == null) {
convertView = inflater.inflate(R.layout.test_edit_text, null);
}
}
return convertView;
}
#Override
public boolean isChildSelectable(int i, int i2) {
return false;
}
}
-> Fragment code :
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home_fragment, container, false);
ExpandableListView listView = (ExpandableListView) rootView.findViewById(R.id.listView);
MyExpandableAdapter adapter = new MyExpandableAdapter(getActivity(), QuestionManager.getQuestionManager().getQuestionWithAnswerNotFromMe());
listView.setAdapter(adapter);
return rootView;
}
XML code :
-> Answer
<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="40dp"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="40dp"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="5dp"
android:gravity="center_vertical"
android:text="hey"
android:textSize="14sp"
android:textStyle="bold" >
</TextView>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/black" />
-> Question
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
>
<CheckedTextView
android:id="#+id/listrow_group_question"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="8dp"
android:gravity="left"
android:paddingLeft="32dp"
android:paddingTop="8dp"
android:text="Test"
android:textSize="14sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listrow_group_author"
android:layout_below="#id/listrow_group_question"
android:gravity="right"
/>
-> Footer
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
>
<TextView
android:id="#+id/txtFooter"
android:textColor="#android:color/holo_green_light"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:textStyle="bold"
android:text="Footer" />
The error is in the adapter :
text.setText(answer.getAnswer());
I really don't understand. I add one of the size of child for it but the bug appear when I do this...
If you have any idea :'( !
Thank you
Log :
01-06 15:03:39.845 23899-23899/com.example.thibault.project_mobile E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
01-06 15:03:39.845 23899-23899/com.example.thibault.project_mobile E/AndroidRuntime: at com.example.thibault.project_mobile.MyExpandableAdapter.getChildView(MyExpandableAdapter.java:92)
01-06 15:03:39.845 23899-23899/com.example.thibault.project_mobile E/AndroidRuntime: at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:451)
The error is in the adapter : text.setText(answer.getAnswer());
Because TextView with textView1 id is only available in listanswer_home.xml layout not in test_edit_text.xml and if if (childPosition < getChildrenCount(groupPosition) - 1) condition is false retuning test_edit_text.xml layout from getChildView and convertView.findViewById(R.id.textView1); return null.
Either add null check before calling setText method of TextView as:
if(text !=null){
text.setText(answer.getAnswer());
}
or add TextView with textView1 in listanswer_home.xml.
Related
In My expandable list view first child item has two buttons.button1 and button2.in other child list item has check box and text.these check boxes are default disabled
in first time user expand the child list and after press button1 i want to enable all check boxes in particular group
As this point i can only enable first child list item checkbox.how can enable all check boxes by clicking button on the 1st child item.
Expandable list adapter
public class ExpandListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<ExpandableListGroup> groups;
public ExpandListAdapter(Context context, ArrayList<ExpandableListGroup> groups) {
this.context = context;
this.groups = groups;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<ExpandableListChild> chList = groups.get(groupPosition).getItems();
return chList.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final ExpandableListChild child = (ExpandableListChild) getChild(groupPosition, childPosition);
final ViewHolder holder;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.btn_start_inspection = (Button) convertView.findViewById(R.id.btn_start_inspection);
holder.btn_complete_inspection = (Button) convertView.findViewById(R.id.btn_complete_inspection);
holder.cbx_complete_inspection = (CheckBox) convertView.findViewById(R.id.check_sub_task);
holder.txt_activity_name = (TextView) convertView.findViewById(R.id.txt_sub_task_title);
holder.txt_activity_name.setText(child.getName().toString());
//button for enable checkboxes
holder.btn_start_inspection.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.btn_complete_inspection.setBackgroundColor(Color.RED);
holder.itemEnabled = true;
notifyDataSetChanged();
}
});
//enable check boxes
if(holder.itemEnabled == true){
holder.cbx_complete_inspection.setEnabled(true);
}else{
holder.cbx_complete_inspection.setEnabled(false);
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
ArrayList<ExpandableListChild> chList = groups.get(groupPosition).getItems();
return chList.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;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ExpandableListGroup group = (ExpandableListGroup) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.list_group, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.txt_company_name);
tv.setText(group.getName());
TextView tvAddress = (TextView) convertView.findViewById(R.id.txt_company_address);
tvAddress.setText(group.getAddressLevel());
iv = (ImageView) convertView.findViewById(R.id.img_expand);
return convertView;
}
#Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
iv.setImageDrawable(context.getResources().getDrawable(R.drawable.exp_fold));
}
#Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
iv.setImageDrawable(context.getResources().getDrawable(R.drawable.exp_min));
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
static class ViewHolder {
Button btn_complete_inspection;
Button btn_start_inspection;
CheckBox cbx_complete_inspection;
TextView txt_activity_name;
boolean itemEnabled = false;
}
}
listchildItem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lnrChild"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/lnr_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp">
<Button
android:id="#+id/btn_start_inspection"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_gravity="left"
android:layout_marginLeft="10dp"
android:background="#drawable/rounded_cornerexpandable_list_child"
android:gravity="center"
android:text="Start Inspection"
android:textAllCaps="false"
android:textColor="#color/color_white"
android:textSize="12sp"
></Button>
<Button
android:id="#+id/btn_complete_inspection"
android:layout_width="150dp"
android:layout_height="35dp"
android:layout_marginLeft="150dp"
android:src="#drawable/rounded_cornerexpandable_list_child_gray"
android:text="Complete Inspection"
android:textAllCaps="false"
android:textColor="#color/color_white"
android:textSize="12sp"
></Button>
<LinearLayout
android:id="#+id/lnr_active_sub_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/btn_complete_inspection">
<LinearLayout
android:id="#+id/lnr_sub_task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:gravity="center"
android:orientation="horizontal">
<CheckBox
android:id="#+id/check_sub_task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:checked="false"
android:gravity="center"></CheckBox>
</LinearLayout>
<LinearLayout
android:id="#+id/textHolder"
android:layout_width="0.0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:gravity="left"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_sub_task_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="exx"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0.0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="0.5"
android:gravity="right"
android:orientation="horizontal">
<ImageView
android:id="#+id/txt_sub_task_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_keyboard_arrow_right_black_24dp"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
What I see is that after click button on 1st list item only 1st check box in the 1st child item is enabled, the other list item check boxes are not. What I want is that checkboxes in the child item rows are enabled. (extra lines added from comments)
I have a system of nested ExpandableListViews that continue on for a few generations (a tree-type layout) for comments. Each top-level comment can have comments with children on those comments. To do so, I have a main ExpandableListView to store all the top level comments, and each "comment" XML contains an ExpandableListView for any child comments. The adapter for my ExpandableListView is as follows
public class ExpandableCommentAdapter extends BaseExpandableListAdapter {
private static final class ViewHolder {
TextView textLabel;
}
private final List<CommentNode> itemList;
private final LayoutInflater inflater;
private Context main;
public ExpandableCommentAdapter(Context context, List<CommentNode> itemList) {
this.inflater = LayoutInflater.from(context);
this.itemList = itemList;
main = context;
}
#Override
public CommentNode getChild(int groupPosition, int childPosition) {
CommentNode n = itemList.get(groupPosition);
ArrayList<CommentNode> comments = new ArrayList<CommentNode>();
for (CommentNode node : n.walkTree(TraversalMethod.BREADTH_FIRST)) {
if (node.getParent().getComment().getId() == n.getComment().getId()) {
comments.add(node);
}
}
Log.v("RedditSlide", "COMMENT CHILD SIZE:" + comments.size());
return comments.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public int getChildrenCount(int groupPosition) {
CommentNode n = itemList.get(groupPosition);
ArrayList<CommentNode> comments = new ArrayList<CommentNode>();
CommentNode comment = n;
for (CommentNode node : comment.walkTree(TraversalMethod.BREADTH_FIRST)) {
if (node.getParent().getComment().getId() == comment.getComment().getId()) {
comments.add(node);
}
}
return comments.size();
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
final ViewGroup parent) {
final CommentNode user = getChild(groupPosition, childPosition);
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment, parent, false);
}
TextView author = (TextView) convertView.findViewById(R.id.author);
TextView comm = (TextView) convertView.findViewById(R.id.commentLine);
TextView upvote = (TextView) convertView.findViewById(R.id.upvotePost);
author.setText(user.getComment().getAuthor());
comm.setText(user.getComment().getBody());
upvote.setText(user.getComment().getScore() + "");
ArrayList<CommentNode> comments = new ArrayList<CommentNode>();
CommentNode comment = user;
for (CommentNode node : comment.walkTree(TraversalMethod.BREADTH_FIRST)) {
if (node.getParent().getComment().getId() == comment.getComment().getId()) {
comments.add(node);
}
}
ExpandableCommentAdapter adapter = new ExpandableCommentAdapter(convertView.getContext(), comments);
ExpandableListView listView = (ExpandableListView) convertView.findViewById(R.id.commentsListUnder);
listView.setAdapter(adapter);
return convertView;
}
#Override
public CommentNode getGroup(int groupPosition) {
return itemList.get(groupPosition);
}
#Override
public int getGroupCount() {
return itemList.size();
}
#Override
public long getGroupId(final int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
final CommentNode user = itemList.get(groupPosition);
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment, parent, false);
}
TextView author = (TextView) convertView.findViewById(R.id.author);
TextView comm = (TextView) convertView.findViewById(R.id.commentLine);
TextView upvote = (TextView) convertView.findViewById(R.id.upvotePost);
author.setText(user.getComment().getAuthor());
comm.setText(user.getComment().getBody());
upvote.setText(user.getComment().getScore() + "");
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
My Comment XML is as follows
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-20dp"
android:layout_marginRight="-20dp"
android:background="#ff2b2b2b"
android:orientation="vertical"
android:padding="20dp"
android:paddingBottom="0dp"
android:paddingEnd="0dp"
android:scrollbars="none">
<TextView
android:id="#+id/author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ccrama"
android:textSize="12dp" />
<TextView
android:id="#+id/commentLine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="COMMENT"
android:textSize="14dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="-10dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/upvote"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"
android:background="#drawable/roundbutton"
android:onClick="upvote"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/iconupvote"
/>
<ImageView
android:id="#+id/downvote"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="-7dp"
android:background="#drawable/roundbutton"
android:onClick="downvote"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/icondownvote"
/>
<TextView
android:id="#+id/upvotePost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="7dp"
android:text="30"
android:textSize="14dp" />
</LinearLayout>
<ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/commentsListUnder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-20dp"
android:layout_marginLeft="-5dp"
android:layout_marginRight="-20dp"
android:background="#ff2b2b2b"
android:orientation="vertical" />
</LinearLayout>
The issue I'm having is the "expandableness" of the child comments seems to skip a generation. Level 1, Level 3, etc all have expansion and their children are visible, but Level 2 can't expand, and only one child is visible. Further children (4, 5, 6...) aren't visible, and Level 3 has an expanding icon but when tapped shows no children.
What am I doing wrong with my layout/system?
Thanks!
Your issues are two things. One you are attempting to nest scrollable views that scroll in the same direction. Android can not handle this situation correctly and you will get all sorts of crazy behavior. Attempting to "hack" around the issues will just cause all sorts of headaches.
The second issues is that you are embedding adapters within adapters. While this "could" work, it is really not recommended. There is no guarantee to how many times a getChildView or getParentView will be called for the same position number. Sometimes it's once. Sometimes it's twice. Heck it could be 4 times. Each time you'd be recreating a new adapter that in-turn will invoke it's getChildView or getParentView methods multiple times per position. To put simply, it's a major performance concern.
Your best solution is to rework a UX design that does not entail embedding ExpandableListViews and adapters.
My problem is, that I have an activity with one big TextView and one ExpandableListView underneath the TextView. Unfortunately the text of the TextView is too long, so I can't see the details when I expand the ExpandableListView.
So my question is, how can I use CWAC-Merge in my Code, to get one big scrollable Activity.
I would like to have, that everything in the activity_contact.xml from scrollView1 to the bottom is scrollable! I already copied the necessary libs (merge-1.0.1.jar and sacklist-1.0.0.jar) to my Project, but I don't know how to apply the MergeAdapter. I would really appreciate, if someone could tell me where and how I have to use the MergeAdapter!
This is my activity_contact.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/titlebar" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:fillViewport="true" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="#string/contact"
android:textSize="#dimen/textSize" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ExpandableListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" >
</ExpandableListView>
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
This is my Contact.java class:
public class Contact extends Activity {
private SparseArray<Group> groups = new SparseArray<Group>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
createData();
ExpandableListAdapter adapter = new ExpandableListAdapter(this, groups,
false, null);
listView.setAdapter(adapter);
}
private void createData() {
Group group1 = new Group(getString(R.string.stringGroup));
group1.children.add(getString(R.string.stringGroup2));
groups.append(0, group1);
}
}
This is my ExpandableListAdapter.java
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private final SparseArray<Group> groups;
private LayoutInflater inflater;
private Activity activity;
private boolean setOnClickListener;
private Class<?> onClickClass;
public ExpandableListAdapter(Activity act, SparseArray<Group> groups,
boolean setOnClickListener, Class<?> onClickListenerClass) {
activity = act;
this.groups = groups;
inflater = act.getLayoutInflater();
this.setOnClickListener = setOnClickListener;
this.onClickClass = onClickListenerClass;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return groups.get(groupPosition).children.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String children = (String) getChild(groupPosition, childPosition);
TextView text = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_details, null);
}
text = (TextView) convertView.findViewById(R.id.copyrightTextView);
text.setText(Html.fromHtml(children));
if (setOnClickListener) {
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(activity, onClickClass);
intent.putExtra("name", children);
activity.startActivity(intent);
}
});
}
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return groups.get(groupPosition).children.size();
}
#Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
#Override
public int getGroupCount() {
return groups.size();
}
#Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
#Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
#Override
public long getGroupId(int groupPosition) {
return 0;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_group, null);
}
Group group = (Group) getGroup(groupPosition);
((CheckedTextView) convertView).setText(group.string);
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
The listrow_details.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="50dp"
android:clickable="true"
android:orientation="vertical" >
<TextView
android:id="#+id/copyrightTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="15dp"
android:gravity="center_vertical"
android:linksClickable="true"
android:padding="10dp"
android:textStyle="bold" >
</TextView>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#android:color/black" />
</LinearLayout>
MergeAdapter is a ListAdapter, not an ExpandableListAdapter. You cannot use MergeAdapter to solve your problem. You are welcome to read through the source code to MergeAdapter and attempt to figure out if there is a way to create a MergeExpandableListAdapter or something like that.
Or, come up with a UI that does not have a long TextView after the ExpandableListView.
I need to use Android ExpandableListView with custom layout for each group(groups have one child). To accomplish this, I've created a class which extends BaseExpandableListAdapter. On the getChildView method, I've created the view of each child. It works. When I click a group, its child comes with true layout. However, during the debuging, I see that it triggers getchildview method many times, so it is too slow to display child. Because there are a lot of operations on each child (db operations, dynamic sub layouts...).
There are a small project, which similar to my project(Logic and code structure are same, only layout code and code of creating each child view are different).
public class MainActivity extends Activity {
private ArrayList<String> groups;
private ArrayList<ArrayList<ArrayList<String>>> childs;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableListView l = (ExpandableListView)findViewById(R.id.expandableListView1);
loadData();
final myExpandableAdapter adapter = new myExpandableAdapter(this, groups, childs);
l.setAdapter(adapter);
}
public class myExpandableAdapter extends BaseExpandableListAdapter {
private ArrayList<String> groups;
private ArrayList<ArrayList<ArrayList<String>>> children;
private Context context;
public myExpandableAdapter(Context context, ArrayList<String> groups, ArrayList<ArrayList<ArrayList<String>>> children) {
this.context = context;
this.groups = groups;
this.children = childs;
}
#Override
public boolean areAllItemsEnabled()
{
return true;
}
public ArrayList<String> getChild(int groupPosition, int childPosition) {
return children.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,View convertView, ViewGroup parent) {
String child=(String)getChild(groupPosition, childPosition).get(0);
switch (groupPosition){
case 0:
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_row, null);
TextView tvPlayerName = (TextView) convertView.findViewById(R.id.tvPlayerName);
tvPlayerName.setText(child);
break;
case 1:
LayoutInflater inflater1 = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater1.inflate(R.layout.child_row1, null);
TextView tvPlayerName1 = (TextView) convertView.findViewById(R.id.tvPlayerName);
tvPlayerName1.setText(child);
Button btn=(Button)convertView.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TextView tv=(TextView)findViewById(R.id.tv);
tv.setText("Clicked");
}
});
break;
case 2:
LayoutInflater inflater2 = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater2.inflate(R.layout.child_row1, null);
TextView tvPlayerName12 = (TextView) convertView.findViewById(R.id.tvPlayerName);
tvPlayerName12.setText(child);
Button btn2=(Button)convertView.findViewById(R.id.button1);
btn2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TextView tv=(TextView)findViewById(R.id.tv);
tv.setText("Clicked");
}
});
break;
}
return convertView;
}
public int getChildrenCount(int groupPosition) {
return children.get(groupPosition).size();
}
public String 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) {
String group = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.expandablelistview_group, null);
}
TextView grouptxt = (TextView) convertView.findViewById(R.id.TextViewGroup);
grouptxt.setText(group);
return convertView;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}
private void loadData(){
groups= new ArrayList<String>();
childs= new ArrayList<ArrayList<ArrayList<String>>>();
groups.add("Group 1");
groups.add("Group 2");
groups.add("Group 3");
childs.add(new ArrayList<ArrayList<String>>());
childs.get(0).add(new ArrayList<String>());
childs.get(0).get(0).add("Child 1 group 1");
childs.add(new ArrayList<ArrayList<String>>());
childs.get(1).add(new ArrayList<String>());
childs.get(1).get(0).add("Child 1 group 2");
childs.add(new ArrayList<ArrayList<String>>());
childs.get(2).add(new ArrayList<String>());
childs.get(2).get(0).add("Child 1 group 3");
}}
During debugging, If I click any group, it comes to String child=(String)getChild(groupPosition, childPosition).get(0); line. After break; }return convertView;, it comes to there again and again, and then it displays layout.
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" >
<ExpandableListView
android:id="#+id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="34dp" >
</ExpandableListView>
expandablelistview_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="fill_parent">
<TextView
android:id="#+id/TextViewGroup"
android:layout_width="wrap_content"
android:layout_height="50px"
android:layout_marginLeft="50px"
android:gravity="center_vertical"
>
</TextView>
child_row.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:id="#+id/tvPlayerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
childrow1.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/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Row1" />
<TextView
android:id="#+id/tvPlayerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
Is there any suggestion to solve this.
Thanks
instead of inflating the view from getChildView use Holder read here
Try changing your ExpandableListView xml and instead of android:layout_height="wrap_content", put android:layout_height="fill_parent"
I have an expandable listview. I have put some dummy data and evrything seems ok but when I run the application all groups are in collapse mode and they don't take effect when I click on each of them. This is the screenshot:
XML of Group is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/bg_slider"
android:orientation="horizontal" >
<TextView
android:id="#+id/tvRecipeName"
style="#style/normal_text.bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="5dp"
android:focusable="false"
android:lines="1"
android:maxLines="1"
android:text="#string/dd_title" />
<ImageButton
android:id="#+id/ibDeleteRecipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:background="#color/transparent"
android:contentDescription="#string/cd"
android:focusable="false"
android:src="#android:drawable/ic_menu_delete" />
<View
android:layout_width="1dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/tvRecipeName"
android:focusable="false" />
</RelativeLayout>
XML of child is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/White"
android:gravity="left|center_vertical" >
<View
android:layout_width="1dp"
android:layout_height="35dp"
android:layout_toLeftOf="#+id/tvIngredient"
android:focusable="false" />
<TextView
android:id="#+id/tvIngredient"
style="#style/normal_text_black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="5dp"
android:lines="1"
android:maxLines="1"
android:text="#string/dd_title"
android:focusable="false" />
<ImageButton
android:id="#+id/ibDeleteIngredient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:background="#color/transparent"
android:contentDescription="#string/cd"
android:focusable="false"
android:src="#android:drawable/btn_dialog" />
</RelativeLayout>
and in main.xml, definition of expandable list is:
<ExpandableListView
android:id="#+id/elv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I have an adapter that I have written this code for that:
public class ExpAdapter extends BaseExpandableListAdapter {
private final String TAG = "ExpAdapter";
private Context context;
static final String arrGroupelements[] = {"India", "Australia", "England", "South Africa"};
static final String arrChildelements[][] = { {"Sachin Tendulkar", "Raina", "Dhoni", "Yuvi" },
{"Ponting", "Adam Gilchrist", "Michael Clarke"},
{"Andrew Strauss", "kevin Peterson", "Nasser Hussain"},
{"Graeme Smith", "AB de villiers", "Jacques Kallis"} };
public ExpAdapter(Context context) {
this.context = context;
Log.i(TAG, "Adapter created.");
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.elv_child, null);
}
TextView tvItem = (TextView) convertView.findViewById(R.id.tvIngredient);
ImageButton ibDelete = (ImageButton) convertView.findViewById(R.id.ibDeleteIngredient);
ibDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "******");
}
});
tvItem.setText(arrChildelements[groupPosition][childPosition]);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return arrChildelements[groupPosition].length;
}
#Override
public Object getGroup(int groupPosition) {
return null;
}
#Override
public int getGroupCount() {
return arrGroupelements.length;
}
#Override
public long getGroupId(int groupPosition) {
return 0;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.elv_group, null);
}
TextView tvItem = (TextView) convertView.findViewById(R.id.tvRecipeName);
ImageButton ibDeleteRcipe = (ImageButton) convertView.findViewById(R.id.ibDeleteRecipe);
ibDeleteRcipe.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "%%%%%%");
}
});
tvItem.setText(arrGroupelements[groupPosition]);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
and finally, in code of fragment activity i have:
public class ShoppingList extends FragmentActivity {
ExpAdapter adapter = new ExpAdapter(this);
//Linking expnadable list view
expListView = (ExpandableListView) findViewById(R.id.elv);
expListView.setAdapter(adapter);
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
Log.i(TAG, "Group " + groupPosition + " expanded.");
}
});
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Log.i(TAG, "Group " + groupPosition + " collapsed.");
}
});
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Log.i(TAG, "item " + childPosition + " of group " + groupPosition + " clicked.");
return false;
}
});
}
When I run the application and click on parents, nothing happens. I tried to add following lines of code at the end of shopping class:
int count = adapter.getGroupCount();
for (int position = 1; position <= count; position++)
expListView.expandGroup(position - 1);
Now when I run the application result is like this screenshot:
If I click on delete buttons (parent or child), I can see they take effect (as I check logcat), however when I click on child or parent nothing happens. So, I have no idea why callbacks doesn't work. Based on my research I found that I need to set Focussable of image buttons to false. As you can see in XML files I did it but still when I click on parent and child rows I can't see any response.
Finally I found the solution :)
I have no idea is it bug or disease or ...
As mentioned above based on my research I found that we need to set focus-ability of image view/box to "false". I did it in XML file and it didn't work. I set focus-ability to "true" in XML and in code set it to false. like this:
#Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.elv_group, null);
}
TextView tvItem = (TextView) convertView.findViewById(R.id.tvRecipeName);
ImageButton ibDeleteRcipe = (ImageButton) convertView.findViewById(R.id.ibDeleteRecipe);
ibDeleteRcipe.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
...
}
});
ibDeleteRcipe.setFocusable(false);
tvItem.setText(arrGroupElements[groupPosition]);
return convertView;
}
Therefore based of my achievement! it should be set to "false" in code not in XML! Now the question is What is difference? -I have no idea.
It is necessary to define android:focusable="false" in the layout xml for focusable views within the expandable list view item but not in the "ExpandableListView" attribute.
Eg: if a custom parent list item includes a checkbox (it will automatically get the focus) it should be like below.
<CheckBox
android:id="#+id/cbkSelectDownload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"/>