I have implemented a Expandable List View,in Android , you can see in the screenshot , it is working fine , and in Expandable List i have multiple child with delete option in group as you can see in the screen shot, i want to delete a particular child which user has selected to delete , I am not able to delete the child form Expandable List , i will post my code , please anyone guide me
public class PendingFragment extends Fragment {
private AnimatedExpandableListView listView;
private PendingAdapter adapter;
CoordinatorLayout coordinatorLayout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_pending, container, false);
coordinatorLayout = (CoordinatorLayout)view.findViewById(R.id.pending_frag_coordinatorLayout);
List<GroupItem> items = new ArrayList<GroupItem>();
// Populate our list with groups and it's children
for(int i = 1; i < 10; i++) {
GroupItem item = new GroupItem();
item.title = "GroupItem " + i;
for(int j = 0; j < i; j++) {
ChildItem child = new ChildItem();
child.title = "ChildItem " + j;
item.items.add(child);
}
items.add(item);
}
adapter = new PendingAdapter(getActivity());
adapter.setData(items);
listView = (AnimatedExpandableListView)view.findViewById(R.id.pending_explistView);
listView.setAdapter(adapter);
return view;
}
}
My Listview Adapter
public class PendingAdapter extends AnimatedExpandableListView.AnimatedExpandableListAdapter {
private LayoutInflater inflater;
private List<GroupItem> items;
ListPopupWindow listPopupWindow;
String[] products = {"Delete"};
Context contexts;
public PendingAdapter(Context context) {
inflater = LayoutInflater.from(context);
this.contexts = context;
}
public void setData(List<GroupItem> items) {
this.items = items;
}
private class ChildHolder {
TextView title;
ImageView option_menu;
}
private class GroupHolder {
TextView title;
}
#Override
public ChildItem getChild(int groupPosition, int childPosition) {
return items.get(groupPosition).items.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getRealChildView(final int groupPosition,final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder holder;
ChildItem item = getChild(groupPosition, childPosition);
Log.e("groupPosition=" + groupPosition, "childPosition=" + childPosition);
if (convertView == null) {
holder = new ChildHolder();
convertView = inflater.inflate(R.layout.expchildlistview, parent, false);
holder.title = (TextView) convertView.findViewById(R.id.txtListChild);
holder.option_menu = (ImageView) convertView.findViewById(R.id.checkin_option_menu);
convertView.setTag(holder);
} else {
holder = (ChildHolder) convertView.getTag();
}
holder.title.setText(item.title);
holder.option_menu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ShowListMenu(view,groupPosition, childPosition);
}
});
return convertView;
}
public void ShowListMenu(View v, final int childPosition, final int groupPosition){
listPopupWindow = new ListPopupWindow(contexts);
listPopupWindow.setAdapter(new ArrayAdapter(contexts, android.R.layout.simple_list_item_1, products));
listPopupWindow.setAnchorView(v);
if (Utils.device_width(contexts) >= 320 && Utils.device_width(contexts) < 480) {
//Log.v(">= 320 < 480=", ">= 320 < 480");
listPopupWindow.setWidth(150);
}else if (Utils.device_width(contexts) >= 480 && Utils.device_width(contexts) < 500) {
//Log.v(">= 480=", "< 500");
listPopupWindow.setWidth(150);
}else{
listPopupWindow.setWidth(200);
}
listPopupWindow.setModal(true);
listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.v("item.remove=", "" + items.get(groupPosition).items.get(childPosition));
items.get(groupPosition).items.remove(childPosition);
notifyDataSetChanged();
listPopupWindow.dismiss();
}
});
listPopupWindow.show();
}
#Override
public int getRealChildrenCount(int groupPosition) {
return items.get(groupPosition).items.size();
}
#Override
public GroupItem getGroup(int groupPosition) {
return items.get(groupPosition);
}
#Override
public int getGroupCount() {
return items.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHolder holder;
GroupItem item = getGroup(groupPosition);
if (convertView == null) {
holder = new GroupHolder();
convertView = inflater.inflate(R.layout.explist_group, parent, false);
holder.title = (TextView) convertView.findViewById(R.id.lblListHeader);
convertView.setTag(holder);
} else {
holder = (GroupHolder) convertView.getTag();
}
holder.title.setText(item.title);
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}
public class GroupItem {
public String title;
public List<ChildItem> items = new ArrayList<ChildItem>();
}
public class ChildItem {
public String title;
}
Cant see items.remove(child) on click on delete. I think you should try deleting the child from the list and than set the adapter with updated values. #Achin
Related
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 a GridView inside an ExpandableListView
I have a function that highlights an item when it is clicked, now I'm trying to implement a button that when pressed it will unselect all selected items, but it is unselecting only the last view clicked
public class GridAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Filhos> child;
public ArrayList<CadastraEscolas> escola;
private ArrayList<ArrayList<Filhos>> filhos = new ArrayList();
public GridAdapter(Context context, ArrayList<CadastraEscolas> groups, ArrayList<Filhos> childValues, int groupPosition) {
mContext = context;
child = childValues;
escola = groups;
posicaoEscola = groupPosition;
}
#Override
public int getCount() {
return child.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_item, null);
holder = new ViewHolder();
final TextView idAluno = (TextView) convertView.findViewById(R.id.idcrianca);
final TextView nomeAluno = (TextView) convertView.findViewById(R.id.name);
convertView.setTag(holder);
final View finalConvertView = convertView;
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (list.size() > 0) {
isChecked = filhos.get(posicaoEscola).get(position).isChecked();
if (!isChecked) {
selecao(true, position, nomeAluno, 0xFFFFFFFF, finalConvertView, View.VISIBLE);
} else {
selecao(false, position, nomeAluno, 0xFFD5672B, finalConvertView, View.GONE);
}
ex.findViewById(R.id.notificar).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
limpaSelecao(false, position, nomeAluno, 0xFFD5672B, parent, View.GONE);
}
});
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(child.get(position).getNome());
return convertView;
}
static class ViewHolder {
TextView text;
}
public void selecao(boolean check, int position, TextView nomeAluno, int color, View v, int visibility) {
filhos.get(posicaoEscola).get(position).setChecked(check);
nomeAluno.setTextColor(color);
v.findViewById(R.id.overlay).setVisibility(visibility);
v.findViewById(R.id.overlayText).setVisibility(visibility);
}
public void limpaSelecao(boolean check, int position, TextView nomeAluno, int color, ViewGroup v, int visibility) {
for (int x = 0; x < group.size(); x++) {
for (int j = 0; j < group.get(x).getalunos().size(); j++) {
if(filhos.get(x).get(j).isChecked()){
v.getChildAt(j).findViewById(R.id.loadingPanel).findViewById(R.id.overlay).setVisibility(View.GONE);
}
nomeAluno.setTextColor(color);
}
}
}
}
Layout:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/nomealuno_main"
android:layout_below="#+id/child">
<View
android:id="#+id/overlay"
android:layout_width="125dp"
android:layout_height="50dp"
android:background="#color/bgOverlay"
android:visibility="gone"/>
</RelativeLayout>
here is what is happening:
when I select all the items from A to B like that:
And hit clear, it will only remove the selected children from the last group that I clicked:
I need to remove them all when I click on Clear, all the children no matter in what group they are have to be removed.
ExpandableAdapter:
public class ExpandListTest extends BaseExpandableListAdapter {
public static final int CHOICE_MODE_MULTIPLE = AbsListView.CHOICE_MODE_MULTIPLE;
public static final int CHOICE_MODE_MULTIPLE_MODAL = AbsListView.CHOICE_MODE_MULTIPLE_MODAL;
/**
* No child could be selected
*/
public static final int CHOICE_MODE_NONE = AbsListView.CHOICE_MODE_NONE;
/**
* One single choice per group
*/
public static final int CHOICE_MODE_SINGLE_PER_GROUP = AbsListView.CHOICE_MODE_SINGLE;
/**
* One single choice for all the groups
*/
public static final int CHOICE_MODE_SINGLE_ABSOLUTE = 10001;
private Context context;
public static ArrayList<CadastraEscolas> groups;
private ArrayList<ArrayList<Filhos>> child = new ArrayList();
private HashMap<String, GPSEscolas> aMap = new HashMap<String, GPSEscolas>();
private HashMap<String, GPSEscolas> HashTask = new HashMap<String, GPSEscolas>();
public static ArrayList<Filhos> listchild;
private GridAdapter adapter;
private SecurePreferences Sessao;
public static CustomGridView gridView;
private SparseArray<SparseBooleanArray> checkedPositions;
private static final String LOG_TAG = ExpandListAdapter.class.getSimpleName();
public ExpandListTest(Context context, ArrayList<CadastraEscolas> groups, HashMap<String, GPSEscolas> data, SecurePreferences mSessao, HashMap<String, GPSEscolas> hashTask) {
this.context = context;
this.groups = groups;
this.aMap = data;
this.Sessao = mSessao;
checkedPositions = new SparseArray<SparseBooleanArray>();
child = new ArrayList();
if (groups != null) {
for (int i = 0; i < groups.size(); i++) {
child.add(i, groups.get(i).getalunos());
}
}
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return child.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, final ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.gridview, null);
}
listchild = new ArrayList<Filhos>();
for (int j = 0; j < groups.get(groupPosition).getalunos().size(); j++) {
listchild.add(child.get(groupPosition).get(j));
}
gridView = (CustomGridView) convertView.findViewById(R.id.GridView_toolbar);
gridView.setExpanded(true);
adapter = new GridAdapter(context, groups, listchild, child, groupPosition, aMap, HashTask, Sessao);
gridView.setAdapter(adapter);// Adapter
gridView.setChoiceMode(CustomGridView.CHOICE_MODE_MULTIPLE);
return convertView;
}
#Override
public int getChildrenCount(int nGroup) {
return 1;
}
#Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
#Override
public int getGroupCount() {
return groups.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
CadastraEscolas group = (CadastraEscolas) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_item, null);
}
ExpandableListView mExpandableListView = (ExpandableListView) parent;
mExpandableListView.expandGroup(groupPosition);
TextView tv = (TextView) convertView.findViewById(R.id.group_name);
tv.setText(group.getNome_fantasia());
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Android ViewGroups can contain any number of Views, but Views can only have one parent ViewGroup, if you try to add a View that already has a parent, you will get this exception (from ViewGroup.addViewInner()):
throw new IllegalStateException("The specified child already has a parent. " +
"You must call removeView() on the child's parent first.");
This means that the structure of your layouts is that of a tree (not a graph), and so you can use any tree traversal algorithm to iterate through every View of your layout.
The following is one of the possible algorithms, which is a post-order traversal.
It goes through the root element, takes the first child and makes a recursive call for the algorithm, then its second, third etc...when there are no children left to go through, it calls your deselect function for the node.
For the tree in the diagram, nodes will be unselected in this order:
A,C,E,D,B,H,I,G, F
public void onClickTheButton(View view) {
unselectall(R.layout.your_layout);
}
public void unselectAll(View view) {
if(view instanceof ViewGroup) {
for(int ii = 0 ; ii<(ViewGroup)view.getChildrenCount(); ii++) {
unselectAll((ViewGroup)view.getChildAt(ii));
}
}
unselect(view);
}
You can find many other ways to do it here: https://en.wikipedia.org/wiki/Tree_traversal
It is a must to learn how those algorithms work if you want to ease your programming experience.
change your getView() method as follows.
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_item, null);
holder = new ViewHolder();
final TextView idAluno = (TextView) convertView.findViewById(R.id.idcrianca);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(child.get(position).getNome());
final View finalConvertView = convertView;
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (list.size() > 0) {
isChecked = filhos.get(posicaoEscola).get(position).isChecked();
if (!isChecked) {
selecao(true, position, nomeAluno, 0xFFFFFFFF, finalConvertView, View.VISIBLE);
} else {
selecao(false, position, nomeAluno, 0xFFD5672B, finalConvertView, View.GONE);
}
ex.findViewById(R.id.notificar).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
limpaSelecao(false, position, nomeAluno, 0xFFD5672B, parent, View.GONE);
}
});
}
});
return convertView;
}
In my app I need to show images inside a expandableListView and it has to be a gridview of images, and these images can be selectable so I have an expandableListAdapter and inside of it I'm implementing a gridview adapter,
the images are showing fine as you can see in the picture 1, now what I'm trying to achieve is the picture 2, how can I pass the groupPosition and the childPosition to the gridAdapter and whe I click on the image make it highlight? :
Picture 2:
public class ExpandListAdapter extends BaseExpandableListAdapter {
public static final int CHOICE_MODE_MULTIPLE = AbsListView.CHOICE_MODE_MULTIPLE;
public static final int CHOICE_MODE_MULTIPLE_MODAL = AbsListView.CHOICE_MODE_MULTIPLE_MODAL;
/**
* No child could be selected
*/
public static final int CHOICE_MODE_NONE = AbsListView.CHOICE_MODE_NONE;
/**
* One single choice per group
*/
public static final int CHOICE_MODE_SINGLE_PER_GROUP = AbsListView.CHOICE_MODE_SINGLE;
/**
* One single choice for all the groups
*/
public static final int CHOICE_MODE_SINGLE_ABSOLUTE = 10001;
private Context context;
private ArrayList<Group> groups;
private ArrayList<ArrayList<Child>> child = new ArrayList();
private ArrayList<Child> listchild;
private GridAdapter adapter;
private CustomGridView gridView;
private SparseArray<SparseBooleanArray> checkedPositions;
private static final String LOG_TAG = ExpandListAdapter.class.getSimpleName();
private int choiceMode = CHOICE_MODE_MULTIPLE;
public ExpandListAdapter(Context context, ArrayList<Group> groups) {
this.context = context;
this.groups = groups;
checkedPositions = new SparseArray<SparseBooleanArray>();
child = new ArrayList();
for (int i = 0; i < groups.size(); i++) {
child.add(i, groups.get(i).getItems());
}
}
public ExpandListAdapter(Context context, ArrayList<Group> children, int choiceMode) {
this(context, children);
// For now the choice mode CHOICE_MODE_MULTIPLE_MODAL
// is not implemented
if (choiceMode == CHOICE_MODE_MULTIPLE_MODAL) {
throw new RuntimeException("The choice mode CHOICE_MODE_MULTIPLE_MODAL " +
"has not implemented yet");
}
this.choiceMode = choiceMode;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return child.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) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.gridview, null);
}
listchild = new ArrayList<Child>();
for (int j = 0; j < groups.get(groupPosition).getItems().size(); j++) {
listchild.add(child.get(groupPosition).get(j));
}
gridView = (CustomGridView) convertView.findViewById(R.id.GridView_toolbar);
gridView.setExpanded(true);
adapter = new GridAdapter(context, listchild, checkedPositions, groupPosition, childPosition);
gridView.setAdapter(adapter);// Adapter
gridView.setChoiceMode(CustomGridView.CHOICE_MODE_MULTIPLE);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setClicked(groupPosition, childPosition);
System.out.println("position" + checkedPositions.get(groupPosition));
if (checkedPositions.get(groupPosition) != null) {
boolean isChecked = checkedPositions.get(groupPosition).get(childPosition);
if(!isChecked){
}
} else {
System.out.println("false");
}
}
});
return convertView;
}
public void setClicked(int groupPosition, int childPosition) {
switch (choiceMode) {
case CHOICE_MODE_MULTIPLE:
SparseBooleanArray checkedChildPositionsMultiple = checkedPositions.get(groupPosition);
// if in the group there was not any child checked
if (checkedChildPositionsMultiple == null) {
checkedChildPositionsMultiple = new SparseBooleanArray();
// By default, the status of a child is not checked
// So a click will enable it
checkedChildPositionsMultiple.put(childPosition, true);
checkedPositions.put(groupPosition, checkedChildPositionsMultiple);
} else {
boolean oldState = checkedChildPositionsMultiple.get(childPosition);
checkedChildPositionsMultiple.put(childPosition, !oldState);
}
break;
// TODO: Implement it
case CHOICE_MODE_MULTIPLE_MODAL:
throw new RuntimeException("The choice mode CHOICE_MODE_MULTIPLE_MODAL " +
"has not implemented yet");
case CHOICE_MODE_NONE:
checkedPositions.clear();
break;
case CHOICE_MODE_SINGLE_PER_GROUP:
SparseBooleanArray checkedChildPositionsSingle = checkedPositions.get(groupPosition);
// If in the group there was not any child checked
if (checkedChildPositionsSingle == null) {
checkedChildPositionsSingle = new SparseBooleanArray();
// By default, the status of a child is not checked
checkedChildPositionsSingle.put(childPosition, true);
checkedPositions.put(groupPosition, checkedChildPositionsSingle);
} else {
boolean oldState = checkedChildPositionsSingle.get(childPosition);
// If the old state was false, set it as the unique one which is true
if (!oldState) {
checkedChildPositionsSingle.clear();
checkedChildPositionsSingle.put(childPosition, !oldState);
} // Else does not allow the user to uncheck it
}
break;
// This mode will remove all the checked positions from other groups
// and enable just one from the selected group
case CHOICE_MODE_SINGLE_ABSOLUTE:
checkedPositions.clear();
SparseBooleanArray checkedChildPositionsSingleAbsolute = new SparseBooleanArray();
checkedChildPositionsSingleAbsolute.put(childPosition, true);
checkedPositions.put(groupPosition, checkedChildPositionsSingleAbsolute);
break;
}
// Notify that some data has been changed
notifyDataSetChanged();
Log.v(LOG_TAG, "List position updated");
Log.v(LOG_TAG, PrintSparseArrays.sparseArrayToString(checkedPositions));
}
public void setChoiceMode(int choiceMode) {
this.choiceMode = choiceMode;
// For now the choice mode CHOICE_MODEL_MULTIPLE_MODAL
// is not implemented
if (choiceMode == CHOICE_MODE_MULTIPLE_MODAL) {
throw new RuntimeException("The choice mode CHOICE_MODE_MULTIPLE_MODAL " +
"has not implemented yet");
}
checkedPositions.clear();
Log.v(LOG_TAG, "The choice mode has been changed. Now it is " + this.choiceMode);
}
#Override
public int getChildrenCount(int nGroup) {
return 1;
}
#Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
#Override
public int getGroupCount() {
return groups.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Group group = (Group) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_item, null);
}
ExpandableListView mExpandableListView = (ExpandableListView) parent;
mExpandableListView.expandGroup(groupPosition);
TextView tv = (TextView) convertView.findViewById(R.id.group_name);
tv.setText(group.getName());
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
GridAdapter:
public class GridAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Child> child;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
private SparseArray<SparseBooleanArray> checkedPositions;
int groupPosition, childPosition;
public GridAdapter(Context context, ArrayList<Child> childValues, SparseArray<SparseBooleanArray> checkedPositions, int groupPosition, int childPosition) {
mContext = context;
child = childValues;
this.checkedPositions = checkedPositions;
this.childPosition = childPosition;
this.groupPosition = groupPosition;
}
#Override
public int getCount() {
return child.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
NetworkImageView i;
childPosition = position;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_item, null);
holder = new ViewHolder();
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
i = (NetworkImageView) convertView
.findViewById(R.id.flag);
i.setImageUrl(String.valueOf(child.get(childPosition).getImage()), imageLoader);
convertView.setTag(holder);
System.out.println("position" + checkedPositions.get(groupPosition));
if (checkedPositions.get(groupPosition) != null) {
boolean isChecked = checkedPositions.get(groupPosition).get(childPosition);
i.setBackgroundResource(R.color.bg);
if(!isChecked){
}
} else {
System.out.println("false");
}
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text = (TextView) convertView.findViewById(R.id.label);
holder.text.setText(child.get(position).getName());
return convertView;
}
static class ViewHolder {
TextView text;
}
}
In My Assumption
ExpandListAdapter Class
In getChildView() method childPosition does not depends on what country flag is being clicked , Because childPosition here returns the position of the Grid view, not the flags under it
So, no need to pass childPosition in constructor of GridView ,since childPosition will always return 1
To get actual flag clicked position Use onClickListener inside your GridAdapter like:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
NetworkImageView i;
childPosition = position;
if (convertView == null) {
} else {
holder = (ViewHolder) convertView.getTag();
}
convertView.setOnclickListener(new OnClickListener{
#Override
onClick(View v){
isChecked=child.get(childPosition).isChecked();
if(!isChecked){
child.get(childPosition).setChecked(true);
}
else {
child.get(childPosition).setChecked(false);
}
});
No need to handle checked positions separately create two methods in your child class isChecked() and setChecked() to maintain check-uncheck state of flags.
Change color of selected or checked flag here
holder.text = (TextView) convertView.findViewById(R.id.label);
holder.text.setText(child.get(position).getName());
return convertView;
}
and remove your Check logic from ExpandListAdapter Class :
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setClicked(groupPosition, childPosition);
System.out.println("position" + checkedPositions.get(groupPosition));
if (checkedPositions.get(groupPosition) != null) {
boolean isChecked = checkedPositions.get(groupPosition).get(childPosition);
if(!isChecked){
}
} else {
System.out.println("false");
}
}
});
You need to set an 'OnItemClickListener' to your gridAdapter, and overide the onItemClick method as following.
Note :- This will highlight the ImageView you click and if you happened to click another ImageView in the same GridView, that ImageView also will be highlighted! Like in a 'multiple selection'.
gridAdapter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ImageView selectedFlagImageView = (ImageView) view;
selectedFlagImageView.setBackgroundColor(Color.parseColor("#37a93400"));
}
});
This will color the background of the selected ImageView. Color code I've used in here is simply, more alpha and less RGBs.(I used Android Studio color chooser)
Following images will give you an idea about how it will look when giving a background color like this.
Without background color :
With background color :
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 have a ExpandableListView in a Fragment.
I'm posting a request to the server using RoboSpice onCreateView.
I want to be able to update the list twice, once when I get the data from the cache and second after the response returns.
For some reason the list is not populated.
I was able to populate it when I instantiate a new adapter and set the adapter for list, but for some reason it doesn't work on some android devices.
This is the code I'm using:
Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
_expandableListAdapter = new GroupsExpandableListAdapter(getActivity(), getSpiceManager());
_groupListView.setAdapter(_expandableListAdapter);
DBServerHelper.Groups.getByBusinessId(getActivity(), getSpiceManager(), getDBHelper(), _clientBusiness.getId(),
new CachedRequestListener<ClientGroup.List>(getDBHelper(), getActivity()) {
#Override
// This method runs twice, after the data comes from the cache and once the request succeed
public void onRequestFromCache(ClientGroup.List clientGroups) {
if (clientGroups == null || clientGroups.isEmpty()) {
Ln.d("Received clientGroups " + clientGroups == null ? "null" : "empty" + " form request GetByBusinessId");
return;
}
_expandableListAdapter.clear();
_expandableListAdapter.setGroups(clientGroups);
_expandableListAdapter.notifyDataSetChanged();
_groupListView.invalidateViews();
}
#Override
public void onRequestFailure(SpiceException spiceException) {
super.onRequestFailure(spiceException);
Ln.e(spiceException, "Failed to get groups ");
Toaster.showLong(getActivity(), "Could not get businesses groups: " + spiceException.getMessage());
}
});
}
GroupsExpandableListAdapter
public class GroupsExpandableListAdapter extends BaseExpandableListAdapter {
private ClientGroup.List _data;
private Activity _activity;
/**
* It is only possible to create a group using the factory method create below
* #param activity
*/
public GroupsExpandableListAdapter(Activity activity, SpiceManager manager) {
super();
_data = new ClientGroup.List();
_activity = activity;
_manager = manager;
}
#Override
public int getGroupCount() {
return _data.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return _data.size();
}
#Override
public Object getGroup(int groupPosition) {
return _data.get(groupPosition).getName();
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return _data.get(groupPosition).getDeleteReason();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
public void clear() {
_data.clear();
}
public void setGroups(ClientGroup.List groups) {
_groups = groups;
}
#Override
public View getGroupView(final int groupPosition, final boolean isExpanded, final View convertView,
final ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater li = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.group_list_group, parent, false);
TextView textView = (TextView) v.findViewById(R.id.groupName);
textView.setText((CharSequence) _data.get(groupPosition).getName());
((ImageView) v.findViewById(R.id.groupIndicator))
.setImageResource(isExpanded ? R.drawable.arrow_up_turquoise : R.drawable.arrow_down_white);
} else {
v = convertView;
}
return v;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater li = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.group_list_child, parent, false);
} else {
v = convertView;
}
TextView joinGroupButton = (TextView) v.findViewById(R.id.joinGroupButton);
final ClientGroup group = _data.get(groupPosition);
String leaveGroupStr = _activity.getString(R.string.leave_group);
String joinGroupStr = _activity.getString(R.string.join_group);
return v;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
Any suggestions?
Thanks!