i am newbie in android development..
i m using expandablelistview
,
In each row of listview i have 3 textview
and 1 button
,on button click
i want selected child data to be on another activity
with his Group header name
is that possible 开发者_JAVA百科in Expandable list view
if its then how ?
please help... For expandable list just implement following code:
ExpandableListDemo.java
public class ExpandableListDemo extends Activity {
private ExpandableListView expandableListView;
private ArrayList<Places> places;
private PlaceAdapter placeAdapter;
DisplayMetrics metrics;
int width;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expandable_list_demo);
initUI();
initData();
placeAdapter=new PlaceAdapter(places, this);
expandableListView.setAdapter(placeAdapter);
expandableListView.setIndicatorBounds(width - GetDipsFromPixel(30), width - GetDipsFromPixel(5));
}
private void initData() {
places=new ArrayList<Places>();
Places place1=new Places();
place1.setName("Maharashtra");
String [] city1={"Pune","Mumbai","Nagpur","Aurangabad","Nasik"};
place1.setCities(city1);
Places place2=new Places();
place2.setName("Gujrat");
String [] city2={"Surat","Bhavnagar","Ahmedabad","Vadodara","Rajkot"};
place2.setCities(city2);
Places place3=new Places();
place3.setName("AndhraPradesh");
String [] city3={ "Hyderabad", "Visakhapatnam", "Vijayawada", "Guntur", "Rajahmundr" };
place3.setCities(city3);
Places place4=new Places();
place4.setName("Karnataka");
String [] city4={"Bagalkote","Bangalore","Belgaum","Bellary"};
place4.setCities(city4);
places.add(place1);
places.add(place2);
places.add(place3);
places.add(place4);
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
}
private void initUI() {
expandableListView=(ExpandableListView) findViewById(R.id.places_listview);
}
public int GetDipsFromPixel(float pixels)
{
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
}
Custom place adapter:
public class PlaceAdapter extends BaseExpandableListAdapter{
private ArrayList<Places> placesList;
private LayoutInflater mInflater;
public PlaceAdapter(ArrayList<Places> placesList,Context context) {
this.placesList=placesList;
mInflater=LayoutInflater.from(context);
}
//return child at specific position.
@Override
public Object getChild(int groupPosition, int childPosition) {
return placesList.get(groupPosition).getCities();
}
//return child id for specific position.
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
//return child view at specific position.
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder childHolder;
if(convertView==null) {
childHolder=new ChildHolder();
convertView=mInflater.inflate(R.layout.child_item,null);
childHolder.childTv=(TextView) convertView.findViewById(R.id.txt_chid_item);
convertView.setTag(childHolder);
}else{
childHolder=(ChildHolder) convertView.getTag();
}
childHolder.childTv.setText("-->"+placesList.get(groupPosition).getCities()[childPosition]);
return convertView;
}
//return number of child for specific parent.
@Override
public int getChildrenCount(int groupPosition) {
return placesList.get(groupPosition).getCities().length;
}
//get parent at specific position.
@Override
public Object getGroup(int groupPosition) {
return placesList.get(groupPosition);
}
//return number of parent items.
@Override
public int getGroupCount() {
return placesList.size();
}
//return parent id for specific position
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//return parent view for specific position
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ParentHolder parentHolder;
if(convertView==null) {
parentHolder=new ParentHolder();
convertView=mInflater.inflate(R.layout.parent_item,null);
parentHolder.parentTv=(TextView) convertView.findViewById(R.id.txt_parent_item);
convertView.setTag(parentHolder);
}else{
parentHolder=(ParentHolder) convertView.getTag();
}
parentHolder.parentTv.setText(placesList.get(groupPosition).getName());
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
private class ParentHolder {
TextView parentTv;
}
private class ChildHolder {
TextView childTv;
}
}
Places model:
public class Places {
private String name;
private String [] cities;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getCities() {
return cities;
}
public void setCities(String[] cities) {
this.cities = cities;
}
}
activity_expandable_list_demo.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ExpandableListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/places_listview"
android:groupIndicator="@drawable/arrow_selector"
></ExpandableListView>
</RelativeLayout>
child_item.xml
<?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" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt_chid_item"/>
</RelativeLayout>
parent_item.xml
<?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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt_parent_item"/>
</RelativeLayout>
AndroidManifest.xml
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ExpandableListDemo"
android:label="@string/title_activity_expandable_list_demo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
(Use above icon in your drawable) I hope this code will more helpful to implement expandable list view
This will help u out:-
-------------in expandable activity------------------
private void loadHosts(final ArrayList<Parent> newParents) {
if (newParents == null)
return;
parents = newParents;
Log.d("StackOverFlow", "parents arrived: " + newParents.size());
if (this.getExpandableListAdapter() == null) {
mAdapter = new MyExpandableListAdapter(this, parents, R.layout.adapter_horizontal);
this.setListAdapter(mAdapter);
} else
((MyExpandableListAdapter) getExpandableListAdapter()).notifyDataSetChanged();
}
//--------------custom adapter---
import java.util.ArrayList;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.merck.activity.R;
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private LayoutInflater inflater;
private ArrayList<Parent> parents;
private int parentLayoutResource;
/**
* A simple adapter which maintains an ArrayList of photo resource Ids. Each
* photo is displayed as an image. This adapter supports clearing the list
* of photos and adding a new photo.
*/
public MyExpandableListAdapter(Context context, ArrayList<Parent> parents, int parentLayoutResource) {
inflater = LayoutInflater.from(context);
this.parents = parents;
this.parentLayoutResource = parentLayoutResource;
}
ImageView img;
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parentView) {
final Parent parent = parents.get(groupPosition);
convertView = inflater.inflate(parentLayoutResource, null, false);
((TextView) convertView.findViewById(R.id.top)).setText(parent.getName());
img = (ImageView) convertView.findViewById(R.id.arrow);
if(!isExpanded){
img.setImageResource(R.drawable.arrow_gray );
}
else
{
img.setImageResource(R.drawable.arrow_gray_down );
}
// CheckBox checkbox = (CheckBox)
// convertView.findViewById(R.id.checkbox);
// checkbox.setChecked(parent.isChecked());
// if (parent.isChecked())
// convertView.setBackgroundResource(R.color.red);
// else
// convertView.setBackgroundResource(R.color.blue);
// checkbox.setOnCheckedChangeListener(new CheckUpdateListener(parent));
return convertView;
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
};
private EditText childText1;
private EditText childText2;
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parentView) {
convertView = inflater.inflate(R.layout.user_profile, parentView, false);
childText1 = (EditText) convertView.findViewById(R.id.et_password);
childText1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
MedListManager.getInstance().setChildText1(arg0.toString());
System.out.println("!!!!! setChildText1"+arg0.toString());
}
});
childText2 = (EditText) convertView.findViewById(R.id.et_confirm_pass);
childText2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
MedListManager.getInstance().setChildText2(arg0.toString());
System.out.println("!!!!! setChildText2"+arg0.toString());
}
});
return convertView;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return parents.get(groupPosition).getChildren().get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition) {
return parents.get(groupPosition).getChildren().size();
}
@Override
public Object getGroup(int groupPosition) {
return parents.get(groupPosition);
}
@Override
public int getGroupCount() {
return parents.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public boolean isEmpty() {
return ((parents == null) || parents.isEmpty());
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean areAllItemsEnabled() {
return true;
}
}
精彩评论