android


ExpandableListView start New Activity


I have problem when I want to start a new activity from a child
in an ExpandableListView
My Code:
package com.multilayerexpandable;
import java.util.ArrayList;
import android.app.ExpandableListActivity;
import android.content.Context;
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 MainActivity 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();
NewAdapter mNewAdapter = new NewAdapter(groupItem, childItem);
mNewAdapter
.setInflater(
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
this);
getExpandableListView().setAdapter(mNewAdapter);
expandbleLis.setOnChildClickListener(this);
}
public void setGroupData() {
groupItem.add("TechNology");
groupItem.add("Mobile");
groupItem.add("Manufacturer");
groupItem.add("Extras");
}
ArrayList<String> groupItem = new ArrayList<String>();
ArrayList<Object> childItem = new ArrayList<Object>();
public void setChildGroupData() {
/**
* Add Data For TecthNology
*/
ArrayList<String> child = new ArrayList<String>();
child.add("Java");
child.add("Drupal");
child.add(".Net Framework");
child.add("PHP");
childItem.add(child);
/**
* Add Data For Mobile
*/
child = new ArrayList<String>();
child.add("Android");
child.add("Window Mobile");
child.add("iPHone");
child.add("Blackberry");
childItem.add(child);
/**
* Add Data For Manufacture
*/
child = new ArrayList<String>();
child.add("HTC");
child.add("Apple");
child.add("Samsung");
child.add("Nokia");
childItem.add(child);
/**
* Add Data For Extras
*/
child = new ArrayList<String>();
child.add("Contact Us");
child.add("About Us");
child.add("Location");
child.add("Root Cause");
childItem.add(child);
}
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this, "Clicked On Child",
Toast.LENGTH_SHORT).show();
return true;
}
}
The adapter
package com.multilayerexpandable;
import java.util.ArrayList;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;
import android.widget.Toast;
#SuppressWarnings("unchecked")
public class NewAdapter extends BaseExpandableListAdapter {
public ArrayList<String> groupItem, tempChild;
public ArrayList<Object> Childtem = new ArrayList<Object>();
public LayoutInflater minflater;
public Activity activity;
public NewAdapter(ArrayList<String> grList, ArrayList<Object> childItem) {
groupItem = grList;
this.Childtem = childItem;
}
public void setInflater(LayoutInflater mInflater, Activity act) {
this.minflater = mInflater;
activity = act;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
tempChild = (ArrayList<String>) Childtem.get(groupPosition);
TextView text = null;
if (convertView == null) {
convertView = minflater.inflate(R.layout.childrow, null);
}
text = (TextView) convertView.findViewById(R.id.textView1);
text.setText(tempChild.get(childPosition));
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(activity, tempChild.get(childPosition),
Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return ((ArrayList<String>) Childtem.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return null;
}
#Override
public int getGroupCount() {
return groupItem.size();
}
#Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
#Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
#Override
public long getGroupId(int groupPosition) {
return 0;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = minflater.inflate(R.layout.grouprow, null);
}
((CheckedTextView) convertView).setText(groupItem.get(groupPosition));
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
The problem is how to start new activity when i clicked that child.
like HTC and start new HTC Activity.
This will give you access to the specific child clicked and hence the position in your List of items.
The implementation could look something like this example:
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this, "Clicked On Child"+childPosition,
Toast.LENGTH_SHORT).show();
// Go to child #0 specific class.
Intent child0Intent = new Intent(this, Child0Activity.class);
startActivity(child0Intent);
}
return false;
}
A full example of how to implement it can be found here:
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
if(groupPosition==0){
if(childPosition == 0){
Intent child0Intent = new Intent(getBaseContext(), Child0Activity.class);
startActivity(child0Intent);}
if(childPosition == 1){
Intent child1Intent = new Intent(getBaseContext(), Child1Activity.class);
startActivity(child1Intent);}
if(childPosition == 2){
Intent child2Intent = new Intent(getBaseContext(), Child2Activity.class);
startActivity(child2Intent);}
}
if(groupPosition==1){
if(childPosition == 3){
Intent child3Intent = new Intent(getBaseContext(), Child3Activity.class);
startActivity(child3Intent);}
if(childPosition == 4){
Intent child4Intent = new Intent(getBaseContext(), Child4Activity.class);
startActivity(child4Intent);}
if(childPosition == 5){
Intent child5Intent = new Intent(getBaseContext(), Child5Activity.class);
startActivity(child5Intent);}
}
return false;
}});
Intent intent = null;
switch (groupPosition){
case 0:
switch (childPosition){
case 0:
img.setImageResource(R.drawable.slika1);
intent = new Intent(activity, Activity1.class);
break;
....
}
break;
case 1:
switch (childPosition){
case 0:
img.setImageResource(R.drawable.slika2);
intent = new Intent(activity, Activity2.class);
break;
....
}
break;
}
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
activity.startActivity(intent);
}
});

Related Links

Is it okay to add tons of Firebase ValueEventListener on Android app?
Set recyclerview above alignParentBottom layout
What role of adapter, service, content provider and other basic elements in MVP architecture of android app?
How to pass request string param using Retrofit for file Upload?
Unable to getPackageName() using MediaPlayer in fragment
Lat and Lng always return 0.0,0.0
Detecting toast messages with AccessibilityService
Android proguard removes all classes in the root jar
How to use MemoryFile to perform IPC in android?
Request Slide menu example for android studio
Dagger 2 Scope for Fragments communication
Facebook shows two separate login dialogs and fails login
devtools extension doesn't work for inspeting android webview anymore in new version of Chrome(55.0.2883.87)
How to simulate a terminal in Android using Kivy?
Can't cherry pick (error)
Using android:largeHeap=“true” ? is a benefit or loss for an application that doesn't need it

Categories

HOME
twitter-bootstrap-3
eslint
themes
components
jwplayer
jsf-2.2
replace
database-normalization
pearson-correlation
lanczos
sendgrid-api-v3
resultset
redis-sentinel
cmdb
3nf
altera
screen-readers
gruntfile
forgot-password
wolframalpha
connector
springfox
spring-async
game-theory
sinch
comparable
user-defined-functions
finagle
google-cast
livelink
python-2.6
turfjs
biztalk-rule-engine
joomla2.5
pdw-file-browser
google-earth-engine
projects-and-solutions
spring-mvc-test
setup.py
doc
levenshtein-distance
photos
show-hide
hammer.js
tf
mongotemplate
zenhub
yii2-user
sundials
cup
pdf-conversion
mouseleave
ziparchive
webjars
async.js
vulcanize
burrows-wheeler-transform
i386
tooleap
refinerycms
clipboard.js
exim4
design-principles
sha512
dnssec
matlab-deployment
cakephp-2.7
phpldapadmin
rapidsvn
viewer
genymotion-call
gii
spring-remoting
square-bracket
xmltodict
scrypt
mongohq
workitem
composite-component
fb.ui
web-notifications
robotlegs
midlet
apache-whirr
entity-attribute-value
dataform
diagrams
request-headers
lobo-cobra
localizable.strings
technical-debt

Resources

Encrypt Message