How to get checked items data and count from ExpandableListView - java

I want to show the count and all the data when clicked. But so far in testing using click on the Parent item
I tried increment checkedCount when check. But it show only checked count in in one group child elemements.
My Adapter:
public class RubExpandAdapter extends BaseExpandableListAdapter {
private Context context;
private List<RubricsModel> rubrics;
private List<VacancyModel> listForRubricListView;
private List<VacancyModel> listForSubRubricListView;
private HashMap<Integer, boolean[]> mChildCheckStates;
public static int rubricPage = 1;
public static int rubricId;
public static int subRubricPage = 1;
public static int subRubricId;
int checkedCount;
FragmentManager fm;
public RubExpandAdapter(Context context, ArrayList<RubricsModel> rubrics, FragmentManager fragmentManager) {
this.context = context;
this.rubrics = rubrics;
fm = fragmentManager;
listForRubricListView = new ArrayList<>();
mChildCheckStates = new HashMap<>();
}
#Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<SubRubricsModel> chList = rubrics.get(groupPosition)
.getItems();
return chList.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final int mGroupPosition = groupPosition;
final int mChildPosition = childPosition;
SubRubricsModel subRubricsModel = (SubRubricsModel) getChild(groupPosition,
childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.rubrics_child_row_layout, null);
}
final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
TextView tv = (TextView) convertView.findViewById(R.id.tvsubrubricsfr);
tv.setText(subRubricsModel.getSubRubricName());
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkBox.setChecked(!checkBox.isChecked());
}
});
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
getChecked[mChildPosition] = isChecked;
mChildCheckStates.put(mGroupPosition, getChecked);
} else {
boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
getChecked[mChildPosition] = isChecked;
mChildCheckStates.put(mGroupPosition, getChecked);
}
for (int i = 0; i < mChildCheckStates.size(); i++){
if (mChildCheckStates.containsValue(true)){
checkedCount ++;
}
}
}
});
if (mChildCheckStates.containsKey(mGroupPosition)) {
boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
checkBox.setChecked(getChecked[mChildPosition]);
} else {
boolean getChecked[] = new boolean[getChildrenCount(mGroupPosition)];
mChildCheckStates.put(mGroupPosition, getChecked);
checkBox.setChecked(false);
}
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
ArrayList<SubRubricsModel> chList = rubrics.get(groupPosition)
.getItems();
return chList.size();
}
#Override
public Object getGroup(int groupPosition) {
return rubrics.get(groupPosition);
}
#Override
public int getGroupCount() {
return rubrics.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
final RubricsModel rubricsModel = (RubricsModel) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.rubrics_group_row_layout, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tvrubricsfr);
tv.setText(rubricsModel.getRubricName());
ImageView indicator = (ImageView) convertView.findViewById(R.id.ivGroupIndicator);
if (isExpanded) {
indicator.setImageResource(R.drawable.arrow_down);
} else {
indicator.setImageResource(R.drawable.arrow_right);
}
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// RubFragment.searchByRubrics = true;
// rubricId = rubricsModel.getRubricID();
// searchByRubric(String.valueOf(rubricId));
Toast.makeText(context, String.valueOf(mChildCheckStates.size()), Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
how to do, tell me, pls?

Related

Child click listener is not working in Expandable list view

I have Expandable list view in my activity but problem is when I am clicking child element it is not sowing toast message.Below is my MainActivity class and adapter class:
MainActivity.class
public class MainActivity extends AppCompatActivity {
ExpandableListView expand;
List<String> category = new ArrayList<>();
Map<String,List<String>> child = new HashMap<>();
MyExListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
expand = findViewById(R.id.expand);
List<String> mobiles = new ArrayList<>();
List<String> jewel = new ArrayList<>();
category.add("Mobile Covers");
category.add("T-Shirts");
category.add("Notebooks");
category.add("Jewellery");
category.add("Honey");
category.add("Corporate Gifts");
category.add("Packaging");
category.add("Deals");
mobiles.add("Apple");
mobiles.add("Samsung");
mobiles.add("OnePlus");
mobiles.add("Vivo");
mobiles.add("Oppo");
mobiles.add("Redmi");
jewel.add("Designer");
jewel.add("Classic");
child.put(category.get(0),mobiles);
child.put(category.get(1),jewel);
adapter = new MyExListAdapter(this,category,child);
expand.setAdapter(adapter);
expand.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(getApplicationContext(),"" +child.get(category.get(groupPosition)).get(childPosition),Toast.LENGTH_SHORT).show();
return false;
}
});
}
MyExListAdapter.java
package com.app.aamkuapp.Adapters;
public class MyExListAdapter extends BaseExpandableListAdapter {
Context context;
List<String> category;
Map<String ,List<String>> child;
public MyExListAdapter(Context context, List<String> category, Map<String, List<String>> child) {
this.context = context;
this.category = category;
this.child = child;
}
#Override
public int getGroupCount() {
return category.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return child.get(category.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return category.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return child.get(category.get(groupPosition)).get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String cat = (String) getGroup(groupPosition);
if(convertView == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_parent,null);
}
TextView txt1 = convertView.findViewById(R.id.textParent);
txt1.setText(cat);
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String chi = (String) getChild(groupPosition,childPosition);
if(convertView == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_child,null);
}
TextView txt2 = convertView.findViewById(R.id.textChild);
txt2.setText(chi);
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
Someone please let me know why Toast is not showing on clicking child element.Any help would be appreciated.
THANKS
Change your adapter code from this..
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
to this..
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
Hope this helps.

Android - setting a listener on items in each row of ExpandableListView

On each row i have two EditTexts.when i change the first one , i want the other to be changed as well(if needed ,for now just setting there '25' for testing).I am using a viewholder pattern,and setting on each of the first edittexts a TextChangedListener,expecting the second to be changed as well .problem is , whenever i change ANY of the first edittexts on ANY of the rows ,only the edittext on the last row is changed. here is the code of the adapter(listener is in getgroupview):
public class CustomExpandableListAdapterNewWorkout extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
private ChildViewHolder childViewHolder;
private GroupViewHolder groupViewHolder;
public CustomExpandableListAdapterNewWorkout(Context context, List<String> expandableListTitle,
HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
#Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
#Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
#Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
String categoryName= (String)getGroup(listPosition);
if (convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item_exercises_exercise, null);
childViewHolder = new ChildViewHolder();
childViewHolder.mChildTitle = (TextView) convertView.findViewById(R.id.expandedListItem);
childViewHolder.mChildImage = (ImageView) convertView.findViewById(R.id.ImgExercisePic);
convertView.setTag(childViewHolder);
}
else
{
childViewHolder = (ChildViewHolder) convertView.getTag();
}
childViewHolder.mChildTitle.setText(expandedListText);
childViewHolder.mChildTitle.setTextAppearance(context,R.style.TitleStyle);
return convertView;
}
#Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}
#Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
#Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
#Override
public long getGroupId(int listPosition) {
return listPosition;
}
#Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_group_new_workout_exercise, null);
groupViewHolder = new GroupViewHolder();
groupViewHolder.mGroupTitle = (TextView) convertView.findViewById(R.id.exerciseName);
groupViewHolder.mMinSets = (EditText) convertView.findViewById(R.id.edtMinimumSets);
groupViewHolder.mMaxSets = (EditText) convertView.findViewById(R.id.edtMaxSets);
groupViewHolder.mMinSets.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
groupViewHolder.mMaxSets.setText("25");
}
});
convertView.setTag(groupViewHolder);
}
else
{
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
groupViewHolder.mGroupTitle.setText(listTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
public final class GroupViewHolder {
TextView mGroupTitle;
EditText mMinSets;
EditText mMaxSets;
}
public final class ChildViewHolder {
TextView mChildTitle;
ImageView mChildImage;
}
}
probably there is something basic i don't understand about adapters and viewholders, and i would like to know the correct method to address it.
ok my bad.
should have declared "GroupViewHolder groupViewHolder" inside the getGroupView method,this way a new one with a new listener is created each time and affects his corresponding row

GridView Within ExpandableListView With Database

Good day!
I'm a newbie at android/java programming and I'm trying to create, kinda like, a history function to my app. I'm using an expandableListView with a gridView (or listView; I don't really know what else to use aside from these two) inside to show said history. All the data comes from sqLite database which has 3 tables named Requests, Properties & ReqLine all linked together.
The format I'm trying to create is something like this (Sorry, cant post images yet):
HEADER: TextView ----- data from db 1st table
CHILD: TextView ----- data from 2nd GRID: 1)item from DB 3rd table 2)item
from DB 3rd table
I was able to populate the header and child portions but I'm having trouble with the child gridView repeating data on every child. I know it's with the way I'm passing the ArrayList to the GridView Adapter but I don't the correct way of doing it or how to implement it.
I tried a bunch of ways including adding another arraylist to my header class model but I don't know how I should get the childcount for it in my adapter.
I've seen lots on posts here in SO but none of them seems to have the solution to my problem. Can anyone help me with this? Thanks in advance! Suggestions on revisions are welcome as well.
Here's my code for the history fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.history_tab_layout, container, false);
openHelper = new DatabaseHelper(getActivity());
myDb = openHelper.getReadableDatabase();
TextView _number = (TextView) rootView.findViewById(R.id.lblNumber);
TextView _date = (TextView) rootView.findViewById(R.id.lblHistoryDate);
TextView _transtype = (TextView) rootView.findViewById(R.id.lblHistoryTransType);
TextView _amount = (TextView) rootView.findViewById(R.id.lblHistoryAmount);
Cursor mCursor = null;
Cursor dataCursor = null;
Cursor itemCursor = null;
String Query = "SELECT * FROM Requests";
mCursor = myDb.rawQuery(Query, null);
transHistory = new ArrayList<HistoryHeader>();
gridHistory = new ArrayList<GridItems>();
int count = 1;
if(mCursor.getCount()!=0) {
while (mCursor.moveToNext()) {
String reqDate = mCursor.getString(mCursor.getColumnIndex("RequestDate"));
String reqTransType = mCursor.getString(mCursor.getColumnIndex("TransType"));
String reqTotalAmt = mCursor.getString(mCursor.getColumnIndex("AmtTotal"));
String reqPCode = mCursor.getString(mCursor.getColumnIndex("Property"));
String BaseId = mCursor.getString(0);
HistoryHeader history = new HistoryHeader(count, reqDate, reqTransType, reqTotalAmt);
String Query2 = "SELECT * FROM Projects WHERE PrjCode = '"+ mCursor.getString(3) +"'";
dataCursor = myDb.rawQuery(Query2, null);
if (dataCursor.getCount()!=0){
while (dataCursor.moveToNext()){
String reqPName = dataCursor.getString(dataCursor.getColumnIndex("PrjName"));
history.setItemList(createItems(reqPCode, reqPName, 1));
}
}
String Query3 = "SELECT * FROM ReqLine WHERE Base_Id = "+ BaseId;
itemCursor = myDb.rawQuery(Query3, null);
if (itemCursor.getCount()!=0) {
while (itemCursor.moveToNext()) {
String reqPurpose = itemCursor.getString(itemCursor.getColumnIndex("Purpose"));
String reqAmount = itemCursor.getString(itemCursor.getColumnIndex("AmtLine"));
Integer reqNum = itemCursor.getInt(itemCursor.getColumnIndex("Linenum"));
GridItems test = new GridItems(reqNum, reqPurpose, reqAmount);
gridHistory.add(test);
}
}
transHistory.add(history);
count++;
}
}
final ExpandableListView _Content = (ExpandableListView) rootView.findViewById(R.id.historyList);
_Content.setIndicatorBounds(5,5);
HistoryAdapter exAdpt = new HistoryAdapter(getActivity(), transHistory, gridHistory);
_Content.setIndicatorBounds(0,20);
_Content.setAdapter(exAdpt);
return rootView;
}
private List<HistoryDetail> createItems(String _strPropertyCode, String _strPropertyName, int num) {
List<HistoryDetail> result = new ArrayList<HistoryDetail>();
for (int i=0; i < num; i++) {
HistoryDetail item = new HistoryDetail(i, _strPropertyCode, _strPropertyName);
result.add(item);
}
return result;
}
And the Code for my HistoryAdapter.java
public class HistoryAdapter extends BaseExpandableListAdapter {
private Context context;
private List<HistoryHeader> _listDataHeader;
private ArrayList<GridItems> _listGridItems;
public HistoryAdapter(Context context, List<HistoryHeader> _listDataHeader, ArrayList<GridItems> _listGridItems) {
this.context = context;
this._listDataHeader = _listDataHeader;
this._listGridItems = _listGridItems;
}
#Override
public int getGroupCount() {
return _listDataHeader.size();
}
#Override
public int getChildrenCount(int groupPosition) {
/*Integer size = _listDataHeader.get(groupPosition).getItemList().size();
return size;*/
return 1;
}
#Override
public Object getGroup(int groupPosition) {
return _listDataHeader.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return _listDataHeader.get(groupPosition).getItemList().get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return _listDataHeader.get(groupPosition).hashCode();
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return _listDataHeader.get(groupPosition).getItemList().get(childPosition).hashCode();
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.history_list_group, null);
}
TextView groupNum = (TextView) v.findViewById(R.id.historyNumber);
TextView groupDate = (TextView) v.findViewById(R.id.historyDate);
TextView groupTransType = (TextView) v.findViewById(R.id.historyTransType);
TextView groupAmount = (TextView) v.findViewById(R.id.historyTotalAmt);
HistoryHeader header = _listDataHeader.get(groupPosition);
groupNum.setText(String.valueOf(header.getId()));
groupDate.setText(header.getDate());
groupTransType.setText(header.getTransType());
groupAmount.setText(header.getTotalAmt());
return v;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup viewGroup) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.history_list_child, null);
}
TextView itemPropCode = (TextView) v.findViewById(R.id.historyPropertyCode);
TextView itemPropName = (TextView) v.findViewById(R.id.historyPropertyName);
GridView itemGrid = (GridView) v.findViewById(R.id.historyItemList);
ItemGridAdapter adapter = new ItemGridAdapter(context,_listGridItems);
itemGrid.setAdapter(adapter);
HistoryDetail detail = _listDataHeader.get(groupPosition).getItemList().get(childPosition);
itemPropCode.setText(detail.getPropertyCode());
itemPropName.setText(detail.getPropertyName());
return v;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
Then the ItemGridAdapter.java
public class ItemGridAdapter extends BaseAdapter {
Context context;
ArrayList<GridItems> itemList;
public ItemGridAdapter(Context context, ArrayList<GridItems> itemList) {
this.context = context;
this.itemList = itemList;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return itemList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.history_grid_layout, null);
}
TextView _rowPurpose = (TextView) convertView.findViewById(R.id.rowPurpose);
TextView _rowAmount = (TextView) convertView.findViewById(R.id.rowAmount);
GridItems gridItems = itemList.get(position);
_rowPurpose.setText(gridItems.getPurpose());
_rowAmount.setText(gridItems.getAmount());
return convertView;
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
// Return true for clickable, false for not
return false;
}
Model Classes
public class HistoryHeader implements Serializable {
private long id;
private String _strDate;
private String _strTransType;
private String _strTotalAmt;
private List<HistoryDetail> itemDetails = new ArrayList<HistoryDetail>();
private List<GridItems> itemGrid = new ArrayList<>();
public HistoryHeader(long id, String _strDate, String _strTransType, String _strTotalAmt) {
this.id = id;
this._strDate = _strDate;
this._strTransType = _strTransType;
this._strTotalAmt = _strTotalAmt;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getDate() {
return _strDate;
}
public void setDate(String _strDate) {
this._strDate = _strDate;
}
public String getTransType() {
return _strTransType;
}
public void setTransType(String _strTransType) {
this._strTransType = _strTransType;
}
public String getTotalAmt() {
return _strTotalAmt;
}
public void setTotalAmt(String _strTotalAmt) {
this._strTotalAmt = _strTotalAmt;
}
public List<HistoryDetail> getItemList() {
return itemDetails;
}
public void setItemList(List<HistoryDetail> itemDetails) {
this.itemDetails = itemDetails;
}
public List<GridItems> getItemGrid(){ return itemGrid; }
public void setItemGrid(List<GridItems> itemGrid) { this.itemGrid = itemGrid; }
public class GridItems {
private long id;
private String _strPurpose, _strAmount;
public GridItems(long id, String _strPurpose, String _strAmount) {
this.id = id;
this._strPurpose = _strPurpose;
this._strAmount = _strAmount;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getPurpose() {
return _strPurpose;
}
public void setPurpose(String _strPurpose) {
this._strPurpose = _strPurpose;
}
public String getAmount() {
return _strAmount;
}
public void setAmount(String _strAmount) {
this._strAmount = _strAmount;
}
Try this:
Cursor mCursor = null;
Cursor dataCursor = null;
Cursor itemCursor = null;
String Query = "SELECT * FROM Requests";
mCursor = myDb.rawQuery(Query, null);
transHistory = new ArrayList<HistoryHeader>();
//allGridHistory = new ArrayList<ArrayList<GridItems>>(); // Changed2
//gridHistory = new ArrayList<GridItems>();
int count = 1;
if(mCursor.getCount()!=0) {
while (mCursor.moveToNext()) {
String reqDate = mCursor.getString(mCursor.getColumnIndex("RequestDate"));
String reqTransType = mCursor.getString(mCursor.getColumnIndex("TransType"));
String reqTotalAmt = mCursor.getString(mCursor.getColumnIndex("AmtTotal"));
String reqPCode = mCursor.getString(mCursor.getColumnIndex("Property"));
String BaseId = mCursor.getString(0);
HistoryHeader history = new HistoryHeader(count, reqDate, reqTransType, reqTotalAmt);
String Query2 = "SELECT * FROM Projects WHERE PrjCode = '"+ mCursor.getString(3) +"'";
dataCursor = myDb.rawQuery(Query2, null);
if (dataCursor.getCount()!=0){
while (dataCursor.moveToNext()){
String reqPName = dataCursor.getString(dataCursor.getColumnIndex("PrjName"));
history.setItemList(createItems(reqPCode, reqPName, 1));
}
}
String Query3 = "SELECT * FROM ReqLine WHERE Base_Id = "+ BaseId;
itemCursor = myDb.rawQuery(Query3, null);
if (itemCursor.getCount()!=0) {
gridHistory = new ArrayList<GridItems>(); // Added
while (itemCursor.moveToNext()) {
String reqPurpose = itemCursor.getString(itemCursor.getColumnIndex("Purpose"));
String reqAmount = itemCursor.getString(itemCursor.getColumnIndex("AmtLine"));
Integer reqNum = itemCursor.getInt(itemCursor.getColumnIndex("Linenum"));
GridItems test = new GridItems(reqNum, reqPurpose, reqAmount);
gridHistory.add(test);
}
history.setItemGrid(gridHistory); // Changed2
}
transHistory.add(history);
count++;
}
}
final ExpandableListView _Content = (ExpandableListView) rootView.findViewById(R.id.historyList);
_Content.setIndicatorBounds(5,5);
HistoryAdapter exAdpt = new HistoryAdapter(getActivity(), transHistory); // Changed2
The adapter:
public class HistoryAdapter extends BaseExpandableListAdapter {
private Context context;
private List<HistoryHeader> _listDataHeader;
//private ArrayList<GridItems> _listGridItems;
public HistoryAdapter(Context context, List<HistoryHeader> _listDataHeader) {
this.context = context;
this._listDataHeader = _listDataHeader;
//this._listGridItems = _listGridItems;
}
#Override
public int getGroupCount() {
return _listDataHeader.size();
}
#Override
public int getChildrenCount(int groupPosition) {
/*Integer size = _listDataHeader.get(groupPosition).getItemList().size();
return size;*/
return 1;
}
#Override
public HistoryHeader getGroup(int groupPosition) {
return _listDataHeader.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return _listDataHeader.get(groupPosition).getItemList().get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return _listDataHeader.get(groupPosition).hashCode();
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return _listDataHeader.get(groupPosition).getItemList().get(childPosition).hashCode();
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.history_list_group, null);
}
TextView groupNum = (TextView) v.findViewById(R.id.historyNumber);
TextView groupDate = (TextView) v.findViewById(R.id.historyDate);
TextView groupTransType = (TextView) v.findViewById(R.id.historyTransType);
TextView groupAmount = (TextView) v.findViewById(R.id.historyTotalAmt);
HistoryHeader header = _listDataHeader.get(groupPosition);
groupNum.setText(String.valueOf(header.getId()));
groupDate.setText(header.getDate());
groupTransType.setText(header.getTransType());
groupAmount.setText(header.getTotalAmt());
return v;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup viewGroup) {
View v = convertView;
// As for using grid view, there is only one child, no suitable view for reuse/recycle.
//if (v == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.history_list_child, null);
//}
TextView itemPropCode = (TextView) v.findViewById(R.id.historyPropertyCode);
TextView itemPropName = (TextView) v.findViewById(R.id.historyPropertyName);
GridView itemGrid = (GridView) v.findViewById(R.id.historyItemList);
// Get the child list of this group/header.
List<GridItems> history_list_child = getGroup(groupPosition).getItemGrid();
// As for using grid view, onChildClickListener cannot be used. You may need to pass the group/header object
// to grid view adapter and do onClick inside getView. First try your original way to display data correctly.
ItemGridAdapter adapter = new ItemGridAdapter(context, history_list_child);
itemGrid.setAdapter(adapter);
HistoryDetail detail = _listDataHeader.get(groupPosition).getItemList().get(childPosition);
itemPropCode.setText(detail.getPropertyCode());
itemPropName.setText(detail.getPropertyName());
return v;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Hope that helps!

How to get id only those which I have checked in android?

if (v.getId() == R.id.contact_btnNext) {
StringBuilder id = new StringBuilder();
String prefix = "";
for (Datamodel datamodel : mAdapter.arrSelectedModel ) {
id.append(prefix).append(datamodel.getId());
prefix = ",";
}
Using this code I am getting value of all ListView it's which is checked and unchecked in id:
public class AdapterContactAddfriend extends BaseAdapter {
private Context context;
private ArrayList<Datamodel> arrModel;
public ArrayList<Datamodel> arrSelectedModel;
public ArrayList<Boolean> arrCheckboxState;
ViewHolder holder;
ImageLoader imageloader;
public AdapterContactAddfriend(Context context,
ArrayList<Datamodel> arrModel) {
this.context = context;
this.arrModel = arrModel;
arrCheckboxState = new ArrayList<Boolean>();
arrSelectedModel = new ArrayList<Datamodel>();
imageloader = new ImageLoader(context);
for (int i = 0; i < arrModel.size(); i++) {
arrCheckboxState.add(false);
}
}
#Override
public int getCount() {
return arrModel.size();
}
#Override
public Object getItem(int position) {
return arrModel.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater
.inflate(R.layout.row_contactaddfriend, null);
holder.checkBox = (CheckBox) convertView
.findViewById(R.id.rowcontact_checkbox);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtContactName.setText(arrModel.get(position).contactName);
holder.checkBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int position = (Integer) buttonView.getTag();
if (isChecked) {
arrCheckboxState.set(position, true);
arrModel.get(position).setChecked(true);
arrSelectedModel.add(arrModel.get(position));
} else {
arrModel.get(position).setChecked(false);
arrCheckboxState.set(position, false);
arrSelectedModel.remove(arrModel.get(position));
}
}
});
return convertView;
}
}
This is my adapter and I have taken boolean value public ArrayList<Boolean> arrCheckboxState for check and unchecked value but I am unable to get value of id which is checked where am doing mistake please tell me because I am getting value of id of all list-view item but I have to get only those id which is selected.
Remove this line..because your are not setting position as tag..as position is already there in parameters why are you getting it from tag ?
int position = (Integer) buttonView.getTag();
Use it like this
holder.checkBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(position<arrCheckboxState.size()){
if (isChecked) {
arrCheckboxState.set(position, true);
arrModel.get(position).setChecked(true);
arrSelectedModel.add(arrModel.get(position));
} else {
arrModel.get(position).setChecked(false);
arrCheckboxState.set(position, false);
arrSelectedModel.remove(arrModel.get(position));
}
}
}
});

