JNR_Prog123 JNR_Prog123 - 2 months ago 53
Java Question

Getting sqlite data into ExpandableListView

I have an

ExpandableListView
created and working with static data, my problem is I am not sure how to get data from my
sqlite database
into the
ExpandableListView
. I have tried multiple websites and posts and have gotten very little.

If someone could please take a look at my code below and suggest a working solution.

Thanks

package com.example.pooveshin.vennsroadaccident2;

import android.app.Activity;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

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);

// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {

@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});

// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});

// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();

}
});

// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {

@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}

/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();

// Adding child data
listDataHeader.add("Accident Number : 1");
listDataHeader.add("Accident Number : 2");
listDataHeader.add("Accident Number : 3");

// Adding child data
List<String> AccYV = new ArrayList<String>();
AccYV.add("Accident Number : ");
AccYV.add("Registration Number : ");
AccYV.add("Make & Model");
AccYV.add("Address of Owner");
AccYV.add("Name of Driver");
AccYV.add("Address of Driver");
AccYV.add("Tel no.Driver");

List<String> AccOV = new ArrayList<String>();
AccOV.add("Name of Driver");
AccOV.add("Identity Number");
AccOV.add("Residential Address");
AccOV.add("Tel no.Work");
AccOV.add("Make & Model");
AccOV.add("Licence Number");

List<String> AccCD = new ArrayList<String>();
AccCD.add("Date");
AccCD.add("Time");
AccCD.add("Place");
AccCD.add("Weather");
AccCD.add("Road Surface");

listDataChild.put(listDataHeader.get(0), AccYV); // Header, Child data
listDataChild.put(listDataHeader.get(1), AccOV);
listDataChild.put(listDataHeader.get(2), AccCD);
}
}


In DBHelper.java

public Cursor getYVAllData()
{
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
Cursor res = sqLiteDatabase.rawQuery("SELECT * FROM " + TABLE_NAME,null);
return res;
}

Answer

you shoud make an adapter for expandablelistview like this

 public class ExpandListAdapter extends BaseExpandableListAdapter {

        private Context context;
        private ArrayList<ExpandListGroup> groups;
        public ExpandListAdapter(Context context, ArrayList<ExpandListGroup> groups) {
            this.context = context;
            this.groups = groups;
        }

        public void addItem(ExpandListChild item, ExpandListGroup group) {
            if (!groups.contains(group)) {
                groups.add(group);
            }
            int index = groups.indexOf(group);
            ArrayList<ExpandListChild> ch = groups.get(index).getItems();
            ch.add(item);
            groups.get(index).setItems(ch);
        }
        public Object getChild(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            ArrayList<ExpandListChild> chList = groups.get(groupPosition).getItems();
            return chList.get(childPosition);
        }

        public long getChildId(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return childPosition;
        }

        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view,
                                 ViewGroup parent) {
            ExpandListChild child = (ExpandListChild) getChild(groupPosition, childPosition);
            if (view == null) {
                LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
                view = infalInflater.inflate(R.layout.expandlist_child_item, null);
            }
            TextView tv = (TextView) view.findViewById(R.id.tvChild);
            tv.setText(child.getName().toString());
            tv.setTag(child.getTag());
            // TODO Auto-generated method stub
            return view;
        }

        public int getChildrenCount(int groupPosition) {
            // TODO Auto-generated method stub
            ArrayList<ExpandListChild> chList = groups.get(groupPosition).getItems();

            return chList.size();

        }

        public Object getGroup(int groupPosition) {
            // TODO Auto-generated method stub
            return groups.get(groupPosition);
        }

        public int getGroupCount() {
            // TODO Auto-generated method stub
            return groups.size();
        }

        public long getGroupId(int groupPosition) {
            // TODO Auto-generated method stub
            return groupPosition;
        }

        public View getGroupView(int groupPosition, boolean isLastChild, View view,
                                 ViewGroup parent) {
            ExpandListGroup group = (ExpandListGroup) getGroup(groupPosition);
            if (view == null) {
                LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
                view = inf.inflate(R.layout.expandlist_group_item, null);
            }

            TextView tv = (TextView) view.findViewById(R.id.tvGroup);
            tv.setText(group.getName());
            // TODO Auto-generated method stub
            return view;
        }

        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return true;
        }

        public boolean isChildSelectable(int arg0, int arg1) {
            // TODO Auto-generated method stub
            return true;
        }

    }

then you should make child and group classes for list like this

child:

 public class ExpandListChild {
        private String Name;
        private String Tag;
        public String getName() {
            return Name;
        }
        public void setName(String Name) {
            this.Name = Name;
        }
        public String getTag() {
            return Tag;
        }
        public void setTag(String Tag) {
            this.Tag = Tag;
        }
    }

group:

public class ExpandListGroup {
    private String Name;
    private ArrayList<ExpandListChild> Items;

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        this.Name = name;
    }

    public ArrayList<ExpandListChild> getItems() {
        return Items;
    }

    public void setItems(ArrayList<ExpandListChild> Items) {
        this.Items = Items;
    }

}

and this is usage in your MainActivity :

     ExpandList = (ExpandableListView) findViewById(R.id.expandableListView);
                        ExpListItems = SetStandardGroups(da.queryName());
                        ExpAdapter = new ExpandListAdapter(MainActivity.this, ExpListItems);

and this is my method for fiil that list:





      public ArrayList<ExpandListGroup> SetStandardGroups(Cursor crsr) {
                ArrayList<ExpandListGroup> list = new ArrayList<ExpandListGroup>();
                ArrayList<ExpandListChild> list2;
                Cursor c = crsr;
                if (c.moveToFirst()) {
                    do {
                        String English = c.getString(c.getColumnIndex("English"));
                        String Farsi = c.getString(c.getColumnIndex("Farsi"));
                        ExpandListGroup gru1 = new ExpandListGroup();
                        gru1.setName(English);
                        ExpandListChild ch1_1 = new ExpandListChild();
                        ch1_1.setName(Farsi);
                        ch1_1.setTag(null);
                        list2 = new ArrayList<ExpandListChild>();
                        list2.add(ch1_1);
                        gru1.setItems(list2);
                        list.add(gru1);
                    } while (c.moveToNext());
                }
                c.close();

                return list;
            }
Comments