Torewin Torewin - 1 month ago 20
Android Question

Android RecyclerView onClick in different Activity

Sort of new to Android so bare with me if this is an easy question.

Everything works well and my onClick in my recyclerView is working in getting the positions of my items, but what my design calls for is to be able to click an item of the recyclerView and open up a new activity (as a popover or pop up). I can achieve this but my problems comes with the information I need to display on the popover. The information comes like this inside the activity (inside a Firebase value call)

attributeList.removeAll(attributeList);
for (DataSnapshot child : dataSnapshot.child("Attribute").getChildren()){
Attribute attribute = child.getValue(Attribute.class);
attribute_list newAttributeList = new attribute_list( attribute.Name + ": " + attribute.Value);
attributeList.add(newAttributeList);
}
attributeAdapter = new attribute_list_adapter(attributeList, getContext());
recyclerAttribute.setAdapter(attributeAdapter);


This works perfectly for displaying the information, but there's more then just a "value" and a "name" associated with the click.

Basically when I select an item, I need to get the position of the item clicked (which I have) and compare it to the position inside attributeList so I can call a Firebase call (or pass the data somehow) to the popover to display values from the "Attribute" class (such as Name, Value, Description, and another list (recyclerView).

My recyclerView:

public class attribute_list_adapter extends RecyclerView.Adapter<attribute_list_adapter.ViewHolder> {

private List<attribute_list> listItems;
private Context context;

public attribute_list_adapter(List<attribute_list> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.attribute_list, parent, false);
return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
attribute_list listItem = listItems.get(position);

holder.txtTitle.setText(listItem.getTxtTitle());
}

@Override
public int getItemCount() {
return listItems.size();
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

public TextView txtTitle;

public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
}

@Override
public void onClick(View v) {

}
}
}

Answer Source

This is example:

public class attribute_list_adapter extends RecyclerView.Adapter<attribute_list_adapter.ViewHolder> {

private List<attribute_list> listItems;
private Context context;

public attribute_list_adapter(List<attribute_list> listItems, Context context) {
    this.listItems = listItems;
    this.context = context;
}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.attribute_list, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
   holder.itemView.setOnClickListener(new View.onClickListener() {
   @Override
   public void onClick(View v) {
     onItemClickListener.onItemClick(position);
   }
});

}


@Override
public int getItemCount() {
    return listItems.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    TextView txtberita;
    ImageView imgberita;
    TextView txtnama;
    public ViewHolder(View itemView) {
        super(itemView);
        txtnama = (TextView) itemView.findViewById(R.id.txtnama);
        txtberita = (TextView) itemView.findViewById(R.id.txtberita);
        imgberita = (ImageView) itemView.findViewById(R.id.imgberita);
    }
}

   public void setOnItemClickListener(OnItemClickListener onItemClickListener){
    this.onItemClickListener = onItemClickListener;
}

OnItemClickListener onItemClickListener;

  public interface OnItemClickListener{
    void onItemClick(int position);
}
}

your Activity. in Oncreate()

 public class TestActivity extends AppCompatActivity implements attribute_list_adapter.OnItemClickListener {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    attribute_list_adapter adapter = new attribute_list_adapter(listItems, this);
     adapter.setOnItemClickListener(this);
}

  @Override
public void onItemClick(int position) {
   // code here
}
}