I am using an expandible view where parent is a text view and children are edittext views. I want to show different hints for different edit texts. I tried to do it programmatically but no success at all. Here is my code.
ContactFragment.java
public class ContactFragment extends Fragment {
private View mView;
Context mContext;
EditText edittext;
ExpandableListAdapter listAdapter;
ExpandableListView elv;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getActivity();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_contact_details, container, false);
ExpandableListView elv = (ExpandableListView) mView.findViewById(R.id.list);
prepareListData();
listAdapter = new ExpandableListAdapter(mContext, listDataHeader,listDataChild);
elv.setAdapter(listAdapter);
elv.setOnGroupClickListener(new ExpandableListView.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
elv.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(mContext,
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
elv.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(mContext,
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
mContext,
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
return mView;
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Contact Details");
// Adding child data
List<String> basic = new ArrayList<String>();
basic.add(" ");
basic.add(" ");
basic.add(" ");
basic.add(" ");
basic.add(" ");
listDataChild.put(listDataHeader.get(0), basic); // Header, Child data
}
}
ExpandableListAdapter.java
public class ExpandableListAdapter extends BaseExpandableListAdapter implements TextWatcher{
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
private ArrayList<EditText> editTextList = new ArrayList<EditText>();
int i=1;
String name,purpose;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
#Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.fragment_contact_details_child, null);
}
EditText editetext = (EditText) convertView .findViewById(R.id.item);
while(i<6)
{
switch (i)
{
case 1:editetext.setHint("Contact Person");
break;
case 2:editetext.setHint("Contact Person Photo");
break;
case 3:editetext.setHint("Designation/Job");
break;
case 4:editetext.setHint("CEO Name");
break;
case 5:editetext.setHint("Company Name");
break;
}
i++;
}
editetext.addTextChangedListener(this);
editTextList.add(editetext);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this._listDataHeader.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.fragment_contact_details_parent, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.textViewParent);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
name=editTextList.get(0).getText().toString();
purpose=editTextList.get(1).getText().toString();
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
System.out.println("Name is"+name);
System.out.println("purpose is"+purpose);
}
}
In your ExpandableListAdapter class under getChildView it looks like you are telling the app to loop through all of the hints and it finishes on "Company Name"
maybe change
while(i<6)
{
switch (i)
{
case 1:editetext.setHint("Contact Person");
break;
case 2:editetext.setHint("Contact Person Photo");
break;
case 3:editetext.setHint("Designation/Job");
break;
case 4:editetext.setHint("CEO Name");
break;
case 5:editetext.setHint("Company Name");
break;
}
i++;
}
to
switch (childPosition)
{
case 1:editetext.setHint("Contact Person");
break;
case 2:editetext.setHint("Contact Person Photo");
break;
case 3:editetext.setHint("Designation/Job");
break;
case 4:editetext.setHint("CEO Name");
break;
case 5:editetext.setHint("Company Name");
break;
}
actual i have better solution why dont u simply use xml code to put hint in your edit text.It will be easy for u and it is not mendetory to code it programmatically.hope u get it
Related
I have a display problem with my 4-stage expandableList.
I can display my data, expand them but the last child does not display the whole list. How would you do it?
Here is a picture and the part of the code in question, I think it comes from the fact that I declare the size of the last list.
public static class ThirdLevelAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<String> materiau;
private HashMap<String, ArrayList<MatosItem>> materiauItemList;
int c = 1;
LayoutInflater inflater3;
public ThirdLevelAdapter(Context context,HashMap<String, ArrayList<MatosItem>> materiauItemList,ArrayList<String> materiau) {
this.context = context;
this.materiauItemList = materiauItemList;
this.materiau = materiau;
this.inflater3 = LayoutInflater.from(context);
}
#Override
public Object getGroup(int groupPosition) {
return materiau.get(groupPosition);
}
#Override
public int getGroupCount() {
return materiau.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext())
.inflate(android.R.layout.simple_expandable_list_item_1,
parent,false);
//Initialization and assign variable
TextView textView = convertView.findViewById(android.R.id.text1);
//Initial string pour le nom de famille
String sGroup = String.valueOf(getGroup(groupPosition));
//Set text on text view
textView.setText(sGroup);
//Set text style bold
textView.setTypeface(null, Typeface.BOLD);
//Set text color
//textView.setBackgroundColor(Color.parseColor("#FF018786"));
textView.setTextColor(Color.parseColor("#FF018786"));
textView.setTextSize(1,20);
//Return View
}
return convertView;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return materiauItemList.get(materiau.get(groupPosition)).get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
view = inflater3.inflate(R.layout.adapter_item2, null);
//get information about item
MatosItem currentItem = (MatosItem) getChild(i,i1);
String itemName = currentItem.getName();
String itemNumber = currentItem.getNumber();
Boolean intemCheck = currentItem.getCheck();
String itemRef = currentItem.getRef();
String itemCodefourn = currentItem.getCodefourn();
//get item name view
TextView itemNameView = view.findViewById(R.id.name);
itemNameView.setText(itemName);
//get item number view
TextView itemNumberView = view.findViewById(R.id.item_number);
itemNumberView.setText(itemNumber);
//get item ref view
TextView itemRefView = view.findViewById(R.id.ref);
itemRefView.setText(itemRef);
//get item codefournisseur
TextView itemCodefournView = view.findViewById(R.id.codefourn);
itemCodefournView.setText(itemCodefourn);
LinearLayout layout = view.findViewById(R.id.compteur);
Button btnplus = view.findViewById(R.id.btnplus);
Button btnmoins = view.findViewById(R.id.btnmoins);
TextView text = view.findViewById(R.id.text);
if(intemCheck){
//checkBox.setChecked(true);
mdescription.add(itemName);
mid.add(itemNumber);
mref.add(itemRef);
mcodefourn.add(itemCodefourn);
}
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
layout.setVisibility(View.VISIBLE);
mdescription.add(""+itemName);
mid.add(""+itemNumber);
mref.add(itemRef);
mcodefourn.add(itemCodefourn);
//checkBox.setChecked(true);
//Toast.makeText(viewGroup.getContext(),itemName , Toast.LENGTH_SHORT).show();
}
});
btnplus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
c=c+1;
text.setText(""+c);
mdescription.add(""+itemName);
mid.add(""+itemNumber);
mref.add(itemRef);
mcodefourn.add(itemCodefourn);
}
});
btnmoins.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
c=c-1;
text.setText(""+c);
mdescription.remove(""+itemName);
mid.remove(""+itemNumber);
mref.remove(itemRef);
mcodefourn.remove(itemCodefourn);
}
});
return view;
}
#Override
public int getChildrenCount(int groupPosition) {
return materiauItemList.get(materiau.get(groupPosition)).size();
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
level 1-2 and 3 work fine and i can scroll
level 4 is not displayed completely and I can't scroll
Thank you for your help
In my Expandable Listview, have three groups. I want to add different three image for that group. i tried many codes, but not working.
My code:
Main Activity:
HashMap<String, List<String>> rightDrawerListDetail = getData();
List<String> rightDrawerListTitle = new ArrayList<String>(rightDrawerListDetail.keySet());
adapterR = new CustomExpandableListAdapter(this, rightDrawerListTitle,rightDrawerListDetail);
mRightDrawerList.setAdapter(adapterR);
private HashMap<String, List<String>> getData(){
HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
ArrayList<String> arrayList = mydb.getAllAddress();
List asRider = new ArrayList();
asRider.add("hello");
List asRidee = new ArrayList();
asRidee.add("hai");
List recent = new ArrayList();
recent.add("Success");
expandableListDetail.put("As a Rider",asRider);
expandableListDetail.put("As a Ridee",asRidee);
expandableListDetail.put("My Recent Activities",recent);
return expandableListDetail;
}
CustomExpandableListAdapter.java:
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private List<String> mGroups;
private LayoutInflater mInflater;
private HashMap<String, List<String>> mexpandableListDetail;
public CustomExpandableListAdapter(Context context, List<String> groups,HashMap<String, List<String>> expandableListDetail) {
mContext = context;
mGroups = groups;
mexpandableListDetail = expandableListDetail;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getGroupCount() {
return mGroups.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return mexpandableListDetail.get(mGroups.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return mGroups.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return mexpandableListDetail.get(mGroups.get(groupPosition)).get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return 0;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.right_drawer_group, null);
}
// Get the group item
String listTitle = (String) getGroup(groupPosition);
// Set group name
TextView textView = (TextView) convertView.findViewById(R.id.textGroup);
textView.setText(listTitle);
ImageView indicator = (ImageView) convertView.findViewById(R.id.groupIndicator);
if (isExpanded) {
indicator.setImageResource(R.drawable.arrowup);
} else {
indicator.setImageResource(R.drawable.arrowdown);
}
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.right_drawer_child, null);
}
// Get child name
String children = (String) getChild(groupPosition, childPosition);
// Set child name
TextView text = (TextView) convertView.findViewById(R.id.textChild);
text.setText(children);
/*convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, children, Toast.LENGTH_SHORT).show();
}
});*/
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Please anyone help to me!
Thanks in advance
In your right_drawer_group.xml add an ImageView where you want your group image to be placed, lets say groupImage.
In your getGroupView:
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.right_drawer_group, null);
}
//Set group image
ImageView groupImage = (ImageView)convertView.findViewById(R.id.groupImage);
switch((String)getGroup(groupPosition))
{
case "As a Rider":
//groupImage set correct image
break;
case "As a Ridee":
//groupImage set correct image
break;
case "My Recent Activities":
//groupImage set correct image
break;
}
// Get the group item
String listTitle = (String) getGroup(groupPosition);
// Set group name
TextView textView = (TextView) convertView.findViewById(R.id.textGroup);
textView.setText(listTitle);
ImageView indicator = (ImageView) convertView.findViewById(R.id.groupIndicator);
if (isExpanded) {
indicator.setImageResource(R.drawable.arrowup);
} else {
indicator.setImageResource(R.drawable.arrowdown);
}
return convertView;
}
It's not the most neat solution as if you change the group name this will break, but it will work for now. Better solution would be is to pass the groups as objects containing getName and getImageId.
I'm using expandable listView and Expandable ListAdapter.
ChlidView show me the titles and child View show me the subTitle .
In the "child View" i have a button that delete the the specific row .
i have two classes, one for loading the data from database into the hashMap and the second class for the ExpandableListAdapter. i create the onClick method on the adapter class , and the delete function is working, but the list not refreshing the updated data. i know i need to load again the data to the hashMap but the adapter class is outSide the first class. i'm trying to use "notifyDataSetChanged" and no result too.
class 1 :
hand = new DbHandler(getActivity());
fullList = new HashMap<ArrayList<ClockModel>, ArrayList<ClockModel>>();
temp = new ArrayList<ClockModel>();
ArrayList<ClockModel> child = new ArrayList<ClockModel>();
child = hand.getDay(workName);
for (int i = 0; i < child.size(); i++) {
if(child.get(i).getDateMonth() == month && child.get(i).getDateYear()==year){
ClockModel m1 = new ClockModel(
child.get(i).getId(),
child.get(i).getWorkName(),
child.get(i).getDateDay(),
child.get(i).getDateMonth(),
);
temp.add(m1);
fullList.put(temp, temp);
}
}
adapter = new ExpandableListCustom(getActivity(), fullList,temp);
lv.setAdapter(adapter);
adapter :
#Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
//final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater)
this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.row_child,null);
}
TextView txMyVal = (TextView) convertView.findViewById(R.id.row_myVal);
TextView txBreakTime = (TextView) convertView.findViewById(R.id.row_breakTime);
TextView txComment = (TextView) convertView.findViewById(R.id.row_comments);
TextView txNameOfDay = (TextView) convertView.findViewById(R.id.row_nameOfDay);
String comments= fullList.get(child).get(groupPosition).getComment();
nameOfDay= fullList.get(child).get(groupPosition).getNameOfDay();
shiftType= fullList.get(child).get(groupPosition).getShiftType();
int breakTime= fullList.get(child).get(groupPosition).getBreakTime();
currentPosition = fullList.get(child).get(groupPosition).getId();
btnRemove = (ImageButton) convertView.findViewById(R.id.row_btnRemove);
btnRemove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
hand.deleteDay(currentPosition);
Toast.makeText(context, "Del : "+currentPosition, 2000).show();
}
});
public class ExpandableListCustom extends BaseExpandableListAdapter implements OnClickListener {
DbHandler hand;
private Context context;
private HashMap<ArrayList<ClockModel>,ArrayList<ClockModel> > fullList; //Headers
ArrayList<ClockModel> child; //data
MySharedPreferences preferences;
public ExpandableListCustom(Context context,
HashMap<ArrayList<ClockModel>, ArrayList<ClockModel>> fullList,
ArrayList<ClockModel> child) {
super();
this.context = context;
this.fullList = fullList;
this.child = child;
clockSet = new Clock();
preferences = new MySharedPreferences(context);
hand = new DbHandler(context);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return this.fullList.get(this.fullList.get(childPosition));
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
//final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater)
this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.row_child,null);
}
TextView txMyVal = (TextView) convertView.findViewById(R.id.row_myVal);
TextView txBreakTime = (TextView) convertView.findViewById(R.id.row_breakTime);
TextView txComment = (TextView) convertView.findViewById(R.id.row_comments);
TextView txNameOfDay = (TextView) convertView.findViewById(R.id.row_nameOfDay);
enterHour= (int) fullList.get(child).get(groupPosition).getEnterHour();
exitHour= (int) fullList.get(child).get(groupPosition).getExitHour();
enterMin= (int) fullList.get(child).get(groupPosition).getEnterMin();
exitMin= (int) fullList.get(child).get(groupPosition).getExitMin();
currentPosition = fullList.get(child).get(groupPosition).getId();
btnRemove = (ImageButton) convertView.findViewById(R.id.row_btnRemove);
btnRemove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
hand.deleteDay(currentPosition);
Toast.makeText(context, "Del : "+currentPosition, 2000).show();
}
});
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub //////////////////////////////////////////
// return this.fullList.get(groupPosition).size();
if(fullList != null){
return 1;
}else{
return 0;
}
}
#Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return this.fullList.get(child).get(groupPosition);
}
#Override
public int getGroupCount() {
if(fullList != null){
return this.fullList.size();
}else{
return 0;
}
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
int dateDay= fullList.get(child).get(groupPosition).getDateDay();
int dateMonth= fullList.get(child).get(groupPosition).getDateMonth();
int dateYear= fullList.get(child).get(groupPosition).getDateYear();
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.row_header, null);
}
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
}
Now when you delete the item from the database you need at the same time to remove it from the adapter and call notifyDataSetChanged():
#Override
public void onClick(View v) {
hand.deleteDay(currentPosition);
fullList.remove(currentPosition);
notifyDataSetChanged();
}
you need to fetch data from database and assign to arraylist "fullList" which is used for adapter when you delete the data and use ...
notifyDataSetChanged();
method of adapter...
I want to call another activity when i click button which is child view of expandable listview in Android.When i run the activity does not show the expandable listview.How to work on exapandable list view.Can some one give me idea how to do this.
Here is my Activity code
public class My_Project extends ExpandableListActivity
{
ExpandableListView expListView;
Button btnNewProject;
Button btn_myprojectdefinemyteam;
DBHelper databaseHelper;
SQLiteDatabase db;
ImageView imgButtonBack;
List dispDataList;
String[] array;
String[] Task_Title_Size;
String[] Task_Start_Date_Size;
String[] Task_CompletionDate_Size;
String[] Task_CompletionTime_Size;
String[] Task_Description_Size;
String[] Task_Status_Size;
String[] Task_IsActive_Size;
String[] dtrProjectNAmeSize;
public static final String Task_Title = "Task_Titles";
public static final String Task_Start_Date = "start_date";
public static final String Task_CompletionDate = "completion_date";
public static final String Task_CompletionTime = "completion_time";
public static final String Task_Description = "task_description";
public static final String Task_Status = "task_status";
public static final String Task_IsActive ="IS_Active";
private int ParentClickStatus=-1;
private int ChildClickStatus=-1;
Myexpandable_ListAdapter mAdapter;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.my_project);
imgButtonBack = (ImageView)findViewById(R.id.imagBackButton);//ImageView imgButtonBack;
imgButtonBack.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent iBack = new Intent(My_Project.this , Menu.class);
startActivity(iBack);
finish();
}
});
expListView = (ExpandableListView) findViewById(android.R.id.list);
databaseHelper = new DBHelper(getApplicationContext());
dispDataList=databaseHelper.viewMyProjectDetals();
dtrProjectNAmeSize=new String[dispDataList.size()];
System.out.println(" dtrProjectNAmeSize = " + dtrProjectNAmeSize);
if ( dispDataList.size() > 6 )
{
Task_Title_Size=(String[])dispDataList.get(0);
Task_Start_Date_Size=(String[]) dispDataList.get(1);
Task_CompletionDate_Size = (String[])dispDataList.get(2);
Task_CompletionTime_Size=(String[])dispDataList.get(3);
Task_Description_Size=(String[]) dispDataList.get(4);
Task_Status_Size = (String[])dispDataList.get(5);
Task_IsActive_Size = (String[])dispDataList.get(6);
for(int i=0;i<Task_Title_Size.length;i++)
{
System.out.println("New data :"+Task_Title_Size[i]);
}
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < Task_Title_Size.length ; i++)
{
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(Task_Title,"" +Task_Title_Size[i]);
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(Task_Start_Date,"" +Task_Start_Date_Size[i]);
curChildMap.put(Task_CompletionDate,"" +Task_CompletionDate_Size[i]);
curChildMap.put(Task_CompletionTime,"" +Task_CompletionTime_Size[i]);
curChildMap.put(Task_Description,"" +Task_Description_Size[i]);
curChildMap.put(Task_Status,"" +Task_Status_Size[i]);
curChildMap.put(Task_IsActive,"" +Task_IsActive_Size[i]);
childData.add(children);
}
mAdapter =new Myexpandable_ListAdapter(
this,
groupData,
R.layout.list_group,
new String[] { Task_Title },
new int[] {R.id.lblListHeader },
childData,
R.layout.child_item,
new String[] { Task_Start_Date , Task_CompletionDate , Task_CompletionTime , Task_Description , Task_Status , Task_IsActive},
new int[] { R.id.TextView_Projectdetails , R.id.textOne , R.id.textTwo , R.id.textThree , R.id.textFour, R.id.textFive}
);
expListView.setAdapter(mAdapter);
}
expListView.setOnGroupClickListener(new OnGroupClickListener()
{
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id)
{
return false;
}
});
//editTextTaskName = (EditText)findViewById(R.id.Task_Name);
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
dtrProjectNAmeSize.length + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
dtrProjectNAmeSize.length + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
btnNewProject = (Button)findViewById(R.id.buttonmyproject_NewProject);
btnNewProject.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(My_Project.this , Add_Project.class);
startActivity(i);
finish();
}
});
}
private class Myexpandable_ListAdapter extends BaseExpandableListAdapter {
public Myexpandable_ListAdapter(Context context,
List<? extends Map<String, ?>> groupData,
int expandedGroupLayout,
String[] groupFrom, int[] groupTo,
List<? extends List<? extends Map<String, ?>>> childData,
int childLayout, String[] childFrom,
int[] childTo) {
super();
// TODO Auto-generated constructor stub
}
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
Context context;
public Object getChild(int groupPosition, int childPosition)
{
//this.get(groupPosition).getChildren().get(childPosition);
String strchildPosition = this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosition);
System.out.println("Child Position =" + strchildPosition);
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosition);
}
//Call when child row clicked
#Override
public long getChildId(int groupPosition, int childPosition)
{
/****** When Child row clicked then this function call *******/
//Log.i("Noise", "parent == "+groupPosition+"= child : =="+childPosition);
if( ChildClickStatus!=childPosition)
{
ChildClickStatus = childPosition;
Toast.makeText(getApplicationContext(), "Parent :"+groupPosition + " Child :"+childPosition ,
Toast.LENGTH_LONG).show();
}
return childPosition;
}
public View getChildView1(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
return parent;
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_item, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.TextView_Projectdetails);
TextView txtOneListChild = (TextView)convertView.findViewById(R.id.textOne);
TextView txtTwoListChild = (TextView)convertView.findViewById(R.id.textTwo);
TextView txtThreeListChild = (TextView)convertView.findViewById(R.id.textThree);
TextView txtFourListChild = (TextView)convertView.findViewById(R.id.textFour);
TextView txtFiveListChild = (TextView)convertView.findViewById(R.id.textFive);
txtListChild.setText(childText);
txtOneListChild.setText(childText);
txtTwoListChild.setText(childText);
txtThreeListChild.setText(childText);
txtFourListChild.setText(childText);
txtFiveListChild.setText(childText);
Button btnAssgnTask = (Button)convertView.findViewById(R.id.button_EditedTask);
btnAssgnTask.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Button btnViewTask = (Button)convertView.findViewById(R.id.buttonViewTask);
btnViewTask.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return ParentClickStatus;
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
Thanks in Advance.
your problem is here:
#Override
public int getGroupCount() {
return ParentClickStatus;
}
as you extends BaseExpandableListAdapter, getChildrenCount and getGroupCount handle number of your view and as ParentClickStatus is equal to -1 getGroupView never called
solution
you need return group row number that you have, so change your code with:
#Override
public int getGroupCount() {
return _listDataHeader.size();
}
for an example you can see section 15. Implementing an expandable ListView
UPDATE
you need change your constructor too:
public Myexpandable_ListAdapter(Context context,
List<? extends Map<String, ?>> groupData,
int expandedGroupLayout,
String[] groupFrom, int[] groupTo,
List<? extends List<? extends Map<String, ?>>> childData,
int childLayout, String[] childFrom,
int[] childTo) {
super();
// TODO Auto-generated constructor stub
}
you passed your data to here, but you don't set to any local data, for example you tried to use _listDataChild in getChild method, but you not initialize that at all, you need initialize that in constructor with your data that passed here.
i am getting a null pointer exception
public class ApisimpleActivity extends ExpandableListActivity {
ExpandableListAdapter mAdapter;
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set up our adapter
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int i=getExpandableListView().getChildCount();
int k=0;
//Toast.makeText(ApisimpleActivity.this, "hello"+i, Toast.LENGTH_LONG).show();
for(int j=0;j<i;j++)
{
View v=(View)getExpandableListView().getChildAt(i);
if(v==null)
//if(v.findViewById(R.id.chk)==null)
k++;
}
Toast.makeText(ApisimpleActivity.this, k+"",Toast.LENGTH_LONG).show();
} });
mAdapter = new MyExpandableListAdapter();
setListAdapter(mAdapter);
registerForContextMenu(getExpandableListView());
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Sample menu");
menu.add(0, 0, 0, "Hello World");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
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;
}
/**
* 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 class MyExpandableListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for groups[i].
private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
private String[][] children = {
{ "Arnold", "Barry", "Chuck", "David" },
{ "Ace", "Bandit", "Cha-Cha", "Deuce" },
{ "Fluffy", "Snuggles" },
{ "Goldy", "Bubbles" }
};
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView textView = new TextView(ApisimpleActivity.this);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(36, 0, 0, 0);
return textView;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
public int getGroupCount() {
return groups.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
View v;
v=convertView;
if(v==null)
{LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.group, null);
}
return v;}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
}
on btn.setOnClickListener when i call the function
getExpandableListView().getChildAt(i) all the views are null.can any one please help why all the view are showing null i need to get the view and check on each weather checkbox is checked or not.please some one help
i think you want getExpandableListView().getChildAt(j) (j instead of i, for j is the size of the list, therefor not in it)
Edit: not really important here, but using k+"" to print an integer is slow. Use String.valueOf(k) instead