Android - start called in state 1 - android

I'm facing trouble with my application, what i want to do is when i click the child of exp list it will play song.
it's clear when start play first song but after stop and I play it again an error display in my logcat.
I don't know how to solve this problem.
please help...
Here's my code:
public class BacktrackActivity extends Activity {
MediaPlayer mp;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
Context context;
public ImageButton stop;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.backtrack);
// final int[] blues = {R.raw.satch_boogie, R.raw.satch_boogie};
mp = MediaPlayer.create(this, R.raw.satch_boogie);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.genre_list);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this,listDataHeader,listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
stop = (ImageButton) findViewById(R.id.imageButton1);
stop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
mp.stop();
mp.reset();
}
});
expListView.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition,
long id) {
// TODO Auto-generated method stub
return false;
}
});
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(), "Choose your " +
listDataHeader.get(groupPosition) + " Backtrack",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
mp.start();
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Blues");
listDataHeader.add("Jazz");
listDataHeader.add("Rock");
listDataHeader.add("RnB");
// Adding child data
List<String> Blues = new ArrayList<String>();
Blues.add("The Shawshank Redemption");
Blues.add("The Godfather");
Blues.add("The Godfather: Part II");
Blues.add("Pulp Fiction");
Blues.add("The Good, the Bad and the Ugly");
Blues.add("The Dark Knight");
Blues.add("12 Angry Men");
List<String> Jazz = new ArrayList<String>();
Jazz.add("The Conjuring");
Jazz.add("Despicable Me 2");
Jazz.add("Turbo");
Jazz.add("Grown Ups 2");
Jazz.add("Red 2");
Jazz.add("The Wolverine");
List<String> Rock = new ArrayList<String>();
Rock.add("2 Guns");
Rock.add("The Smurfs 2");
Rock.add("The Spectacular Now");
Rock.add("The Canyons");
Rock.add("Europa Report");
List<String> RnB = new ArrayList<String>();
RnB.add("2 Guns");
RnB.add("The Smurfs 2");
RnB.add("The Spectacular Now");
RnB.add("The Canyons");
RnB.add("Europa Report");
listDataChild.put(listDataHeader.get(0), Blues); // Header, Child data
listDataChild.put(listDataHeader.get(1), Jazz);
listDataChild.put(listDataHeader.get(2), Rock);
listDataChild.put(listDataHeader.get(3), RnB);
}
}
here's my logcat error
12-04 08:52:54.317: W/Trace(1413): Unexpected value from nativeGetEnabledTags: 0
12-04 08:52:54.336: E/MediaPlayer(1413): start called in state 1
12-04 08:52:54.336: E/MediaPlayer(1413): error (-38, 0)
12-04 08:52:54.356: W/Trace(1413): Unexpected value from nativeGetEnabledTags: 0
12-04 08:52:54.356: W/Trace(1413): Unexpected value from nativeGetEnabledTags: 0
12-04 08:52:54.416: W/Trace(1413): Unexpected value from nativeGetEnabledTags: 0
12-04 08:52:54.416: W/Trace(1413): Unexpected value from nativeGetEnabledTags: 0
12-04 08:52:54.466: E/MediaPlayer(1413): Error (-38,0)

Usually this kind of error happens when you try to call start() before prepare() is finished.
Before calling mp.start(); you should make sure that you have already set the setDataSource() and prepare() is completed.
In your code by clicking stop, you have reset the media.So before calling mp.start() again you should at least setDataSource() again.
IMHO, it worked the first time because you have set the data source ( MediaPlayer.create(this, R.raw.satch_boogie); )inside the onCreate() method and there is enough time for it to prepared by the time you click the listView child.
Try setDataSource (MediaPlayer.create(this, R.raw.satch_boogie);) anytime you reset it.

Related

FindViewById giving Unreachable statement error in Fragment activity [duplicate]

