Why is custom ExpandableListView not displayed in android - java

I am trying to make custom ExpandableListView having child or node. But some parents don't have children. I don't want to show anything. In other words
I need to display like that
Parent 1
parent 2
child1
child2
child3
parent 3
parent 4
child1
So I try like this I make a xml route_dashboard.xml
<?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="match_parent" >
<ExpandableListView
android:id="#+id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
>
</ExpandableListView>
</RelativeLayout>
custom adapter
public class CustomExpendableListView extends BaseExpandableListAdapter {
private String[] fatherName={"naveen","ravi","sharma","uncle","rahat"};
String[] raviChildrenname={"abc","pqr","mnn"};
String[] sharmaChildrenname={"zxa","yh","er"};
Context context;
public CustomExpendableListView(Context context) {
this.context=context;
}
#Override
public int getGroupCount() {
return 0;
}
#Override
public int getChildrenCount(int groupPosition) {
return 0;
}
#Override
public Object getGroup(int groupPosition) {
return null;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
#Override
public long getGroupId(int groupPosition) {
return 0;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.parenttextview, null);
}
TextView item = (TextView) convertView.findViewById(R.id.parentTextView);
item.setText(fatherName[groupPosition]);
return item;
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
return null;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
call main function
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.route_dashboard);
expendablelistView = (ExpandableListView) findViewById(R.id.expandableListView1);
adapter = new CustomExpendableListView(this);
expendablelistView.setAdapter(adapter);
}
child.xml
<?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="match_parent" >
<TextView
android:id="#+id/childTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
parenttextviewxml
<?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="match_parent" >
<TextView
android:id="#+id/parentTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
I take three array
private String[] fatherName={"naveen","ravi","sharma","uncle","rahat"};
String[] raviChildrenname={"abc","pqr","mnn"};
String[] sharmaChildrenname={"zxa","yh","er"};
fatherName is the parents nodes.raviChildrenname is the child of ravi parent node .sharmaChildrenname is the child of sharma
could you please give some code.?

Your getGroupCount method must return value greater than zero. In this case only list view will display something
Something like that:
#Override
public int getGroupCount() {
return fatherName.length;
}
#Override
public int getChildrenCount(int groupPosition) {
if (fatherName[groupPosition].equalsIgnoreCase("ravi")) {
return raviChildrenname.length;
}
if (fatherName[groupPosition].equalsIgnoreCase("sharma")) {
return sharmaChildrenname.length;
}
return 0;
}
You also need to implement getChildView method for this listView:
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.childView, null);
}
TextView item = (TextView) convertView.findViewById(R.id.childTextView);
if (fatherName[groupPosition].equalsIgnoreCase("ravi")) {
item.setText(raviChildrenname[childPosition]);
}
if (fatherName[groupPosition].equalsIgnoreCase("sharma")) {
item.setText( sharmaChildrenname[childPosition]);
}
return item;
}

Related

Trying to create a custom ArrayAdapter with ArrayList

I'm trying to get ArrayList with different variables that i'm getting from a database to display in a customized listview
this is the main xml
<?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="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View the balance sheet"
android:textSize="29sp"
android:id="#+id/view"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="36dp" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView">
</ListView>
</RelativeLayout>
this is the customized layout
<?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="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/description_names"
android:text="description"
android:layout_marginTop="25sp"
android:layout_marginLeft="15sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/values_names"
android:text="value"
android:layout_marginTop="25sp"
android:layout_marginLeft="105sp"/>
</RelativeLayout>
this is the adapter, i wanted both the ArrayLists from the main activity but it's not working
class CustomAdapter extends ArrayAdapter<String> {
CustomAdapter(#NonNull Context context, ArrayList<String> mDetails,ArrayList<String> mValues) {
super(context,R.layout.custom_layout ,mDetails);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater inflater=LayoutInflater.from(getContext());
View customView=inflater.inflate(R.layout.custom_layout,parent,false);
String details=getItem(position);
String values=getItem(position);
TextView description_names=(TextView)customView.findViewById(R.id.description_names);
TextView values_names=(TextView)customView.findViewById(R.id.values_names);
description_names.setText(details);
return customView;
}
}
main activity
ListView listvIew=(ListView)findViewById(R.id.listView);
final ArrayAdapter<String>array=new CustomAdapter(this,mDetails,mValues);
listvIew.setAdapter(array);
private ArrayList<String>mDetails=new ArrayList<>();
private ArrayList<String>mValues=new ArrayList<>();
V3Ref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value=dataSnapshot.child("records").child("values").getValue(String.class);
Log.d(TAG, "onChildAdded: the value is"+value);
mDetails.add(value);
mValues.add(value);
array.notifyDataSetChanged();
}
i'm getting values from the database then putting them in ArrayLists then i'm trying to display them in a customized listview but it's not working when i try to use the second Arraylist
adapter calls from your main activity should be like,
CusatomeAdapter adapter;
adapter = new CusatomeAdapter(mActivity,mList1 ,mList2);
mListview.setAdapter(adapter);
here is your adapter calss
public class CusatomeAdapter extends BaseAdapter {
public ArrayList<String> mList1;
public ArrayList<String> mList2;
Activity activity;
public CusatomeAdapter(Activity activity,ArrayList<String> mList1 , ArrayList<String> mList2){
super();
this.activity = activity;
this.mList1 = mList1;
this.mList2 = mList2;
}
#Override
public int getCount() {
return mList1.size(); //give the size of list here
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_detail, null);
holder = new ViewHolder();
holder.tv_detail = (TextView) convertView.findViewById(R.id.tv_detail);
//same way define another text view and set their value ..
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_detail.setText(mList1.get(position).toString());
return convertView;
}
private class ViewHolder {
TextView tv_detail;
}
}