ExpandableListView.getPackedPositionGroup always returns 0

I have an ExpandableListView with a contextmenu. It doesn't matter how many group items I have, it always returns 0.
My code:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
int group = ExpandableListView.getPackedPositionGroup((int) info.packedPosition);
int type = ExpandableListView.getPackedPositionType((int) info.packedPosition);
int child = ExpandableListView.getPackedPositionChild((int) info.packedPosition);
Log.d("Value ConTEXT: " + String.valueOf(group), String.valueOf(child) + " GROUP COUNT " + getExpandableListAdapter().getGroupCount());
menu.setHeaderTitle(((Person) dAdapter.getGroup(group)).getName());
// menu.add(0, 0, 0, "Geschiedenis verwijderen");
menu.add(1, 1, 1, "Verwijderen uit lijst");
}
And my custom base list adapter:
public class DrinkActionAdapter extends BaseExpandableListAdapter {
Activity context;
ArrayList<Person> personsGroup;
ArrayList<Person> personsChild;
public DrinkActionAdapter(Activity context, ArrayList<Person> personsGroup,
ArrayList<Person> personsChild) {
this.context = context;
this.personsGroup = personsGroup;
this.personsChild = personsChild;
}
public void addPerson(Person p) {
personsGroup.add(p);
personsChild.add(p);
for (Person pp : personsGroup) {
Log.d(pp.getName(), pp.getName());
}
}
public Object getChild(int groupPosition, int childPosition) {
Log.d("GetChild" + String.valueOf(groupPosition), String.valueOf(childPosition));
return personsGroup.get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
Log.d("GetChildID" + String.valueOf(groupPosition), String.valueOf(childPosition));
return childPosition;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
Person p = personsChild.get(groupPosition);
View v = convertView;
Log.d(p.getName(), p.getName() + "CHILD");
if (v == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = mInflater.inflate(R.layout.drink_action_child, null);
}
if (personsChild.size() != 0) {
TextView lbFavoriteDrink = (TextView) v.findViewById(R.id.drink_action_child_lb_favorite_drink);
if (p.getFavoriteDrink() != null) {
lbFavoriteDrink.setText(p.getFavoriteDrink().getName());
} else {
lbFavoriteDrink.setText("Nog niks gedronken.");
}
TextView lbCost = (TextView) v.findViewById(R.id.drink_action_child_lb_cost);
lbCost.setText(String.valueOf(p.getTotalMoneySpent()));
TextView lbAmount = (TextView) v.findViewById(R.id.drink_action_child_lb_amount);
lbAmount.setText(String.valueOf(p.getTotalAmountOfDrinks()));
TextView lbKcal = (TextView) v.findViewById(R.id.drink_action_child_lb_kcal);
lbKcal.setText(String.valueOf(p.getTotalKcal()));
TextView lbPunishment = (TextView) v.findViewById(R.id.drink_action_child_lb_boete);
lbPunishment.setText(p.getPunishment());
TextView lbWeightPutOn = (TextView) v.findViewById(R.id.drink_action_child_lb_weight);
lbWeightPutOn.setText(String.valueOf(p.getWeightPutOn()));
TextView lbLiters = (TextView) v.findViewById(R.id.drink_action_child_lb_liter);
lbLiters.setText(String.valueOf(p.getAmountOfLiters()));
TextView lbTimeSinceLastDrink = (TextView) v.findViewById(R.id.drink_action_child_lb_time_since_last_drink);
lbTimeSinceLastDrink.setText(String.valueOf(p.getTimeSinceLast()[2]) + ":" + String.valueOf(p.getTimeSinceLast()[1]) + ":" + String.valueOf(p.getTimeSinceLast()[0]));
} else {
// WHAT TO DO IF NO PROFILES
}
return v;
}
public int getChildrenCount(int groupPosition) {
Log.d("GetChildrenCount" + String.valueOf(groupPosition), String.valueOf(groupPosition));
return 1;
// return persons.size();
}
public Object getGroup(int groupPosition) {
Log.d(String.valueOf(groupPosition), String.valueOf(groupPosition) + personsGroup.get(groupPosition).getName());
return personsGroup.get(groupPosition);
}
public int getGroupCount() {
return personsGroup.size();
}
public long getGroupId(int groupPosition) {
Log.d(String.valueOf(groupPosition) + "GetGroupId", String.valueOf(groupPosition));
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Person p = personsGroup.get(groupPosition);
View v = convertView;
Log.d(groupPosition + " " + p.getName(), p.getName() + "GROUP");
if (v == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = mInflater.inflate(R.layout.drink_action_item, null);
}
if (personsGroup.size() != 0) {
TextView lbName = (TextView) v.findViewById(R.id.drink_action_lb_name);
TextView lbAlcohol = (TextView) v.findViewById(R.id.drink_action_info_current_alcohol);
ImageView btnDrink = (ImageView) v.findViewById(R.id.drink_action_item_btn_drink);
btnDrink.setTag(p);
lbName.setText(p.getName());
lbAlcohol.setText(String.valueOf(p.getCurrentAlcoholLevel()));
} else {
// WHAT TO DO IF NO PROFILES
}
return v;
}
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
Whenever I longclick on an item, it always selects the first one.
Try to register to the contextMenu in the getChildView() method just before return v; with this code ((Activity) context).registerForContextMenu(v);

Categories

Resources