This question already has answers here:
I get the error "Unreachable statement" return in android
(3 answers)
Closed 5 years ago.
I'm pretty new to Fragment based activities. I'm working on a collapsible listview inside the CollapsingToolbarLayout.
Here, I'm facing Unreachable statement error in java file. Not able to get rid of it.
My code -
public class MenuActivity extends android.support.v4.app.Fragment {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_booktable, container, false);
// get the listview
expListView = (ExpandableListView) container.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(getActivity().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(getActivity().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(
getActivity().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("South Indian");
listDataHeader.add("Quick Bites");
listDataHeader.add("Soups");
// Adding child data
List<String> SouthIndian = new ArrayList<String>();
SouthIndian.add("Kara Bath");
SouthIndian.add("Chow Chow Bath");
SouthIndian.add("2 Idli 1 Vada");
SouthIndian.add("Rava Idli");
SouthIndian.add("Curd Vada");
SouthIndian.add("Masala Dosa");
List<String> QuickBites = new ArrayList<String>();
QuickBites.add("Veg Sandwich");
QuickBites.add("Veg Toast Sandwich");
QuickBites.add("Bread Butter Jam");
QuickBites.add("Bread Jam");
QuickBites.add("Pakoda");
QuickBites.add("Masala Puri");
List<String> Soups = new ArrayList<String>();
Soups.add("Tomato Soup");
Soups.add("Sweet corn Soup");
Soups.add("Veg Soup");
Soups.add("Veg Schezwan Soup");
Soups.add("Veg Noodles Soup");
listDataChild.put(listDataHeader.get(0), SouthIndian); // Header, Child data
listDataChild.put(listDataHeader.get(1), QuickBites);
listDataChild.put(listDataHeader.get(2), Soups);
}
}
How do I fix the error for the line expListView = (ExpandableListView) container.findViewById(R.id.lvExp); and make this code run?
move return inflater.inflate(R.layout.activity_booktable, container, false) to the end of method onCreateView()
Your first line in onCreateView already returns the View so the rest of the code is not executed:
return inflater.inflate(R.layout.activity_booktable, container, false);
You have to replace it with
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.activity_booktable, container, false);
...
return myView;
}
and then better do the findViewById on this view, e.g.
expListView = (ExpandableListView) myView.findViewById(R.id.lvExp);

How to remove item from ArrayList<HashMap<String, String>> one by one in android

I am new to Android. I am Using "com.devsmart.android.ui.HorizontalListView" to show my items(playing cards). In the List I am getting 8 cards in the first attempt. What I want if I again call the method the there must be 7 cards so on means 1 cards must be remove from the list at each time till the list gets empty. But I am not able not do this removing of item from the list. I am posting my code here. Help will be appreciated.
MainActivity.java
ArrayList<HashMap<String, String>> aList9;
Button btn_aditional_card;
HorizontalListView list1;
int[] cards3 = new int[]{
R.drawable.card1,
R.drawable.card1,
R.drawable.card1,
R.drawable.card1,
R.drawable.card1,
R.drawable.card1,
R.drawable.card1,
R.drawable.card1,
R.drawable.card1,
R.drawable.card1,
R.drawable.card1,
R.drawable.card1,
R.drawable.card1,
R.drawable.card1
};
public class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_aditional_card = (Button) findViewById(R.id.btn_aditional_card);
list1 = (HorizontalListView) findViewById(R.id.listview1);
btn_aditional_card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
add8();
}
});
}
public void add8() {
final android.view.animation.Animation animScale = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
list1.startAnimation(animScale);
aList9 = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 8; i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("card", Integer.toString(cards3[i]));
aList9.add(hm);
}
String[] from = {"card"};
int[] to = {R.id.ImageView};
final SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList9, R.layout.activity_animation__adapter, from, to);
list1.setAdapter(adapter);
adapter.notifyDataSetChanged();
list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
aList9.remove(aList9.size() - 1);
adapter.notifyDataSetChanged();
}
});
}
Here removing of item from the list must be done on list1.setOnItemClickListener but I don't have any idea how.
In your list1.setOnItemClickListener, do this:
if (aList9.size() > 0) {
aList9.remove(aList9.size() - 1);
adapter.notifyDataSetChanged();
}
When removing item from data list, you have to notify your adapter to update view.
You can remove the last item from the list like;
if (aList9.size() > 0) {
aList9.remove(aList9.size() - 1);
}
Another solution,
Take Length of your integer array and traverse the loop inside your add8 method according to this length and at the end of the method decrement the length by one, like;
int length = cards3.length;
Inside your add8 method;
for (int i = 0; i < length; i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("card", Integer.toString(cards3[i]));
aList9.add(hm);
}
After for loop decrement the length like;
length--;
Hope you'll get the solution.
In your main activity the code should look like.I am using arraylist instead you can use hashmap.This code is only to show you way what you need to do. follow this code in your project you will get solution this is tested in my site and working fine you need to change somewhere according to your code.In button click i am only removing last item from arraylist and notifying that dataset changed.hope it will help you.
ArrayList<String> arrlist = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrlist.add("B");
arrlist.add("C");
arrlist.add("D");
arrlist.add("E");
arrlist.add("F");
arrlist.add("G");
ListView lv = (ListView) findViewById(R.id.listview);
Button btnShowList = (Button) findViewById(R.id.btnShowList);
final ArrayAdapter adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, arrlist);
//Listview adapter
lv.setAdapter(adapter);
btnShowList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (arrlist.size() > 0) {
arrlist.remove(arrlist.size() - 1);
adapter.notifyDataSetChanged();
}
}
});
}
Read the comments
class MainActivity extends AppCompatActivity {
//make your Adapter global
private SimpleAdapter adapter;
ArrayList<HashMap<String, String>> aList9;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add8();
btn_aditional_card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// on button click you can add a view to the listview if you want
// if you don't want this just comment out the following line
// i still don't understand what you want to do with this button
alist9.add(...);
// if you add stuff in the array list then notify the adapter
// it will update the view
adapter.notifyDataSetChanged();
}
});
}
public void add8() {
// initialize your ArrayList and listview
//populate them
for(int i = 0; i < 8; i++) {
// do your stuff.
// populate the list
}
// initialize your adapter as before
adapter = new SimpleAdapter(....);
listview.setadapter(adapter);
adapter.notifyDataSetChanged();
list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// delete the items like this
// check if size is greater than 0 and then remove
aList9.remove(aList9.size() - 1);
adapter.notifyDataSetChanged();
}
});
}
}

