My design : I have created a custom adapter(SignalsExpandableListAdapter)with a CheckedTextView for my ExpandableListView.
public class SignalsExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHandler;
private HashMap<String,List<String>> listHashMap;
public SignalsExpandableListAdapter(Context context, List<String> listDataHandler, HashMap<String, List<String>> listHashMap) {
this.context = context;
this.listDataHandler = listDataHandler;
this.listHashMap = listHashMap;
}
Then data is filled inside a fragment with HashMap and List as below. This works well. I can see children and parent elements inside my ExpandableListView.
ArrayList<List<String[]>> deviceMList = new ArrayList<>();
final List<String[]> mList = new ArrayList<>();
deviceMList.add(mList);
What I'm looking for I : When I select any child (can be multiple) I want to indicate that selection with a tick using CheckedTextView. And also I want to uncheck when I select any checked child item. (A simple check/uncheck design ). As well when any child is selected another function is called for plotting. This is where I got stuck. Every time when I select one child, random multiple children checkedTextViews are also indicated as checked not only the one I select.
After some searching I tried 3 attempts to overcome this and to check only the child I select. But it seems like neither of my attempts is working. I'm not sure what is going on. My attempts are mentioned below. I would really appreciate any suggestions on this.
I tried Attempt 4 (Described below in section II.) but still seems like check/unchecks is not happening in real-time.
Attempt 1 : Trying to make checked true inside Fragment
listAdapter = new SignalsExpandableListAdapter(context,signalsListDataHeader,signalsListHash);
signalsExpandableListView.setAdapter(listAdapter);
signalsExpandableListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
//tickCheckboxes();
signalsExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
CheckedTextView sensorCheckedView = (CheckedTextView) v.findViewById(R.id.sensorsCheckedTextView);
if(!mService.mPlotManager.ifPropertyExist(deviceMList.get(groupPosition).get(childPosition))) {
sensorCheckedView.setChecked(true);
try {
if(Plot==null) {
Log.e(LOG_TAG, "Plot is null!");
}
mService.mPlotManager.addSignal(deviceMList.get(groupPosition).get(childPosition), Plot);
} catch (Exception e) {
Log.e(LOG_TAG, "Error! + e);
e.printStackTrace();
}
} else {
mService.mPlotManager.removeSignal(deviceMList.get(groupPosition).get(childPosition));
//signalsExpandableListView.setItemChecked(childPosition,false);
sensorCheckedView.setChecked(false);
}
return true;
}
});
Attempt 2 : Creating a different function and call it inside Fragment. Created function is as below and function calling is commented above Attempt 1.
//loop through groups and then children
public void tickCheckboxes() {
//CheckedTextView sensorCheckedView = (CheckedTextView) signalsExpandableListView.findViewById(R.id.sensorsCheckedTextView);
for (int i = 0; i < deviceMList.size(); i++) {
for (int j = 0; j < deviceMList.get(i).size(); j++) {
if (mService.mPlotManager.ifPropertyExist(deviceMList.get(i).get(j))) {
signalsExpandableListView.setItemChecked(j, true);
//sensorCheckedView.setChecked(true);
} else {
signalsExpandableListView.setItemChecked(j, false);
//sensorCheckedView.setChecked(false);
}
}
}
}
Attempt 3 : Accessing child element with ExpandableListView Adapter. I updated the getChildView() method as below.
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
final String childText = (String) getChild(groupPosition,childPosition);
if(view == null){
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.sensors_list_items,null);
}
CheckedTextView txtListChild = (CheckedTextView) view.findViewById((R.id.sensorsCheckedTextView));
txtListChild.setText(childText);
txtListChild.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (txtListChild.isChecked()) {
txtListChild.setChecked(false);
} else {
txtListChild.setChecked(true);
}
}
});
return view;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
List Items xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckedTextView
android:id="#+id/sensorsCheckedTextView"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:textColor="#color/black"
android:paddingTop="5dp"
android:paddingBottom="5dp"/>
</LinearLayout>
Group xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/deviceNameTextView"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:text="Device Name"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="#color/colorAccent"
android:textColor="#color/colorPrimary"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"/>
</LinearLayout>
What I'm looking for II : For check/uncheck I modified Adapter as in Attempt 4 with the help of the answer mentioned below. I cannot see the checked mark in real-time. When I click a child then I want to scroll up/down to visually see the checkmark. Same as with the uncheck. It is strange. It seems like a kind of "refreshing" is required to see the check/uncheck state in real-time. I would appreciate any suggestions on how to get rid of this.
Thank you.
Attempt 4: Modified adapter. This works for a check/uncheck but can not visually see it in real-time.
public class SignalsExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHandler;
private HashMap<String,List<String>> listHashMap;
private MService mService;
private ArrayList<List<String[]>> deviceMList ;
public SignalsExpandableListAdapter(Context context, List<String> listDataHandler, HashMap<String, List<String>> listHashMap,mService service, ArrayList<List<String[]>> dMList ) {
this.context = context;
this.listDataHandler = listDataHandler;
this.listHashMap = listHashMap;
this.mService = service;
this.deviceMList =dMList;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
final String childText = (String) getChild(groupPosition,childPosition);
if(view == null){
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.sensors_list_items,null);
}
CheckedTextView txtListChild = (CheckedTextView) view.findViewById((R.id.sensorsCheckedTextView));
txtListChild.setText(childText);
if(mService.mPlotManager.ifPropertyExist(deviceMList.get(groupPosition).get(childPosition))) {
txtListChild.setChecked(true);
} else {
txtListChild.setChecked(false);
}
return view;
}
I assume that your mService with a class named PlottingService, then try following changes to the adapter:
public class SignalsExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHandler;
private HashMap<String,List<String>> listHashMap;
private PlottingService mService;
public SignalsExpandableListAdapter(Context context, List<String> listDataHandler, HashMap<String, List<String>> listHashMap, PlottingService ps) {
this.context = context;
this.listDataHandler = listDataHandler;
this.listHashMap = listHashMap;
this.mService = ps;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
final String childText = (String) getChild(groupPosition,childPosition);
if(view == null){
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.sensors_list_items,null);
}
CheckedTextView txtListChild = (CheckedTextView) view.findViewById((R.id.sensorsCheckedTextView));
txtListChild.setText(childText);
// Always set check state according to data.
if(mService.mPlotManager.ifPropertyExist(deviceMList.get(groupPosition).get(childPosition))) {
txtListChild.setChecked(true);
} else {
txtListChild.setChecked(false);
}
return view;
}
Inside Fragment, do Attempt 1, only change:
listAdapter = new SignalsExpandableListAdapter(context,signalsListDataHeader,signalsListHash);
to
listAdapter = new SignalsExpandableListAdapter(context,signalsListDataHeader,signalsListHash, mService);
Hope that helps!
Updated:- In Fragment, comment out the OnChildClickListener and in the adapter, use the following getChildView:
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View view, ViewGroup parent) {
String childText = (String) getChild(groupPosition,childPosition);
if(view == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.sensors_list_items,null);
}
CheckedTextView txtListChild = (CheckedTextView) view.findViewById((R.id.sensorsCheckedTextView));
txtListChild.setText(childText);
// Always set check state according to data.
if(mService.mPlotManager.ifPropertyExist(deviceMList.get(groupPosition).get(childPosition))) {
txtListChild.setChecked(true);
} else {
txtListChild.setChecked(false);
}
txtListChild.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckedTextView sensorCheckedView = (CheckedTextView)v;
if(!sensorCheckedView.isChecked()) {
sensorCheckedView.setChecked(true);
try {
if(Plot==null) {
Log.e(LOG_TAG, "Plot is null!");
}
mService.mPlotManager.addSignal(deviceMList.get(groupPosition).get(childPosition), Plot);
} catch (Exception e) {
Log.e(LOG_TAG, "Error!" + e);
e.printStackTrace();
}
} else {
mService.mPlotManager.removeSignal(deviceMList.get(groupPosition).get(childPosition));
sensorCheckedView.setChecked(false);
}
}
});
return view;
}
Updated 2:- In Fragment, comment out the OnChildClickListener and in the adapter, add a inner class and use the following getChildView:
class Position {
int group, child;
Position(int group, int child) {
this.group = group;
this.child = child;
}
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
String childText = (String) getChild(groupPosition,childPosition);
if(view == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.sensors_list_items,null);
}
CheckedTextView txtListChild = (CheckedTextView) view.findViewById((R.id.sensorsCheckedTextView));
txtListChild.setText(childText);
// Always set check state according to data.
if(mService.mPlotManager.ifPropertyExist(deviceMList.get(groupPosition).get(childPosition))) {
txtListChild.setChecked(true);
} else {
txtListChild.setChecked(false);
}
txtListChild.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckedTextView sensorCheckedView = (CheckedTextView)v;
int groupPosition = ((Position)v.getTag()).group;
int childPosition = ((Position)v.getTag()).child;
if(!sensorCheckedView.isChecked()) {
sensorCheckedView.setChecked(true);
try {
if(Plot==null) {
Log.e(LOG_TAG, "Plot is null!");
}
mService.mPlotManager.addSignal(deviceMList.get(groupPosition).get(childPosition), Plot);
} catch (Exception e) {
Log.e(LOG_TAG, "Error!" + e);
e.printStackTrace();
}
} else {
mService.mPlotManager.removeSignal(deviceMList.get(groupPosition).get(childPosition));
sensorCheckedView.setChecked(false);
}
}
});
txtListChild.setTag(new Position(groupPosition, childPosition));
return view;
}
If Updated 2 still have the issue of multiple checked items, then the possible cause is mService.mPlotManager.ifPropertyExist(deviceMList.get(groupPosition).get(childPosition)). Instead of using mService to verify if an item is selected or not, it may be a better idea to change listHashMap from HashMap<String, List<String>> to HashMap<String, List<ChildItem>> [The accepted answer in Values of counter changes after scrolling ExpendableListView, you need a boolean field instead of the integer field, quantity]. Then when a child item is clicked, check and update with the list.
Related
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 have a 'row' view, which has two text views and one check-box. I am trying to implement it so that when the checkbox is clicked on, it displays the strings of the two text views in the same row.
I have implemented a for loop to get the parent of the checkbox view, then iterate all the parent views children and get the strings from inside them. But instead of the text I am getting an empty Toast message.
My first class code:
public class InteractiveArrayAdapter extends ArrayAdapter<model> implements
OnClickListener {
private final List<model> list;
private final Activity context;
public InteractiveArrayAdapter(Activity context, List<model> list) {
super(context, R.layout.rep, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.rep, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.TextView07);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.CheckBox05);
view.getParent();
viewHolder.checkbox
.setOnClickListener(new CompoundButton.OnClickListener() {
#Override
public void onClick(View v) {
boolean h = ((CompoundButton) v).isChecked();
model element = (model) viewHolder.checkbox
.getTag();
element.setSelected(h);
ViewGroup row = (ViewGroup) viewHolder.checkbox
.getParent();
for (int itemPos = 0; itemPos < ((ViewGroup) row)
.getChildCount(); itemPos++) {
View view = ((ViewGroup) row)
.getChildAt(itemPos);
if (view instanceof TextView) {
viewHolder.text = (TextView) view; //Found it!
Toast.makeText(context,
viewHolder.text.getText(), 10000)
.show();
break;
}
}
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
}
My second class model :
public class ListActivity1 extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_list);
// Create an array of Strings, that will be put to our ListActivity
InteractiveArrayAdapter adapter = new InteractiveArrayAdapter(this,
getModel());
setListAdapter(adapter);
}
private List<model> getModel() {
List<model> list = new ArrayList<model>();
list.add(get("Linux"));
list.add(get("Windows7"));
list.add(get("Suse"));
list.add(get("Eclipse"));
list.add(get("Ubuntu"));
list.add(get("Solaris"));
list.add(get("Android"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
// Initially select one of the items
list.get(1).setSelected(true);
return list;
}
private model get(String s) {
return new model(s);
}
}
Can any one help?
pls try this adapter instead of your custom adapter.
public class ListAdapter<T extends BaseEntity> extends ArrayAdapter {
#SuppressWarnings("unused")
private final List<T> objects;
private Activity activity;
private final List<T> checkboxStatusListOfObject;
private OnClickListener listener;
#SuppressWarnings("unchecked")
public ListAdapter(Activity activity, List<T> objects,
OnClickListener listener, List<T> checkboxStatusListOfObject) {
super( R.layout.simple_row_checkbox_1_item_listview , objects);
this.objects = objects;
this.activity = activity;
this.listener = listener;
this.checkboxStatusListOfObject = checkboxStatusListOfObject;
getFilter();
}
#SuppressWarnings("unchecked")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ObjectViews sqView = null;
if(rowView == null)
{
// Get a new instance of the row layout view
LayoutInflater inflater = this.activity.getLayoutInflater();
rowView = inflater.inflate(R.layout.YourCustomLAyout, null);
// Hold the view objects in an object,
// so they don't need to be re-fetched
sqView = new ObjectViews();
sqView.checbox = (CheckBox) rowView.findViewById(R.id.checkbox);
if(this.listener !=null){
sqView.checbox.setOnClickListener( this.listener);
}
sqView.text1 = (TextView) rowView.findViewById(R.id.text);
sqView.dataObject = new Object();
// Cache the view objects in the tag,
// so they can be re-accessed later
rowView.setTag(sqView);
} else {
sqView = (ObjectViews) rowView.getTag();
}
// Transfer the stock data from the data object
// to the view objects
T object = (T) objects.get(position);
sqView.text1.setText(object.toString());
sqView.checbox.setText("");
sqView.checbox.setTag(sqView);
sqView.dataObject = object;
if(sqView.getChecbox().isChecked()){
if(this.checkboxStatusListOfObject.indexOf( sqView.dataObject) != -1 ){
sqView.getChecbox().setChecked(true);
}
else{
sqView.getChecbox().setChecked(false);
}
}
return rowView;
}
public class ObjectViews {
Object dataObject;
CheckBox checbox;
TextView text1;
public Object getDataObject() {
return dataObject;
}
public void setDataObject(Object dataObject) {
this.dataObject = dataObject;
}
public CheckBox getChecbox() {
return checbox;
}
public void setChecbox(CheckBox checbox) {
this.checbox = checbox;
}
public TextView getText1() {
return text1;
}
public void setText1(TextView text1) {
this.text1 = text1;
}
}
Define your listener in activity and send to adapter.
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
// here your code
if(cb.isChecked()){
ObjectViews object = (ObjectViews) cb.getTag();
String text = object.getText1().getText();
Toast.makeText(context,text, Toast.LENGTH_LONG).show();
}
}
};
//you can configure this adapter constructor. this is only example
yourListView.setAdapter(
new ListAdapter(getApplicationContext(), yourObjectList
listener, YourSelectedObjectList));
and layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<CheckBox
android:id="#+id/checkbox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20dp" />
<TextView
android:id="#+id/text"
android:layout_width="0dp"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:textSize="20dp" />
</LinearLayout>
I'm trying to refresh the list view when I click on a single item of the list view.
For example I'm trying to do something like this:
public class RssItemsAdapter extends ArrayAdapter<Item> {
private TextView txtTitle;
private ImageView imgFavourite;
private int resourceId;
private Context context;
private List<Item> items = new ArrayList<Item>();
public RssItemsAdapter(Context context, int resourceId, List<Item> items)
{
super(context, resourceId, items);
this.context = context;
this.items = items;
this.resourceId = resourceId;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Item getItem(int index) {
return items.get(index);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
Item item = getItem(position);
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(resourceId, parent, false);
}
txtTitle = (TextView) row.findViewById(R.id.title);
txtTitle.setText(item.getTitle());
imgFavourite = (ImageView) row.findViewById(R.id.favourite);
imgFavourite.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (item.isFavourite() == true) {
imgFavourite.setBackgroundResource(android.R.drawable.btn_star_big_off);
}
else {
imgFavourite.setBackgroundResource(android.R.drawable.btn_star_big_on);
}
}
});
// add a "star" icon on favourite items
if (item.isFavourite() == true)
imgFavourite.setBackgroundResource(android.R.drawable.btn_star_big_on);
else
imgFavourite.setBackgroundResource(android.R.drawable.btn_star_big_off);
return row;
}
Every time I click on the imgFavourite icon it should change. Where is the problem?
It doesn't look like you are ever changing the value of item.isFavourite(). Assuming you want to toggle when that item is clicked...
imgFavourite.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Toggle the current state
item.setIsFavourite(!item.isFavourite());
if (item.isFavourite() == true) {
imgFavourite.setBackgroundResource(android.R.drawable.btn_star_big_off);
}
else {
imgFavourite.setBackgroundResource(android.R.drawable.btn_star_big_on);
}
}
});
Also, you should be doing this kind of work in the ListView's onItemClickListsner. As is, this will create a new onClickListener each time a new row is brought into view. Beyond that, check into the "view holder pattern".
I am trying to set up an OnItemClickListener for the items in my Gridview. For some reason the onItemCLick method within the listener is never called.
Setting the listener and the adapter:
UsersAdapter usersAdapter = new UsersAdapter(venueUsers);
gridView.setAdapter(usersAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(Users.this, com.roqbot.client.login.Profile.class);
intent.putExtra("idUser", id);
startActivity(intent);
}
});
My Adapter:
private class UsersAdapter extends BaseAdapter implements ListAdapter {
private JSONArray users;
private UsersAdapter(JSONArray users) {
this.users = users;
}
public int getCount() {
return users.length();
}
public JSONObject getItem(int position) {
return users.optJSONObject(position);
}
public long getItemId(int position) {
return users.optJSONObject(position).optInt("idUser");
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = Users.this.getLayoutInflater().inflate(R.layout.user_icon, null);
JSONObject user = getItem(position);
TextView username = (TextView) convertView.findViewById(R.id.username);
username.setText(user.optString("sName"));
TextView userScore = (TextView) convertView.findViewById(R.id.userScore);
int iDJScore = user.optInt("iDJScore");
if (iDJScore > 0) {
userScore.setText(Integer.toString(iDJScore));
}
else {
userScore.setVisibility(TextView.INVISIBLE);
((ImageView) convertView.findViewById(R.id.userScoreBg)).setVisibility(View.INVISIBLE);
}
TextView userLevel = (TextView) convertView.findViewById(R.id.userLevel);
userLevel.setText(user.optString("iDJLevel"));
TextView userMatch = (TextView) convertView.findViewById(R.id.userMatch);
ImageView matchIcon = (ImageView) convertView.findViewById(R.id.matchIcon);
int iCompatibility = user.optInt("iCompatibility");
if (iCompatibility != 0) {
userMatch.setText( iCompatibility + "%");
}
else {
userMatch.setVisibility(TextView.INVISIBLE);
matchIcon.setVisibility(ImageView.INVISIBLE);
}
ImageView userIcon = (ImageView) convertView.findViewById(R.id.userIcon);
String sUserIcon = user.optString("sImageUrl-thumb");
imageLoader.DisplayImage(sUserIcon, Users.this, userIcon);
return convertView;
}
}
I am pretty baffled as to why the click listener isn't working. This code works in a lot of other places for me.
I had same problem.
In my situation, "android:focusable = true" block item click event
In My GridList Item Layout
<TextView android:id="#+id/auction_item_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
***android:focusable="true"***
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
/>
I remove [ android:focusable="true" ], problem is solved
Actually, you just need to make sure that the items in the grid aren't clickable. GridViews can't handle clickable items. Buttons for example won't work. And also if you made an item from a LinearLayout, or any other non-clickable item, you have to make sure that you didn't set the property: clickable="true".