Issue
I'm trying to create an ExpandibleListView with intents that switch to new classes. When using only one switch, there is no problem, but when I want different parents to switch diffent classes, only one of them works, the other one stays like empty (Its arrows turn up and down but nothing happens). How can I make them work together?
Here is my codes:
ExpandibleListAdapter.java
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.widget.TextView;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader;
private HashMap<String,List<String>> listHashMap;
public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listHashMap = listHashMap;
}
@Override
public int getGroupCount() {
return listDataHeader.size();
}
@Override
public int getChildrenCount(int i) {
return listHashMap.get(listDataHeader.get(i)).size();
}
@Override
public Object getGroup(int i) {
return listDataHeader.get(i);
}
@Override
public Object getChild(int i, int i1) {
return listHashMap.get(listDataHeader.get(i)).get(i1); // i = group item , i1= ChildItem
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup ViewGroup) {
String headerTitle = (String)getGroup(i);
if(view == null)
{
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_group,null);
}
TextView lblListHeader = (TextView)view.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return view;
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup ViewGroup) {
final String childText = (String)getChild(i,i1);
if(view == null)
{
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item,null);
}
TextView txtListChild = (TextView)view.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity
{
private ExpandableListView listView;
private ExpandableListAdapter listAdapter;
private List<String> listDataHeader;
private HashMap<String,List<String>> listHash;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ExpandableListView)findViewById(R.id.lvExp);
initData();
listAdapter = new ExpandableListAdapter(this,listDataHeader,listHash);
listView.setAdapter(listAdapter);
}
private void initData() {
listDataHeader = new ArrayList<>();
listHash = new HashMap<>();
listDataHeader.add("Line One");
listDataHeader.add("Line Two");
List<String> genel = new ArrayList<>();
**//The problem starts here**
listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
final String selected = (String) listAdapter.getGroup(groupPosition);
Intent intent;
switch (selected) {
case "Line One":
intent = new Intent(MainActivity.this,LineOne.class);
startActivity(intent);
break;
}
return false; //return true doesn't let the other parents to open.
}
});
List<String> terims = new ArrayList<>();
listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
final String terims = (String) listAdapter.getGroup(groupPosition);
Intent niyet;//I used "intent" instead of "niyet" as same above but nothing has changed.
switch (terims) {
case "Line Two":
niyet = new Intent(MainActivity.this, LineTwo.class);
startActivity(niyet);
break;
}
return false;
}
});
listHash.put(listDataHeader.get(0),genel);
listHash.put(listDataHeader.get(1),terims);
}
}
Solution
You cannot set 2 OnGroupClickListeners. The second one cancels the first one. You should set one listener and then check for both sets of conditions in the one listener.
Answered By - David Wasser
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.