Narding Da Greatx
Narding Da Greatx

Reputation: 33

I'm trying to set an onclick method on the searched result of my searchview

And now I just want to whenever I click on the expadablelistadapter it'll go to another acitvity. How to I determine which of it is clicked? I'm all to new in android, hope someone can help me with his.

This is the code to where the code of my listadapter

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

private Context context;
private String container;
private ArrayList<ParentRow> parentRowList;
private ArrayList<ParentRow> originalList;

   public MyExpandableListAdapter(Context context
        , ArrayList<ParentRow> originalList) {
    this.context = context;
    this.parentRowList = new ArrayList<>();
    this.parentRowList.addAll(originalList);
    this.originalList = new ArrayList<>();
    this.originalList.addAll(originalList);
}

@Override
public int getGroupCount() {
    return parentRowList.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    return parentRowList.get(groupPosition).getChildList().size();
}

@Override
public Object getGroup(int groupPosition) {
    return parentRowList.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return parentRowList.get(groupPosition).getChildList().get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public boolean hasStableIds() {
    return true;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    ParentRow parentRow = (ParentRow) getGroup(groupPosition);

    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.parent_row, null);
    }

    TextView heading = (TextView) convertView.findViewById(R.id.parent_text);

    heading.setText(parentRow.getName().trim());
    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    ChildRow childRow = (ChildRow) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater)
                context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.child_row, null);
    }

    ImageView childIcon = (ImageView) convertView.findViewById(R.id.child_icon);
    childIcon.setImageResource(childRow.getIcon());

    final TextView childText = (TextView) convertView.findViewById(R.id.child_text);
    childText.setText(childRow.getText().trim());

    final View finalConvertView = convertView;
    childText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(finalConvertView.getContext()
                    , childText.getText()
                    , Toast.LENGTH_SHORT).show();

        }
    });

    return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

public void filterData(String query) {
    query = query.toLowerCase();
    parentRowList.clear();

    if (query.isEmpty()) {
        parentRowList.addAll(originalList);
    }
    else {
        for (ParentRow parentRow : originalList) {
            ArrayList<ChildRow> childList = parentRow.getChildList();
            ArrayList<ChildRow> newList = new ArrayList<ChildRow>();

            for (ChildRow childRow: childList) {
                if (childRow.getText().toLowerCase().contains(query)) {
                    newList.add(childRow);
                }
            } // end for (com.example.user.searchviewexpandablelistview.ChildRow childRow: childList)
            if (newList.size() > 0) {
                ParentRow nParentRow = new ParentRow(parentRow.getName(), newList);
                parentRowList.add(nParentRow);
            }
        } // end or (com.example.user.searchviewexpandablelistview.ParentRow parentRow : originalList)
    } // end else

    notifyDataSetChanged();
}

}

This is the part of my code where my onclick functionality is suppose to be, I want to add and intent to other activity how can I achieve it?

 @Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    ChildRow childRow = (ChildRow) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater)
                context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.child_row, null);
    }

    ImageView childIcon = (ImageView) convertView.findViewById(R.id.child_icon);
    childIcon.setImageResource(childRow.getIcon());

    final TextView childText = (TextView) convertView.findViewById(R.id.child_text);
    childText.setText(childRow.getText().trim());

    final View finalConvertView = convertView;
    childText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(finalConvertView.getContext()
                    , childText.getText()
                    , Toast.LENGTH_SHORT).show();
        }
    });

    return convertView;
}

Upvotes: 0

Views: 121

Answers (2)

Bhupat Bheda
Bhupat Bheda

Reputation: 1978

add this two listener

expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                //here get you selected groupPosition
                Log.e("position",""+groupPosition);
                ParentRow parentRow = parentRowList.get(groupPosition);
                //if you want to send intent of any parent row click then put intent code here
                startActivity(new Intent......);
                return false;
            }
        });
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                //here get you selected groupPostion and childPosition 
                Log.e("position",""+groupPosition+":"+childPosition);
                 ChildRow childRow = parentRowList.get(groupPosition).getChildList().get(childPosition)
                //if you want to send intent of any child row click then put intent code here
                startActivity(new Intent......);
                return false;
            }
        });

Upvotes: 0

Ahtsham Abbasi
Ahtsham Abbasi

Reputation: 84

Override this function in your class:

@Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {     
         //check childPosition and call appropriate activity    
         return true; 
}

Upvotes: 1

Related Questions