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.
Related
Followed the tutorial from androidhive to create an expandable listview.
http://www.learn-android-easily.com/2013/07/android-tabwidget-example.html
Followed tutorial to create horizontal tabs.
Edited the Tab Widget Example (Second tutorial) to put the expandable listview in the tabs. Did this by creating the expandable list adapter java file in the project and copying the expandable list code into the tab activity file. (Brought in the required xml files too) The tab activity then looked like this:
import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Tab1Activity 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.expandable_listview);
// 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);
}
// 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);
}
}
Worked fine. Tried to do the same with project from here:
http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
Here I've got a few errors. I think that setContentView can't be used with fragments so I got rid of it and kept the original inflater.
Similarly I think I need to use something else instead of the findViewById and change my arguments for the ExpandableListAdapter(). Not really sure what this needs to be changes to however..
My tab fragment:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class GamesFragment extends Fragment
{
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_games, container, false);
super.onCreate(savedInstanceState);
// 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);
return rootView;
}
// 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);
}
}
My tab fragment 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"
android:orientation="vertical"
android:background="#ff8400" >
<ExpandableListView
android:id="#+id/lvExp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ExpandableListView>
</RelativeLayout>
Do let me know if I am going about this the wrong way. All I'm looking to do is create swipe views with tabs which have expandable listviews in them.
1) Call findViewById() on the View returned
findViewById in Fragment
2) Change the first argument in the ExpandableListAdapter from 'this' to 'getActivity()'
My new onCreateView looks like this:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.tab_expandable_list, container, false);
// get the listview
expListView = (ExpandableListView) view.findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
return view;
}
I am creating an application, which has different screens for admin users and different screens for normal users. When admin logs in, screen will be displayed which consists of expandable list views. The Expandable list view header is a string array. The child items are the list of values obtained from database. Now, please let me know how can I use expandable list view in my case? Since I have different list for child views should I use many adapters? When I try to use ExpandableListAdapter, It tells me to implement some 8 methods, should I use all those if yes how? The following code snippet is what which I have now:
This is my Admin Activity class:
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleCursorTreeAdapter;
import java.util.List;
public class AdminActivity extends AppCompatActivity {
Toolbar toolbar;
ExpandableListAdapter listAdapter;
List<String> titleText;
SQLiteDataBaseAdapter db;
ExpandableListView login, android, ios, testing, java, dotNet, os, hr, others;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
toolbar = (Toolbar) findViewById(R.id.appBar);
toolbar.setTitle(" Admin Screen");
toolbar.setTitleTextColor(Color.WHITE);
login = (ExpandableListView) findViewById(R.id.expandableListViewLogin);
android = (ExpandableListView) findViewById(R.id.expandableListViewAndroid);
ios = (ExpandableListView) findViewById(R.id.expandableListViewIos);
testing = (ExpandableListView) findViewById(R.id.expandableListViewTesting);
java = (ExpandableListView) findViewById(R.id.expandableListViewJava);
dotNet = (ExpandableListView) findViewById(R.id.expandableListViewDotNet);
os = (ExpandableListView) findViewById(R.id.expandableListViewOS);
hr = (ExpandableListView) findViewById(R.id.expandableListViewHR);
others = (ExpandableListView) findViewById(R.id.expandableListViewOthers);
// Lsit of values for header. One for each list view.
titleText.add("User Id Authentication");
titleText.add("Android Posts Authentication");
titleText.add("iOS Posts Authentication");
titleText.add("Testing Posts Authentication");
titleText.add("Java Posts Authentication");
titleText.add("Dot Net Posts Authentication");
titleText.add("OS Posts Authentication");
titleText.add("HR Posts Authentication");
titleText.add("Others Posts Authentication");
SQLiteDataBaseAdapter db = new SQLiteDataBaseAdapter(this);
List<String> childData = db.getAndroidList();
//setting the list adapter
listAdapter = new ExpandableListAdapter(this, titleText, childData);// this tells to implement some 8 methods, should I implement??
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_admin, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I have so many expandable list views in one screen The array list is for the headers one for each expandable list view, the children will be again list of values from database. Please let me know how to use expandable list view in my case. I am very new to android and this is the first time I am working on Expandable List View. All suggestions are welcome. Thanks in advance.
You can find good tutorials for Expandable listview in the following link.
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
You can remove unwanted header and child from the String List(based on Admin/User) before give it as input to Expandable list view adapter
You should set adapter:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
toolbar = (Toolbar) findViewById(R.id.appBar);
toolbar.setTitle(" Admin Screen");
toolbar.setTitleTextColor(Color.WHITE);
login = (ExpandableListView) findViewById(R.id.expandableListViewLogin);
android = (ExpandableListView) findViewById(R.id.expandableListViewAndroid);
ios = (ExpandableListView) findViewById(R.id.expandableListViewIos);
testing = (ExpandableListView) findViewById(R.id.expandableListViewTesting);
java = (ExpandableListView) findViewById(R.id.expandableListViewJava);
dotNet = (ExpandableListView) findViewById(R.id.expandableListViewDotNet);
os = (ExpandableListView) findViewById(R.id.expandableListViewOS);
hr = (ExpandableListView) findViewById(R.id.expandableListViewHR);
others = (ExpandableListView) findViewById(R.id.expandableListViewOthers);
titleText.add("User Id Authentication");
titleText.add("Android Posts Authentication");
titleText.add("iOS Posts Authentication");
titleText.add("Testing Posts Authentication");
titleText.add("Java Posts Authentication");
titleText.add("Dot Net Posts Authentication");
titleText.add("OS Posts Authentication");
titleText.add("HR Posts Authentication");
titleText.add("Others Posts Authentication");
SQLiteDataBaseAdapter db = new SQLiteDataBaseAdapter(this);
List<String> childData = db.getAndroidList();
//setting the list adapter
listAdapter = new ExpandableListAdapter(this, titleText, childData);
ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
listView.setAdapter(listAdapter);
}
**Its Working**
package com.keshav.myexpandablelistviewexampleworkinginactivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
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);
// tODO get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// TODO 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() {
// TODO Colapse Here Using this... in android
int previousGroup = -1;
boolean flag = false;
#Override
public void onGroupExpand(int groupPosition) {
Log.e("keshav", "onGroupClick is -> " + groupPosition);
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
if (groupPosition != previousGroup && flag) {
expListView.collapseGroup(previousGroup);
}
previousGroup = groupPosition;
flag = true;
}
});
// 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();
}
});
// Todo 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("Months");
listDataHeader.add("Top 250");
listDataHeader.add("Now Showing");
listDataHeader.add("Coming Soon..");
// Adding child data
List<String> weeks = new ArrayList<String>();
weeks.add("Sunday");
weeks.add("Monday");
weeks.add("Tuesday");
weeks.add("Wednesday");
weeks.add("Thursday");
weeks.add("Friday");
weeks.add("Saturday");
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("Om Shanti Om");
top250.add("Badshah");
top250.add("Bahubali Part 1");
top250.add("Carry on Jatta");
top250.add("Sholey");
top250.add("Mard");
top250.add("Dewwar");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("Bahubali");
nowShowing.add("Kabali");
nowShowing.add("Luckky Di Unlukky Story");
nowShowing.add("Sachin Billions Dream");
nowShowing.add("Red 2");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("Tubelight ");
comingSoon.add("Bahubali 3 2018");
comingSoon.add("Dhoom 4");
comingSoon.add("Hindi Medium");
listDataChild.put(listDataHeader.get(0), weeks);
listDataChild.put(listDataHeader.get(1), top250); // Header, Child data
listDataChild.put(listDataHeader.get(2), nowShowing);
listDataChild.put(listDataHeader.get(3), comingSoon);
}
}
package com.keshav.myexpandablelistviewexampleworkinginactivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
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);
// tODO get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// TODO 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() {
// TODO Colapse Here Using this... in android
int previousGroup = -1;
boolean flag = false;
#Override
public void onGroupExpand(int groupPosition) {
Log.e("keshav", "onGroupClick is -> " + groupPosition);
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
if (groupPosition != previousGroup && flag) {
expListView.collapseGroup(previousGroup);
}
previousGroup = groupPosition;
flag = true;
}
});
// 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();
}
});
// Todo 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("Months");
listDataHeader.add("Top 250");
listDataHeader.add("Now Showing");
listDataHeader.add("Coming Soon..");
// Adding child data
List<String> weeks = new ArrayList<String>();
weeks.add("Sunday");
weeks.add("Monday");
weeks.add("Tuesday");
weeks.add("Wednesday");
weeks.add("Thursday");
weeks.add("Friday");
weeks.add("Saturday");
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("Om Shanti Om");
top250.add("Badshah");
top250.add("Bahubali Part 1");
top250.add("Carry on Jatta");
top250.add("Sholey");
top250.add("Mard");
top250.add("Dewwar");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("Bahubali");
nowShowing.add("Kabali");
nowShowing.add("Luckky Di Unlukky Story");
nowShowing.add("Sachin Billions Dream");
nowShowing.add("Red 2");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("Tubelight ");
comingSoon.add("Bahubali 3 2018");
comingSoon.add("Dhoom 4");
comingSoon.add("Hindi Medium");
listDataChild.put(listDataHeader.get(0), weeks);
listDataChild.put(listDataHeader.get(1), top250); // Header, Child data
listDataChild.put(listDataHeader.get(2), nowShowing);
listDataChild.put(listDataHeader.get(3), comingSoon);
}
}
activity_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"
android:background="#f4f4f4" >
<ExpandableListView
android:id="#+id/lvExp"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:cacheColorHint="#00000000"/>
</LinearLayout>
list_group.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="wrap_content"
android:orientation="vertical"
android:padding="8dp"
android:background="#000000">
<TextView
android:id="#+id/lblListHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="17dp"
android:textColor="#f9f93d" />
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:orientation="vertical" >
<TextView
android:id="#+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dip"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textColor="#000000"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
</LinearLayout>
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.
I am new to android please help me out for this problem.
In expandable list view clicking on each child list new activity should open.
here is my code
package com.example.index;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
public class IndexMainActivity 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_index_main);
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
}
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Part 1");
listDataHeader.add("Part 2");
listDataHeader.add("Part 3");
// Adding child data
List<String> parta = new ArrayList<String>();
parta.add("Sweet Hour Of Prayer");
parta.add("Prayer and Royal Family");
parta.add("The Holy Bible-King James Version");
parta.add("William shakespeare - Scriptres about prayer");
List<String> partb = new ArrayList<String>();
partb.add("Samuel Rutherford/scriptures-Thankfulness");
List<String> partc = new ArrayList<String>();
partc.add("Matthew Henry/Scriptures on - Faith");
partc.add("John Wesley/Scriptures on - Freedom");
partc.add("Charles Simeon/Scriptures on -Protection");
partc.add("Christmas Evans/Scriptures on - Guidance");
listDataChild.put(listDataHeader.get(0), parta); // Header, Child data
listDataChild.put(listDataHeader.get(1), partb);
listDataChild.put(listDataHeader.get(2), partc);
}
}
there are three list i.e part a , part b and part c now by clicking on part a i will get four child list by clicking on first child list i.e Sweet Hour of prayer new activity should open.
Try Below Code:
public class MyActivity extends Activity implements ExpandableListView.OnChildClickListener
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.event_mainactivity);
mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
ExpandableListAdapter adapter = new ExpandableListAdapter(this,
mExpandableListView, mGroupCollection);
mExpandableListView.setAdapter(adapter);
mExpandableListView.setOnChildClickListener(this);
}
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
{
Toast.makeText(getApplicationContext(), "Go to Activity :: "+childPosition, Toast.LENGTH_LONG).show();
return true;
}
}
There is a onChildClickListener for ExpandableListView. Check this code:
yourListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// Open activity using intent...
return false;
}
});
Hope it helps!
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