Android displaying list using expandableListView
Asked Answered
A

2

7

I have followed this example tutorial and developed a sample application http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

enter image description here

If i click on Core i want to load one more list in the same Screen under Core,How Could i do this?using my below code i am able to load the child list in another Screen,help?

My Code for ChildClickListener goes here:

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            // TODO Auto-generated method stub

            Log.d("onChildClick", "onChildClick");

            String position = (String) parentItems.get(groupPosition);
            Log.d("position", position);

            String child = listDataChild.get(position).get(childPosition);

            Log.d("child", child);

            if (child.equalsIgnoreCase("Core")) {

                ArrayList<String> parentItems = new ArrayList<String>();

                HashMap<String, List<String>> listDataChild = new HashMap<String, List<String>>();
                ArrayList<String> childItems = new ArrayList<String>();
                childItems.add("corejava");
                childItems.add("corejava");
                childItems.add("corejava");
                childItems.add("corejava");

                parentItems.add(position);
                listDataChild.put(parentItems.get(0), childItems);
                expandableList = (ExpandableListView) findViewById(R.id.lvExp);

                CoreAdapter adapter = new CoreAdapter(parentItems,
                        listDataChild);
                CustExpListview SecondLevelexplv = new CustExpListview(
                        MainActivity.this);
                adapter.setInflater(
                        (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
                        MainActivity.this);

                SecondLevelexplv.setAdapter(adapter);
                SecondLevelexplv.setGroupIndicator(null);

                expandableList.setAdapter(adapter);

            }
Appertain answered 5/3, 2014 at 6:42 Comment(0)
N
2

i write one treeView adapter for you, you can use this and put many level as you want, for that you need just copy following code:

MainActivity:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // add data for test on one list<AccountHierarchical>


        AccountHierarchical obj = new AccountHierarchical();
        obj.setLevelId(1);
        obj.setTitle("level 1");


        AccountHierarchical obj2 = new AccountHierarchical();
        obj2.setLevelId(1);
        obj2.setTitle("level 2");


        AccountHierarchical obj3 = new AccountHierarchical();
        obj3.setLevelId(1);
        obj3.setTitle("level 3");


        List<AccountHierarchical> emptyList = new ArrayList<AccountHierarchical>();
        obj3.setList(emptyList);

        List<AccountHierarchical> list2 = new ArrayList<AccountHierarchical>();
        list2.add(obj3);list2.add(obj3);list2.add(obj3);list2.add(obj3);list2.add(obj3);
        obj2.setList(list2);

        List<AccountHierarchical> list = new ArrayList<AccountHierarchical>();
        list.add(obj2);list.add(obj2);list.add(obj2);list.add(obj2);list.add(obj2);
        obj.setList(list);


        List<AccountHierarchical> result = new ArrayList<AccountHierarchical>();
        result.add(obj);result.add(obj);result.add(obj);result.add(obj);



        // create Adapter
        TreeView adapter = new TreeView(result, this,
                getLayoutInflater() , false, 0);

        ExpandableListView expandList = (ExpandableListView)findViewById(R.id.expandableList_tree);
        expandList.setGroupIndicator(null);
        expandList.setAdapter(adapter);
    }

CustExpListview:

public class CustExpListview extends ExpandableListView {

    int intGroupPosition, intChildPosition, intGroupid;

    public CustExpListview(Context context) {
           super(context);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//           widthMeasureSpec = MeasureSpec.makeMeasureSpec(400,
//                        MeasureSpec.AT_MOST);
           heightMeasureSpec = MeasureSpec.makeMeasureSpec(600,
                        MeasureSpec.AT_MOST);
           super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


}

AccountHierarchical:

public class AccountHierarchical {

    List<AccountHierarchical> list;
    private String Title;
    private int Id;


    public int getId() {
        return Id;
    }

    public void setLevelId(int Id) {
        this.Id = Id;
    }

    public String getTitle() {
        return Title;
    }

    public void setTitle(String title) {
        Title = title;
    }

    public List<AccountHierarchical> getList() {
        return list;
    }

    public void setList(List<AccountHierarchical> list) {
        this.list = list;
    }

}

TreeView:

public class TreeView extends BaseExpandableListAdapter implements
        OnClickListener {

    List<AccountHierarchical> list;
    LayoutInflater inflatter;
    static Context context;
    boolean checkGroup;
    int position;

    static ProgressDialog ProgressDialog;

    public TreeView( List<AccountHierarchical> list, Context context,
            LayoutInflater inflatter, boolean checkGroup, int position
         ) {
        this.list = list;
        TreeView.context = context;
        this.inflatter = inflatter;
        this.checkGroup = checkGroup; // this is true when you call from inner.
        this.position = position;



    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {

        return list.get(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {

        return list.get(groupPosition).getList().get(childPosition).getId();
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        if (checkGroup)
            groupPosition = position;




        List<AccountHierarchical> childtemp = list.get(groupPosition)
                .getList();

        // call this adapter again for creating another level
        if (childtemp.get(childPosition).getList().size() > 0) {
            CustExpListview SecondLevelexplv = new CustExpListview(context);
            TreeView adapter  = null;

            adapter = new TreeView(childtemp, context,
                        inflatter, true, childPosition);
            SecondLevelexplv.setGroupIndicator(null);
            SecondLevelexplv.setAdapter(adapter);
            return SecondLevelexplv;
        }
        // call one layout, this is last child
        else {

            convertView = inflatter.inflate(R.layout.grouprow, null);
            TextView tv = (TextView) convertView.findViewById(R.id.grouprow);



            tv.setText(list.get(groupPosition).getList().get(childPosition)
                    .getTitle());
            tv.setPadding(0, 0, 20, 0);
            convertView.setTag(list.get(groupPosition).getList()
                    .get(childPosition) );
            convertView.setId(position);
            convertView.setOnClickListener(this);
            return convertView;
        }

    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public int getGroupCount() {

        if (checkGroup)
            return 1;

        return list.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return list.get(groupPosition).getId();
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {

        if (convertView == null)
            convertView = inflatter.inflate(R.layout.grouprow, null);

        TextView tv = (TextView) convertView.findViewById(R.id.grouprow);

        if (checkGroup)
            tv.setText(list.get(position).getTitle());
        else
            tv.setText(list.get(groupPosition).getTitle());

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {

        return false;
    }

    @Override
    public void onClick(View v) {
        // your onClick method
    }

}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     >

    <ExpandableListView
        android:id="@+id/expandableList_tree"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

    </ExpandableListView>

</RelativeLayout>

grouprow.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/grouprow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp" 
        android:text=""/>



</RelativeLayout>

snap:

enter image description here

Nephrotomy answered 5/3, 2014 at 7:37 Comment(1)
Thanks, Your code help a lot, in perusing a difficult task.Maidservant
O
1

You have to create a layout in the xml file (from where your core textview comes from) and display an image view below onClick of that imageView you will just have to make your layout Gone and Visible I have just given you direction rest relies on your skill :)

    <RelativeLayout
        android:id="@+id/option"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:gravity="center"
        android:orientation="horizontal" >

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/linear"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/navigationIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/nn" />


        <ImageView
            android:id="@+id/callOption"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/phonee" />

        <ImageView
            android:id="@+id/imageViewfeedback"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/editer" />
    </RelativeLayout>

In Jave File

linear.setVisibility(View.GONE);
        option.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    if(count==0)
                    {
                        linear.setVisibility(View.VISIBLE);

                        count=1;
                    }
                    else
                    {


                        linear.setVisibility(View.GONE);
                        count=0;
                    }

            }
        });
Oldest answered 5/3, 2014 at 6:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.