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!
Related
I'm a beginner on Android, i'm creating a movie application where the user can browse the movies and search for top rated or most popular ones.
i'm using DetailsFragment to display movie details, also i'm using an expandable list view to display movie's trailers and reviews
my problem is that i get an error on this line
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
that says: this cannot be referenced from static context and i can't solve it by using getActivity() or DetailsFragment.this
Sorry if I'm being extremely incompetent but I searched and I couldn't understand how to fix it. Thanks.
here is DetailsFragment.java
package com.example.android.movies;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static com.example.android.movies.R.id.favoriteButton;
import static com.example.android.movies.R.id.iv_poster;
/**
* A placeholder fragment containing a simple view.
*/
public class DetailsFragment extends Fragment{
static Long id;
String title, overview, poster, date;
String vote;
ImageView posterIV;
TextView titleTV, dateTV, overviewTV, voteTV;
static Context mContext;
static ExpandableListAdapter listAdapter;
static ExpandableListView expListView;
static List<String> listDataHeader;
static HashMap<String, List<Trailers>> listDataChild;
static ArrayList<Trailers> arrTrailers;
static ArrayList<Trailers> arrReviews;
ToggleButton favorite;
private MoviesDB db;
Trailers t;
public static DetailsFragment newInstance() {
return new DetailsFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_details, container, false);
// get the listview
expListView = (ExpandableListView) rootView.findViewById(R.id.lvExp);
// receiving intent
Intent gIntent = getActivity().getIntent();
Movies movie = null;
if (gIntent != null && gIntent.hasExtra("movie")) {
movie = gIntent.getParcelableExtra("movie");
id = movie.getMposter_id();
title = movie.getMtitle();
titleTV = (TextView) rootView.findViewById(R.id.tv_title);
titleTV.setText(title);
date=movie.getMdate();
dateTV = (TextView) rootView.findViewById(R.id.tv_date);
dateTV.setText(date);
vote=String.valueOf(movie.getMvote());
voteTV= (TextView) rootView.findViewById(R.id.tv_vote);
voteTV.setText(vote);
overview=movie.getMoverview();
overviewTV = (TextView) rootView.findViewById(R.id.tv_overview);
overviewTV.setText(overview);
poster = movie.getMposter_path();
posterIV = (ImageView) rootView.findViewById(iv_poster);
Picasso.with(getActivity())
.load("http://image.tmdb.org/t/p/w185".concat(poster)).into(posterIV);
favorite= (ToggleButton) rootView.findViewById(favoriteButton);
}
favorite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
db.insert(id,title,poster,date, Double.parseDouble(vote),overview,t.getContent1());
}
});
return rootView;
}
private static void settingAdapter() {
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
}
/*
* Preparing the list data
*/
private static void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<Trailers>>();
// Adding child data
listDataHeader.add("Reviews");
listDataHeader.add("Trailers");
listDataChild.put(listDataHeader.get(0), arrReviews); // Header, Child data
listDataChild.put(listDataHeader.get(1), arrTrailers);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mContext=getActivity();
new TrailersFetcher(getActivity(), id).execute();
}
public static void updateTrailers(ArrayList<Trailers> trailers){
arrTrailers = new ArrayList<>();
for (int i = 0; i <trailers.size() ; i++) {
arrTrailers.add(trailers.get(i));
}
new ReviewsFetcher(mContext, id).execute();
}
public static void updateReviews(ArrayList<Trailers> reviews){
arrReviews = new ArrayList<>();
for (int i = 0; i <reviews.size() ; i++) {
arrReviews.add(reviews.get(i));
}
settingAdapter();
}
}
Simlple OOP rule - Non-static variables/members/objects cannot be referenced in static context (methods, classes etc).
Why?
Static references does not require initialization to call, so there may be cases (mostly) that normal (non-static) members/objects are not initialized and a call is made to that (static) method. This will always throw NullPointerException (for reference types).
So its checked at compile time that non-static reference is not made in static context.
Solution..
either make a static object of ExpandableList, or
remove static from private static void settingAdapter().
I think you don't need so many static declarations.
modifier 'static' is only allowed in constant variable declarations.
Non-static variable this cannot be referenced from a static context.
private static void settingAdapter() {
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
}
Just remove 'static':
From Methods:
settingAdapter
prepareListData
updateTrailers
updateReviews
From Variables:
Context mContext
ExpandableListAdapter listAdapter
ExpandableListView expListView
List listDataHeader
HashMap> listDataChild
ArrayList arrTrailers
ArrayList arrReviews
I have an Expandable listview and I'm trying to start a new activity by clicking on the child row, But it doesn't work with me
Also I want to add child items and parent items both dynamically to be able to change them with several strings from string.xml
package com.example.khloud.finaaaaaal_hospital;
import java.util.ArrayList;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;
public class Third extends ExpandableListActivity implements
OnChildClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ExpandableListView expandbleLis = getExpandableListView();
expandbleLis.setDividerHeight(2);
expandbleLis.setGroupIndicator(null);
expandbleLis.setClickable(true);
setGroupData();
setChildGroupData();
MyExpandableAdapter mNewAdapter = new MyExpandableAdapter(groupItem, childItem);
mNewAdapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
this);
getExpandableListView().setAdapter(mNewAdapter);
expandbleLis.setOnChildClickListener(this);
}
public void setGroupData() {
groupItem.add("htc");
groupItem.add("sony");
}
ArrayList<String> groupItem = new ArrayList<String>();
ArrayList<Object> childItem = new ArrayList<Object>();
public void setChildGroupData() {
ArrayList<String> child = new ArrayList<String>();
child.add("1");
childItem.add(child);
child = new ArrayList<String>();
child.add("1");
child.add("2");
childItem.add(child);
}
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Intent child0Intent = new Intent(Third.this, Final_list.class);
startActivity(child0Intent);
return true;}}
while i am searching for examples to make list of answers in expandable list i found code which use 4 java classes and one xml with expand list but not use xml for parent and child and i want to change text color and check box color
XML
<Spinner
android:id="#+id/coursescomplaint"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_gravity="center"
android:spinnerMode="dialog"
style="#style/spinner_style"/>
<ExpandableListView
android:id="#+id/expandcomplaintcourse"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/Button"
android:text="Submit"
android:layout_gravity="center"/>
JAVA
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Complaint extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private static List<Answers> questions;
private ExpandableListView expandableListView;
private AnswersAdabter adapter;
private int lastExpandedPosition = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_complaint);
LoadQuestions();
expandableListView = (ExpandableListView)findViewById(R.id.expandcomplaintcourse);
adapter = new AnswersAdabter(this, questions);
expandableListView.setAdapter(adapter);
// The choice mode has been moved from list view to adapter in order
// to not extend the class ExpansibleListView
adapter.setChoiceMode(AnswersAdabter.CHOICE_MODE_SINGLE_PER_GROUP);
// Handle the click when the user clicks an any child
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
adapter.setClicked(groupPosition, childPosition);
return false;
}
});
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
if (lastExpandedPosition != -1 && groupPosition != lastExpandedPosition) {
expandableListView.collapseGroup(lastExpandedPosition);
}
lastExpandedPosition = groupPosition;
}
});
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.coursescomplaint);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Course1");
categories.add("Course2");
categories.add("Course3");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, R.layout.spinner, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
private void LoadQuestions() {
questions = new ArrayList<Answers>();
ArrayList<String> citiesAustralia = new ArrayList<String>(
Arrays.asList("Brisbane", "Hobart", "Melbourne", "Sydney"));
questions.add(new Answers("Australia", citiesAustralia));
ArrayList<String> citiesChina = new ArrayList<String>(
Arrays.asList("Beijing", "Chuzhou", "Dongguan", "Shangzhou"));
questions.add(new Answers("China", citiesChina));
ArrayList<String> citiesIndia = new ArrayList<String>(
Arrays.asList("Bombay", "Calcutta", "Delhi", "Madras"));
questions.add(new Answers("India", citiesIndia));
ArrayList<String> citiesNewZealand = new ArrayList<String>(
Arrays.asList("Auckland", "Christchurch", "Wellington"));
questions.add(new Answers("New Zealand", citiesNewZealand));
ArrayList<String> citiesRussia = new ArrayList<String>(
Arrays.asList("Moscow", "Kursk", "Novosibirsk", "Saint Petersburg"));
questions.add(new Answers("Russia", citiesRussia));
}
}
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>
So, I was wondering if there is any method to set Up onclickListner for Expandable List view..
I know that it is possible when you haven't implement Child data with "HashMap".
here is My code. I tired all possible onclick listeners for hashmap method. but no success yet.
enter code here
package com.prashant.dfs;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ExpandableListView;
public class Chapter_1_full 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_chapter_1_full);
//get the listView(expand)
expListView = (ExpandableListView) findViewById(R.id.ch1_expand);
//prepareDataList
prepareListData();
listAdapter=new ExpandableListAdapter(this, ListDataHeader, listDataChild);
expListView.setAdapter(listAdapter);
}
private void prepareListData() {
ListDataHeader = new ArrayList<String>();
listDataChild=new HashMap<String, List<String>>();
//Adding child Data
ListDataHeader.add("1.1 Introductin");
ListDataHeader.add("1.2 DataType");
ListDataHeader.add("1.3 ADT");
List<String> Intro = new ArrayList<String>();
Intro.add("WHAT is DFS");
Intro.add("Algorithem");
Intro.add("Flowchart");
List<String> datatype = new ArrayList<String>();
datatype.add("WHAT is DFS");
datatype.add("Algorithem");
datatype.add("Flowchart");
List<String> ADT = new ArrayList<String>();
ADT.add("WHAT is DFS");
ADT.add("Algorithem");
ADT.add("Flowchart");
listDataChild.put(ListDataHeader.get(0),Intro);
listDataChild.put(ListDataHeader.get(1),datatype);
listDataChild.put(ListDataHeader.get(2),ADT);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.chapter_1_full, menu);
return true;
}
}
Well, assuming your adapter is correctly implementing its methods...
expandableListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(final ExpandableListView parent, final View view, final int groupPosition, final int childPosition,
final long identifier) {
final Object childObj = parent.getExpandableListAdapter().getChild(groupPosition, childPosition);
if (childObj != null) {
// Do your magic
}
return true;
}
});