How to use expandable list view in the following scenario - android

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>

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

Expandable list style Change?

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

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.

In android using OnClickListner for expandable listview

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!

Set onChildItemClickListner for ExpadableListView?

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

Categories

Resources