0

i am developing a shopping app in that app i need to list the products that are uploaded by the seller and i need to display those items in list view with limited information.

once buyer click on the particular item it need to naviagte to another page and it needs to show more details about that product.

i have generated a list using recyclerview in android studio.if i click on the one dataset in that listview it has to navigate to another page and has to display more information about the dataset that is cliked by the user.

i have tried the following code segment but it wont work .

public class getItem extends AppCompatActivity {

    private RecyclerView mAd_list;
    private DatabaseReference mDatabase;

    Button buy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_items);

        mDatabase = FirebaseDatabase.getInstance().getReference().child("Items");

        mAd_list = (RecyclerView) findViewById(R.id.ad_list);
        mAd_list.setHasFixedSize(true);
        mAd_list.setLayoutManager(new LinearLayoutManager(this) {
        });

        mAd_list.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intentRegistration=new Intent(getApplicationContext(),DisplayListDetailActivity.class);
                startActivity(intentRegistration);
            }

        });


    }

    @Override
    protected void onStart() {
        super.onStart();

        FirebaseRecyclerAdapter<Item, AdViewHolder>firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Item, AdViewHolder>(

                Item.class,
                R.layout.view_item,
                AdViewHolder.class,
                mDatabase

        ) {

            @Override
            protected void populateViewHolder(AdViewHolder viewHolder, Item model, int position) {

                viewHolder.setTitle(model.getName());
                viewHolder.setDesc(model.getPrice().toString());
                viewHolder.setImage(getApplicationContext(), model.getImage());

            }
        };
        mAd_list.setAdapter(firebaseRecyclerAdapter);
    }
}

class AdViewHolder extends RecyclerView.ViewHolder{

    View mView;

    public AdViewHolder(View itemView) {
        super(itemView);

        mView = itemView;
    }

    public void setTitle(String title) {

        TextView post_title = (TextView) mView.findViewById(R.id.post_title);
        post_title.setText(title);
    }

    public void setDesc(String desc){
        TextView post_desc = (TextView) mView.findViewById(R.id.post_desc);
        post_desc.setText(desc);
    }

    public void setImage (Context ctx, String image){
        ImageView post_image = (ImageView) mView.findViewById(R.id.post);
        Picasso.with(ctx).load(image).into(post_image);

    }

}

interface

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp">



    <TextView
        android:layout_width="match_parent"
        android:layout_height="324dp"
        android:id="@+id/post_desc"
        android:text="The Ad Description Goes Here"
        android:paddingTop="260dp"
        android:textSize="16dp"
        android:paddingBottom="15dp"
        android:paddingLeft="10dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    </LinearLayout>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/post"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/post_title"
        android:text="Ad Title Goes Here"
        android:paddingTop="220dp"
        android:textSize="16dp"
        android:paddingLeft="10dp"
        android:textStyle="bold" />



</android.support.v7.widget.CardView>
Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
  • You have set click listener on list container. There's much questions/tutorials about your case. Listening of click should be set on items not entire list but it's hard to paste some sources by phone, sorry... – Karol Kulbaka Oct 16 '17 at 18:13
  • 1
    You can find the solution [here](https://stackoverflow.com/questions/28767413/how-to-open-a-different-activity-on-recyclerview-item-onclick) – Ravi Oct 16 '17 at 18:29
  • Possible duplicate of [RecyclerView onClick](https://stackoverflow.com/questions/24471109/recyclerview-onclick) – Viktor Yakunin Oct 16 '17 at 19:08
  • Try setOnItemClickListener ? – Cagri Yalcin Oct 17 '17 at 06:25

2 Answers2

0

You should listen to the click event on each item of your recyclerview, so try to implement the View.OnClickListener interface in AdViewHolder class (and please, define your class name based on convention).

glennsl
  • 28,186
  • 12
  • 57
  • 75
Ibrokhim Kholmatov
  • 1,079
  • 2
  • 13
  • 16
0

You are setting the OnClickListener in the entire Recycler.

Instead of doing that you have to create the onclicListener in every item. You can do it in the viewHolder.

public AdViewHolder(View itemView) {
        super(itemView);

        mView = itemView;
        mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Do Something here for every Item
            }
        });
    }
AndroidStorm
  • 859
  • 9
  • 25