List onItemClickListener not working with

<?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:paddingLeft="10dp"
android:paddingStart="10dp"
android:paddingRight="10dp"
android:paddingEnd="10dp"
android:focusable="false"
android:focusableInTouchMode="false">
<android.support.v7.widget.CardView
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="60dp"
android:padding="5dp"
android:elevation="6dp">
<TextView
android:id="#+id/major_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#color/black"
android:textSize="#dimen/text_size_large"
android:text="Computer Science"
android:gravity="center" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/subject_scroll"
android:paddingBottom="8dp"
android:paddingTop="10dp"
android:paddingLeft="15dp"
android:paddingStart="15dp"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
`This is dropdown.xml which is the view that will be inside the listview.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/courses_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:orientation="vertical"
android:clickable="false">
</ListView>
</LinearLayout>
main.xml
the listview with id couses_list with contain the dropdown.xml view
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("ListView", "onItemClick: true");
});
I have tried setting the setFocusable() to false to everyview inside listview and other method shown in precious solutions in stack overflow but nothing worked for me.What wrong am i doing here..
ListView uses MajorAdapter which extends BindableAdapter
public class MajorAdapter extends BindableAdapter<Major> {
public class ViewHolder {
TextView majorName;
private boolean isOpen = false;
private RecyclerView subjectRecyclerView;
Major major;
List<Subject> subjects = new ArrayList<>();
ViewHolder(View view){
majorName = (TextView)view.findViewById(R.id.major_name);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(view.getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
subjectRecyclerView = (RecyclerView)view.findViewById(R.id.subject_scroll);
subjectRecyclerView.setLayoutManager(linearLayoutManager);
SubjectAdapter subjectAdapter = new SubjectAdapter(subjects);
subjectRecyclerView.setAdapter(subjectAdapter);
subjectRecyclerView.addItemDecoration(new RecyclerListDecorater(view.getContext()));
}
public void toggle(){
if(this.isOpen)
this.isOpen = false;
else
this.isOpen = true;
}
public boolean isOpen(){
return this.isOpen;
}
public Model getMajor(){
return major;
}
public RecyclerView getRecyclerView(){
return this.subjectRecyclerView;
}
}
public MajorAdapter(Context context){
super(context);
}
#Override
public View getNewView(LayoutInflater inflater, int position, ViewGroup container) {
View view = inflater.inflate(R.layout.majors,null,false);
view.setFocusable(false);
ViewHolder holder = new ViewHolder(view);
view.setTag(holder);
return view;
}
#Override
public void bindView(Major item, int position, View view) {
ViewHolder holder= (ViewHolder)view.getTag();
holder.major = item;
holder.majorName.setText(item.getCourseName());
}
}
here is the bindable adapter
public abstract class BindableAdapter<T> extends ArrayAdapter<T> {
private LayoutInflater inflater;
public BindableAdapter(Context context){
super(context,0);
setup(context);
}
private void setup(Context context){
inflater = LayoutInflater.from(context);
}
#Override
public final View getView(int position, View view, ViewGroup container){
if(view == null){
view = getNewView(inflater, position, container);
if(view == null)
throw new IllegalStateException("View created cannot be null");
}
bindView(getItem(position), position, view);
return view;
}
public abstract View getNewView(LayoutInflater inflater, int position, ViewGroup container);
public abstract void bindView(T item, int position, View view);
#Override
public View getDropDownView(int position, View view, ViewGroup parent){
return getView(position, view, parent);
}
}

Can't click on ListView after including Spinner inside ListView

I have used the below XML on my custom adapter class in row XML file and also added my custom adapter class. When I click to Spinner after can't click on ListView on item click. How to solved this issue?
My XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/exam_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/qeuestion_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="qwq"
android:textColor="#color/Black"
android:textSize="18sp" />
<ImageView
android:id="#+id/img_select_question"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:src="#drawable/select_question" />
<Spinner
android:id="#+id/marks_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_toLeftOf="#+id/img_select_question"
android:focusable="false"
android:focusableInTouchMode="false"
android:padding="10dp"
android:prompt="#string/tv_chapter" />
</RelativeLayout>
spinner in set value as a custom class.
so i don't no it better or not.
also same help to solved click on list view issue.
My adapter Java code:
public class Question_list_adapter extends BaseAdapter{
private LayoutInflater l_Inflater;
Activity obj_a;
List<Questions_Details> question_list;
private static class ViewHolder {
public DataHolder data;
public TextView qeuestion_name;
public ImageView img_select_question;
public Spinner marks_spinner;
}
public Question_list_adapter(Activity activity,List<Questions_Details> list_question_Details) {
this.obj_a=activity;
this.question_list =list_question_Details;
l_Inflater = LayoutInflater.from(activity);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return question_list.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return question_list.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View vi = convertView;
if(vi==null)
vi = l_Inflater.inflate(R.layout.question_list_row, null);
holder = new ViewHolder();
holder.qeuestion_name = (TextView)vi.findViewById(R.id.qeuestion_name);
holder.img_select_question=(ImageView)vi.findViewById(R.id.img_select_question);
holder.qeuestion_name.setText(question_list.get(position).question);
holder.marks_spinner= (Spinner)vi.findViewById(R.id.marks_spinner);
holder.data = new DataHolder(obj_a);
holder.marks_spinner.setAdapter(holder.data.getAdapter());
if(Global_Application.selected_question_list.contains(question_list.get(position).question_Id))
holder.img_select_question.setVisibility(View.VISIBLE);
else
holder.img_select_question.setVisibility(View.INVISIBLE);
return vi;
}
}
spinner in set custom class
public class DataHolder {
private int selected;
private ArrayAdapter<CharSequence> adapter;
public DataHolder(Context parent) {
adapter = ArrayAdapter.createFromResource(parent, R.array.marks, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
public ArrayAdapter<CharSequence> getAdapter() {
return adapter;
}
public String getText() {
return (String) adapter.getItem(selected);
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
}
Try to replace your adapter code with this
public class Question_list_adapter extends BaseAdapter {
private LayoutInflater l_Inflater;
Activity activity;
List<Questions_Details> question_list;
private class ViewHolder {
public TextView qeuestion_name;
public ImageView img_select_question;
public Spinner marks_spinner;
}
public Question_list_adapter(Activity activity,List<Questions_Details> list_question_Details) {
this.activity=activity;
this.question_list =list_question_Details;
}
#Override
public int getCount() {
return question_list.size();
}
#Override
public Object getItem(int position) {
return question_list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(activity).inflate(R.layout.question_list_row, null);
holder.qeuestion_name = (TextView)convertView.findViewById(R.id.qeuestion_name);
holder.img_select_question=(ImageView)convertView.findViewById(R.id.img_select_question);
holder.marks_spinner= (Spinner)convertView.findViewById(R.id.marks_spinner);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.qeuestion_name.setText(question_list.get(position).question);
holder.marks_spinner.setAdapter(new DataHolder(activity));
if(Global_Application.selected_question_list.contains(question_list.get(position).question_Id))
holder.img_select_question.setVisibility(View.VISIBLE);
else
holder.img_select_question.setVisibility(View.INVISIBLE);
return convertView;
}
}

Adding button as lest element in gridview

I know how to make every single cell in a gridview into a button, but that's not what I'm after.
I have a gridview that's filled with this adapter.
public class TaskAdapter extends BaseAdapter{
private Context mContext;
public TaskAdapter(Context c)
{
mContext = c;
}
#Override
public int getCount() {
return nThumbsIds.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View sView = convertView;
if(convertView == null){
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
sView = inflater.inflate(R.layout.task_square, parent, false);
TextView tView = (TextView) sView.findViewById(R.id.textView);
tView.setText(nThumbsIds.get(position));
}
return sView;
}
}
And nThumbsIds is filled with Strings whose contents will be different at almost every build.
I want to add a button as the last element of the grid, always after all the Strings. Is there a way?
Try like this:
Create Adapter
public class TaskAdapter extends BaseAdapter
{
private Context mContext;
public TaskAdapter(Context c)
{
mContext = c;
}
#Override
public int getCount()
{
return nThumbsIds.size();
}
#Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return nThumbsIds.get(position);
}
#Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
TextView tView =null;
Button tBtn =null;
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.task_square, parent, false);
tView = (TextView) convertView.findViewById(R.id.grid_item_txv);
tBtn = (Button) convertView.findViewById(R.id.grid_item_btn);
if(position < nThumbsIds.size()-1)
{
tView.setText(nThumbsIds.get(position));
tView.setVisibility(View.VISIBLE);
tBtn.setVisibility(View.GONE);
}
else
{
tBtn.setText(nThumbsIds.get(position));
tView.setVisibility(View.GONE);
tBtn.setVisibility(View.VISIBLE);
}
return convertView;
}
Your Grid Item layout xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/grid_item_txv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:layout_centerInParent="true"
android:gravity="center"
android:text="Smaple"/>
<Button
android:id="#+id/grid_item_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:gravity="center"
android:layout_centerInParent="true"
android:text="sample"
android:visibility="gone" />
</RelativeLayout>
GridView in your activity's layout :
<GridView
android:id="#+id/test_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="100dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
</GridView>

Expandable List View, Vertical Child

I made an expandable list like the one in this link: http://www.techienjoy.com/android-expandable-list-dynamically-created-example.php, but I want to make the child to look in vertical not horizontal.
I only have 1 xml with:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ExpandableListView android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:groupIndicator="#null"/>
Everything else is in the class. How can I set them vertical?
My java code is the same one in the link.
Please have a look at this Example. Hope this will help you.
public class ExpandListViewActivity extends ExpandableListActivity
{
/**
* strings for group elements
*/
static final String arrGroupelements[] =
{
"India",
"Australia",
"England",
"South Africa"
};
/**
* strings for child elements
*/
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"
}
};
static final int arrChildelementsim[][] =
{
{
R.drawable.ic_launcher,
R.drawable.aqua,
R.drawable.pin_red,
R.drawable.aqua,
},
{
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
},
{
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
},
{
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
}
};
DisplayMetrics metrics;
int width;
ExpandableListView expList;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
expList = getExpandableListView();
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
//this code for adjusting the group indicator into right side of the view
expList.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));
expList.setAdapter(new ExpAdapter(this));
expList.setOnGroupExpandListener(new OnGroupExpandListener()
{
#Override
public void onGroupExpand(int groupPosition)
{
Log.e("onGroupExpand", "OK");
}
});
expList.setOnGroupCollapseListener(new OnGroupCollapseListener()
{
#Override
public void onGroupCollapse(int groupPosition)
{
Log.e("onGroupCollapse", "OK");
}
});
expList.setOnChildClickListener(new OnChildClickListener()
{
#Override
public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2,
int arg3, long arg4) {
Log.e("OnChildClickListener", "OK");
// TODO Auto-generated method stub
return false;
}
});
}
public int GetDipsFromPixel(float pixels)
{
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
public class ExpAdapter extends BaseExpandableListAdapter {
private Context myContext;
public ExpAdapter(Context context) {
myContext = context;
}
#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) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_row, null);
}
TextView tvPlayerName = (TextView) convertView.findViewById(R.id.tvPlayerName);
ImageView tvPlayerimage = (ImageView) convertView.findViewById(R.id.tvPlayerImage);
tvPlayerName.setText(arrChildelements[groupPosition][childPosition]);
tvPlayerimage.setImageResource(arrChildelementsim[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) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.group_row, null);
}
TextView tvGroupName = (TextView) convertView.findViewById(R.id.tvGroupName);
tvGroupName.setText(arrGroupelements[groupPosition]);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
Child_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:id="#+id/tvPlayerImage"
android:layout_width="wrap_content"
android:layout_height="30dip"
android:gravity="center_vertical"
android:layout_marginLeft="50dip"
>
</ImageView>
<TextView
android:id="#+id/tvPlayerName"
android:layout_width="wrap_content"
android:layout_height="30dip"
android:gravity="center_vertical"
android:layout_marginLeft="10dip"
android:textSize="14sp" >
</TextView>
</LinearLayout>
group_row.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" >
<TextView
android:id="#+id/tvGroupName"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:gravity="center_vertical"
android:paddingLeft="30dip"
android:textSize="16sp"
android:textStyle="bold"
>
</TextView>
</LinearLayout>
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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ExpandableListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:groupIndicator="#drawable/group_indicator" >
<TextView
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="hello" >
</TextView>
</ExpandableListView>
</LinearLayout>

Categories