I'm doing a program and I do not know why it fails. I have a ExpandableListView
object that makes Adapter
and% Fragment
that calls this object. To the elements of the list, it works, the problem is in the onChildClick
, what I pretend is that when I press an element of the list, it is saved in TextView
and shows it to use it later. The problem is that it only collects the first element of the list when pressed, if I pulse another element, the application stops working without giving me an error. What could I do to solve this?
Class ExpandibleListView
:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader ;
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(
int groupPosition, final int childPosition, boolean isLastChild,
View convertView, ViewGroup parent
) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
The Fragment :
public class MainActivityFragment extends Fragment {
//Expandable list Adapter
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
//Users
private List <User> items = new ArrayList<>();
//Obejeto a pasar
User user = new User();
String password;
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_main, container, false);
//Botones
TextPass = (EditText) view.findViewById(R.id.IdpasswordUser);
TextName = (TextView) view.findViewById(R.id.text1);
// get the listview
expListView = (ExpandableListView) view.findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(
getActivity(), listDataHeader, listDataChild
);
// setting list adapter
expListView.setAdapter(listAdapter);
ExpandableListView ExList =(ExpandableListView) view.findViewById(R.id.lvExp) ;
//Llamamos al boton fichar
fichar = (Button) view.findViewById(R.id.fichar);
//Call ExpandibleList view child on pressed
ExList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(
ExpandableListView parent, View v, int groupPosition,
int childPosition, long id
) {
ShowItem(listDataHeader.get(childPosition));
TextName.setText(
listAdapter
.getChild(groupPosition,childPosition).toString()
);
return false;
}
});
fichar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
password = TextPass.getText().toString();
//SuperUsuario
if(
key.equalsIgnoreCase("cesi.tic")&&
password.equalsIgnoreCase("123456789")
){
((MainActivity) getActivity()).EditarUsuario();
fichar.setVisibility(view.INVISIBLE);
TextPass.setVisibility(view.INVISIBLE);
TextDNI.setVisibility(view.INVISIBLE);
}else{
LeerFirebase();
}
}
});
return view;
}
public void ShowItem(String id)
{
// do something
}
}
As you can see in onChildClick
, it's like I think it should work but it's not like that.