• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Populate ExpandableListview from Sqlite Database:Android

Pooveshin

Newbie
I know this is a duplicate question however I have tried multiple ways and cannot seem to get it working. I have created an expandable list view and need a way to populate it with data from a Sqlite database, each record needs to be in their expanding view. At present I have put static data to show the Lists expand and contract, I have attached my code can anyone help me?

Expandable List Adapter.java
public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;

public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}

@override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}

@override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

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

final String childText = (String) getChild(groupPosition, childPosition);

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}

TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);

txtListChild.setText(childText);
return convertView;
}

@override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}

@override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}

@override
public int getGroupCount() {
return this._listDataHeader.size();
}

@override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}

TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);

return convertView;
}

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

@override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}


Main Activity.java

public class MainActivity extends Activity {

DBHelper DB;
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> top250 = new ArrayList<String>();
top250.add("Registration No");
top250.add("Name of Owner");
top250.add("Make & Model");
top250.add("Address of Owner");
top250.add("Name of Driver");
top250.add("Address of Driver");
top250.add("Tel no.Driver");

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

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

listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), comingSoon);
}
}

list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />

</LinearLayout>

List_group.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/lblListHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="20dp"
android:textColor="#57585a" />

</LinearLayout>

DBHelper.java

public DBHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public static final String CREATE_TABLE = "CREATE TABLE "+ TABLE_NAME + "(Accident_Number INTEGER PRIMARY KEY AUTOINCREMENT, Registration_No TEXT, make_Model TEXT, Name_of_Owner TEXT, " +
"Address_of_Owner TEXT, Name_of_Driver TEXT, Address_of_Driver TEXT, Tel_of_Owner TEXT, Tel_of_Driver TEXT, vehicle_Insured TEXT);";

public boolean SaveVehicle(String registration_no, String make_Model, String Name_of_Owner, String Address_of_Owner, String Name_of_Driver, String Address_of_Driver, String Tel_of_Owner, String Tel_of_Driver, String vehicle_Insured)
{
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Reg_Number,registration_no);
contentValues.put(Make_Model,make_Model);
contentValues.put(Name_Owner,Name_of_Owner);
contentValues.put(Add_Owner,Address_of_Owner);
contentValues.put(Name_Driver,Name_of_Driver);
contentValues.put(Add_Driver,Address_of_Driver);
contentValues.put(Tel_Owner,Tel_of_Owner);
contentValues.put(Tel_Driver,Tel_of_Driver);
contentValues.put(Vehicle_Insured,vehicle_Insured);
long result = sqLiteDatabase.insert(TABLE_NAME,null, contentValues);
if (result == -1 )
return true;
else
return false;
}

public Cursor getYVAllData()
{
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
Cursor res = sqLiteDatabase.rawQuery("SELECT * FROM " + TABLE_NAME,null);
return res;
}
 
You have a data access method getYVAllData() in the DBHelper class to retrieve all your data from the database. Is there any problem calling that to populate your Listview?
 
You have a data access method getYVAllData() in the DBHelper class to retrieve all your data from the database. Is there any problem calling that to populate your Listview?

Hi
I'm not entirely to sure on how to go about doing it.. I'm relatively new to android.. if you could show or help in any way that would be much appreciated.
 
call DB.getYVAllData() to return the database cursor containing the search results.
Then iterate over the cursor, extracting all the required fields.

If you don't understand the above, then you need to look at an Android database tutorial. It's too much to go through in a forum thread.
Have a look at this

http://www.tutorialspoint.com/android/android_sqlite_database.htm
 
Last edited by a moderator:
Back
Top Bottom