How to implement onchildclick listener?

So I am having an expandable listview. What I want is to make clickable each children. If I press the first one I want to open the class1, if I press the second one I want to open the class2, if I press the third one I want to open the class3 and so on... I am new in programming so please explain me like you would do it for a dummy.
I am getting this error message
The constructor Intent(new ExpandableListView.OnChildClickListener(){}, Class<Men>) is undefined
Source Code
package info.androidhive.expandablelistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
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;
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();
switch( childPosition )
{
case 0: Intent Men = new Intent(this, Men.class);
startActivity(Men);
break;
case 1: Intent newActivity2 = new Intent(this, youtube.class);
startActivity(newActivity2);
break;
case 2: Intent newActivity1 = new Intent(this, olympiakos.class);
startActivity(newActivity1);
break;
case 3: Intent newActivity3 = new Intent(this, karaiskaki.class);
startActivity(newActivity3);
break;
case 4: Intent newActivity4= new Intent(this, reservetickets.class);
startActivity(newActivity4);
break;
}
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Top 250");
listDataHeader.add("Now Showing");
listDataHeader.add("Coming Soon..");
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank Redemption");
top250.add("The Godfather");
top250.add("The Godfather: Part II");
top250.add("Pulp Fiction");
top250.add("The Good, the Bad and the Ugly");
top250.add("The Dark Knight");
top250.add("12 Angry Men");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("The Conjuring");
nowShowing.add("Despicable Me 2");
nowShowing.add("Turbo");
nowShowing.add("Grown Ups 2");
nowShowing.add("Red 2");
nowShowing.add("The Wolverine");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
comingSoon.add("The Smurfs 2");
comingSoon.add("The Spectacular Now");
comingSoon.add("The Canyons");
comingSoon.add("Europa Report");
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), comingSoon);
}
You dont have any Men class in your application. Thats why its showing error. See for the correct class name. Again for your convenience use a different intent name
case 0: Intent goactivity = new Intent(youractivity.this, men.class);
startActivity(goactivity);
//as your coding looks looks like may be your class is **men** not **Men**
Again if it is Men check if that extends Activity or not
It is basically because when you refer to this within a OnChildClickListener, it refers to the OnChildClickListener, not the Activity. Try using MainActivity.this instead. Or you can use view.getContext(). Both will give you the proper context.

expandlelistview child click

