0

I did recyclerView, and each child is "Detail".

I am now asking how to sent user to the next activity when he click on "Detail". For each click send him to the other activity.

 private List<ParentObject> initData() {
    TitleCreator titleCreator = TitleCreator.get(this);
    List<TitleParent> titles = titleCreator.getAll();

    List<ParentObject> parentObject = new ArrayList<>();
    for(TitleParent title:titles)
    {
        List<Object> childList = new ArrayList<>();
        childList.add(new TitleChild("Detail", ""));
        title.setChildObjectList(childList);
        parentObject.add(title);
    }
    return parentObject;

}

This is code where I put every child to be "Detail".

I had four Parent titles and of course four child titles. When user click on first *Parent title it open child title. Question is when user click on this child title how to send him to the other activity?

Here is picture

Here is my adapter fil

package com.example.user_pc.glavnastranicaprojekat.Adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import 
com.bignerdranch.expandablerecyclerview.Adapter.ExpandableRecyclerAdapter;
import com.bignerdranch.expandablerecyclerview.Model.ParentObject;
import com.example.user_pc.glavnastranicaprojekat.models.TitleChild; 
import com.example.user_pc.glavnastranicaprojekat.models.TitleParent;
import com.example.user_pc.glavnastranicaprojekat.R;
import 
com.example.user_pc.glavnastranicaprojekat.ViewHolders.TitleChildViewHolder;
import
com.example.user_pc.glavnastranicaprojekat.ViewHolders.TitleParentViewHolder;

import java.util.List;



public class MyAdapter extends 
ExpandableRecyclerAdapter<TitleParentViewHolder,TitleChildViewHolder> {

LayoutInflater inflater;

public MyAdapter(Context context, List<ParentObject> parentItemList) {
    super(context, parentItemList);
    inflater = LayoutInflater.from(context);
}

@Override
public TitleParentViewHolder onCreateParentViewHolder(ViewGroup viewGroup) {
    View view = inflater.inflate(R.layout.list_parent,viewGroup,false);
    return new TitleParentViewHolder(view);
}

@Override
public TitleChildViewHolder onCreateChildViewHolder(ViewGroup viewGroup) {
    View view = inflater.inflate(R.layout.list_child,viewGroup,false);
    return new TitleChildViewHolder(view);
}

@Override
public void onBindParentViewHolder(TitleParentViewHolder titleParentViewHolder, int i, Object o) {
    TitleParent title = (TitleParent)o;
    titleParentViewHolder._textView.setText(title.getTitle());

}

@Override
public void onBindChildViewHolder(TitleChildViewHolder titleChildViewHolder, int i, Object o) {
    TitleChild title = (TitleChild)o;
    titleChildViewHolder.option1.setText(title.getOption1());
    titleChildViewHolder.option2.setText(title.getOption2());

}
}

TitleParent.java :

package com.example.user_pc.glavnastranicaprojekat.models;

import com.bignerdranch.expandablerecyclerview.Model.ParentObject;

import java.util.List;
import java.util.UUID;



public class TitleParent implements ParentObject{

private List<Object> mChildrenList;
private UUID _id;
private String title;

public TitleParent(String title) {
    this.title = title;
    _id = UUID.randomUUID();
}

public UUID get_id() {
    return _id;
}

public void set_id(UUID _id) {
    this._id = _id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

@Override
public List<Object> getChildObjectList() {
    return mChildrenList;
}

@Override
public void setChildObjectList(List<Object> list) {
    mChildrenList = list;
}
}

Here is TitleChild.java:

package com.example.user_pc.glavnastranicaprojekat.models;


public class TitleChild {

public String option1;
public String option2;

public TitleChild(String option1, String option2) {
    this.option1 = option1;
    this.option2 = option2;
}

public String getOption1() {
    return option1;
}

public void setOption1(String option1) {
    this.option1 = option1;
}

public String getOption2() {
    return option2;
}

public void setOption2(String option2) {
    this.option2 = option2;
}


}

TitleCreator.java:

package com.example.user_pc.glavnastranicaprojekat.models;


import android.content.Context;

import java.util.ArrayList;
import java.util.List;



public class TitleCreator {
static TitleCreator _titleCreator;
List<TitleParent> _titleParents;

public TitleCreator(Context context) {
    _titleParents = new ArrayList<>();


    TitleParent title = new TitleParent(String.format("Coppa"));
    _titleParents.add(title);


    TitleParent title1 = new TitleParent(String.format("Levante"));
    _titleParents.add(title1);


    TitleParent title2 = new TitleParent(String.format("Monument"));
    _titleParents.add(title2);

    TitleParent title3 = new TitleParent(String.format("Comming soon"));
    _titleParents.add(title3);

}

public static TitleCreator get(Context context)
{
    if(_titleCreator == null)
        _titleCreator = new TitleCreator(context);
    return _titleCreator;
}

public List<TitleParent> getAll() {
    return _titleParents;
}
}

TitleChildViewHolder:

package com.example.user_pc.glavnastranicaprojekat.ViewHolders;


import android.view.View;
import android.widget.TextView;

import com.bignerdranch.expandablerecyclerview.ViewHolder.ChildViewHolder;

import com.example.user_pc.glavnastranicaprojekat.R;



public class TitleChildViewHolder extends ChildViewHolder {
public TextView option1,option2;
public TitleChildViewHolder(View itemView) {
    super(itemView);
    option1 = itemView.findViewById(R.id.option1);
    option2 = itemView.findViewById(R.id.option2);

}
}

TitleParentViewHolder:

package com.example.user_pc.glavnastranicaprojekat.ViewHolders;


import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.bignerdranch.expandablerecyclerview.ViewHolder.ParentViewHolder;
import com.example.user_pc.glavnastranicaprojekat.R;



public class TitleParentViewHolder extends ParentViewHolder {
public TextView _textView;
public ImageButton _imageButton;

