Wednesday, May 4, 2011

Add Dynamic groups and childes in ExpandableListView in Android

package src.com;
import android.app.Activity;
import android.os.Bundle;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Hookup extends ExpandableListActivity {
ExpandableListAdapter mAdapter;
Context mContext;

private String[] groups = { "BFBFBF", "Testing" };
private String[][] children = { { "M", "N" }, { "One", "Four", "Six" } };

private static final String TAG = "Hookup";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set up our adapter
mAdapter = new MyExpandableListAdapter(this);
setListAdapter(mAdapter);
registerForContextMenu(getExpandableListView());

}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Hookup Menu");
menu.add(0, 0, 0, R.string.hello);
}

public boolean onContextItemSelected(MenuItem item) {
System.out.println("Insidded onContextItemSelected");

// Intent intent = new Intent();
// intent.setClass(this, HookupMapView.class);
// startActivity(intent);
Log.i(TAG, "onContextItemSelected");
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item
.getMenuInfo();

String title = ((TextView) info.targetView).getText().toString();
int type = ExpandableListView
.getPackedPositionType(info.packedPosition);

if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPos = ExpandableListView
.getPackedPositionGroup(info.packedPosition);
int childPos = ExpandableListView
.getPackedPositionChild(info.packedPosition);
Toast.makeText(
this,
title + ": Child " + childPos + " clicked in group "
+ groupPos, Toast.LENGTH_SHORT).show();
return true;
} else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
int groupPos = ExpandableListView
.getPackedPositionGroup(info.packedPosition);
Toast.makeText(this, title + ": Group " + groupPos + " clicked",
Toast.LENGTH_SHORT).show();

return true;
}

return false;
}

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for
// groups[i].
// private String[] groups = { "Xxxxxxx", "yyyyyy near me" };
// private String[][] children = { { "A", "B", "C" },
// { "1", "2", "3" } };

public MyExpandableListAdapter(Context context) {
mContext = context;
}

public Object getChild(int groupPosition, int childPosition) {
Log.i(TAG, "getChild");
return children[groupPosition][childPosition];
}

public long getChildId(int groupPosition, int childPosition) {
Log.i(TAG, "getChildId");
return childPosition;
}

public int getChildrenCount(int groupPosition) {
Log.i(TAG, "getChildId");
return children[groupPosition].length;
}

public LinearLayout getGenericView(String string) {
Log.i(TAG, "getGenericView");
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);

LinearLayout ll = new LinearLayout(mContext);
ImageView icon = new ImageView(mContext);
icon.setImageResource(R.drawable.icon);
icon.setPadding(35, 0, 0, 0);
ll.addView(icon);

TextView textView = new TextView(mContext);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(36, 0, 0, 0);
// CheckBox cBox = new CheckBox(mContext);
ll.setLayoutParams(lp);
ll.addView(textView);
// ll.addView(cBox);
textView.setText(string);
// cBox.setClickable(true);
// CheckBox cb = new CheckBox(mContext);
// ll.addView(cb);
return ll;

}

public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
Log.i(TAG, "getChildView");
// String myText =
// this.getChild(groupPosition,childPosition).toString();
// LinearLayout ll = getGenericView(myText+"BCD");
//textView.setText(getChild(groupPosition,childPosition).toString())
// ;
// return ll;
LayoutInflater layoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = layoutInflater.inflate(R.layout.bluetest_row, null);
TextView tt = (TextView) v.findViewById(R.id.text1);
String myText = this.getChild(groupPosition, childPosition)
.toString();
tt.setText(myText);
CheckBox cb = (CheckBox) v.findViewById(R.id.checkbox);
cb.setVisibility(View.VISIBLE);
ImageView icon = (ImageView) v.findViewById(R.id.rowicon);
icon.setImageResource(R.drawable.icon);
return v;
}

public Object getGroup(int groupPosition) {
Log.i(TAG, "getGroup");
return groups[groupPosition];
}

public int getGroupCount() {
Log.i(TAG, "getGroupCount");
return groups.length;
}

public long getGroupId(int groupPosition) {
Log.i(TAG, "getGroupId");
return groupPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Log.i(TAG, "getGroupView");
String myText = this.getGroup(groupPosition).toString();
return getGenericView(myText);
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
Log.i(TAG, "isChildSelectable");
return true;
}

public boolean hasStableIds() {
Log.i(TAG, "hasStableIds");
return true;
}

public void registerDataSetObserver(DataSetObserver observer) {
}

}

}


bluetest_row.xml


android:layout_weight="1" android:background="#fafafa">
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"
/>
android:text="myTest"
/>

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. dear Aftab and anfer
    its nice to hear that u able to make the child view dynamic. i m trying to make the child view dynamic. how could i do that. i m not understanding the above code. plx explain the steps.

    ReplyDelete
  3. dear sir
    i have two array(category and items). category array include id and name, another array containing item name,itemid,elated category id. how can i create dynamic expandable listview ,with category as header and items show in the related category. will you please explain ?

    ReplyDelete