I have an expandle listview witha few children. If i click a child i want it to open it own activity. Right now i got it to work to go to 1 activity only. So when for example when i click on Mambo beach i want it to open an activity with the information of Mambo beach. And when i click on Avis car Rental it should open that acitivity
below my example:
public class TodoFragment extends Fragment {
public TodoFragment(){}
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_todo,container, false);
}
#Override
public void onStart() {
super.onStart();
// get the listview
expListView = (ExpandableListView) getView().findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(getActivity(), 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_LONG).show();
return false;
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
//You have to create next activity say NextActivity.java
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding group data
listDataHeader.add("Culture");
listDataHeader.add("Beaches");
listDataHeader.add("Car Rental");
listDataHeader.add("Dinner");
// Adding child data
List<String> culture= new ArrayList<String>();
culture.add("Grotten van Hato");
culture.add("Ostrich Farm");
culture.add("Shete Boka national Park");
culture.add("Landhuis Knip");
culture.add("Christoffelpark");
culture.add("Navy Museum");
culture.add("Post Museum");
List<String> beaches = new ArrayList<String>();
beaches.add("Mambo Beach");
beaches.add("Knip");
beaches.add("Playa Kalki");
beaches.add("Westpunt");
beaches.add("Boca Santa Cruz");
beaches.add("Cas Abao");
beaches.add("Playa PortoMari");
beaches.add("Kontiki Beach");
beaches.add("Jan Thiel Beach");
List<String> car = new ArrayList<String>();
car.add("budget rental");
car.add("Avis Rental");
car.add("Alamo Car Rental");
car.add("Noordstar Rental");
car.add("Europa Rental");
List<String> dinner = new ArrayList<String>();
dinner.add("Truk di Pan");
dinner.add("Burger King");
dinner.add("Punda Food");
// Header, Child data
listDataChild.put(listDataHeader.get(0), culture);
listDataChild.put(listDataHeader.get(1), beaches);
listDataChild.put(listDataHeader.get(2), car);
listDataChild.put(listDataHeader.get(3), dinner);
}
}
You could implement this by simply creating, and launching, a different Intent based on the value of childPosition. For example:
switch (childPosition)
{
case 0:
Intent intentChild0 = new Intent(yourMainActivity.this, ActivityForChild0.class);
startActivity(intentChild0);
break;
case 1:
Intent intentChild1 = new Intent(yourMainActivity.this, ActivityForChild1.class);
startActivity(intentChild1);
break;
//Etc...
default:
break;
}
Adding this to your OnChildClickListener will cause a different Activity to be launched depending on what child was clicked.
There are 2 solutions: one is switch the child position and redirect the activity.But this solution has a problem, because you will have many activities and your project will not be ordered.
The other solution is implement one activity and many fragments depends of how many child you have. In your activity you only redirect the fragment depends of the child id.

how to show show sqllite data in expendablelistview

I am creating expandable listview like this image so many tables, when i click any one show its child my questions how to create database just show only 1 like computer electronic when click on that tap show its childs i found this tutorial is use hardcode data plz how can i used sqlite database for expandablelistview?
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("Top 250");
listDataHeader.add("Now Showing");
listDataHeader.add("Coming Soon..");
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank Redemption");
top250.add("The Godfather");
top250.add("The Godfather: Part II");
top250.add("Pulp Fiction");
top250.add("The Good, the Bad and the Ugly");
top250.add("The Dark Knight");
top250.add("12 Angry Men");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("The Conjuring");
nowShowing.add("Despicable Me 2");
nowShowing.add("Turbo");
nowShowing.add("Grown Ups 2");
nowShowing.add("Red 2");
nowShowing.add("The Wolverine");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
comingSoon.add("The Smurfs 2");
comingSoon.add("The Spectacular Now");
comingSoon.add("The Canyons");
comingSoon.add("Europa Report");
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), comingSoon);
}
}
In your prepareListData()
// Adding child data
List<String> top250 = getTop250();
public List<String> getTop250() {
List<String> top250List = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
// Adding contact to list
top250List.add(cursor.getString(0));
} while (cursor.moveToNext());
}
// return top 250 list
return top250List;
}
similarly get other lists and add it to child, hope this helps and for CURD in sqlite DB look here

Categories

Resources