 public TitleParentViewHolder(View itemView) {
    super(itemView);
    _textView = itemView.findViewById(R.id.parentTitle);
    _imageButton =  itemView.findViewById(R.id.expandArrow);


}
}

MainActivity:

    RecyclerView recyclerView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
recyclerView = findViewById(R.id.myRecyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
MyAdapter adapter = new MyAdapter(this,initData());
    adapter.setParentClickableViewAnimationDefaultDuration();
    adapter.setParentAndIconExpandOnClick(true);

    recyclerView.setAdapter(adapter);

}

 private List<ParentObject> initData() {
    TitleCreator titleCreator = TitleCreator.get(this);
    List<TitleParent> titles = titleCreator.getAll();



    List<ParentObject> parentObject = new ArrayList<>();
    for(TitleParent title:titles)
    {
        List<Object> childList = new ArrayList<>();
        childList.add(new TitleChild("Detail", ""));
        title.setChildObjectList(childList);
        parentObject.add(title);

    }
    return parentObject;

}
XcombeX
  • 117
  • 1
  • 8

2 Answers2

0
titleChildViewHolder.itemView.setOnClickListener(
      new OnClickListener(View v){
          context.startActivity(new Intent(context,DESTINATION_ACTIVITY.class)));
     }
);

Put this code inside onBindChildViewHolder

Jay Thummar
  • 2,281
  • 1
  • 14
  • 22
0

try something like this:

@Override
public void onBindChildViewHolder(TitleChildViewHolder titleChildViewHolder, int i, Object o) {
    TitleChild title = (TitleChild)o;
    titleChildViewHolder.option1.setText(title.getOption1());
    titleChildViewHolder.option2.setText(title.getOption2());

    titleChildViewHolder.option1.setOnClickListener(
      new OnClickListener(View v){
          context.startActivity(new Intent(context,ACTIVITY_TO_LAUNCH.class)));
     }
);
}

If you need you can set an equivalent OnClickListener also on titleChildViewHolder.option2

Maybe you could add a container of views option1 and option2 so you could set only one ClickListener

firegloves
  • 5,581
  • 2
  • 29
  • 50
  • I addde that in MainActivity, but it still not working – XcombeX May 26 '18 at 14:39
  • You can t do that into the activity. In all the cases if you need help you must post your code, otherwise we can t read it and correct it – firegloves May 26 '18 at 14:49
  • I add, please check it now – XcombeX May 26 '18 at 14:54
  • I can't see MainActivity. Anyway why don't you replace my suggested onBindChildViewHolder method with yours? – firegloves May 26 '18 at 15:01
  • I will put MainActivity, but I have to change some line of code...because my MainActivity is long, I use something for map, log in, reg, and so on...where I have to put your suggested? – XcombeX May 26 '18 at 15:06
  • Check now please – XcombeX May 26 '18 at 15:16
  • I posted a method named onBindChildViewHolder. Press ctrl+f on this page and find your code corresponding to that method, then update that method with what i putted there – firegloves May 26 '18 at 16:07
  • I still not woking – XcombeX May 26 '18 at 17:37
  • I have to make that method in MainActivity, right? And then caal that method on onCreat method? – XcombeX May 26 '18 at 17:41
  • Have you any idea of what you are doing? I can t help you if you haven t studied basic android and RecyclerView functionalities – firegloves May 26 '18 at 17:45
  • I have never use onBundChildViewHolder...and it is first time to use recycler view – XcombeX May 26 '18 at 17:47
  • So go to read official documentation about that. Search for some guide or tutorial, try to understand their functionality, then try yourself and if you not will be able to solve come back to ask for help. As now we are not understanding each other – firegloves May 26 '18 at 17:49