0

Actually my issue is when i am clicking on Recyclerview items it shows only that item data in next activity based upon using id.but there is no Onitemclick method in recyclerview and how to rectify this error any one can solve this error?

MainActivity.java

    public class MainActivity extends Activity {
    RecyclerView recyclerView;
    List<Your> yourss;
    String Tag_id="category_id";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView) findViewById(R.id.recycle);
        LinearLayoutManager ll = new LinearLayoutManager(MainActivity.this);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(ll);
        new JsonTask().execute();
    }

    public class JsonTask extends AsyncTask<String, String, List<Your>> {
        @Override
        protected List<Your> doInBackground(String... params) {
            HttpURLConnection httpURLConnection = null;
            BufferedReader bufferedReader = null;
            try {
                URL url = new URL("http://yoursubshop.com/webservices/categories.php");
                httpURLConnection = (HttpURLConnection) url.openConnection();
                InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());
                bufferedReader = new BufferedReader(inputStreamReader);
                StringBuffer sb = new StringBuffer();
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    sb.append(line);
                }
                String finaljson = sb.toString();
                JSONObject firstobject = new JSONObject(finaljson);
                JSONArray firstarray = firstobject.getJSONArray("data");
                List<Your> yo = new ArrayList<>();
                for (int i = 0; i < firstarray.length(); i++) {
                    JSONObject finalobject = firstarray.getJSONObject(i);
                    Your yos = new Your();
                    yos.category_id = finalobject.getString("Tag_id");
                    yos.category_name = finalobject.getString("category_name");
                    yo.add(yos);
                }
                return yo;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } finally {
                if (httpURLConnection != null) {
                    httpURLConnection.disconnect();
                }
                try {
                    if (bufferedReader != null) {

                        bufferedReader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(List<Your> result) {
            super.onPostExecute(result);
            Rvadapter listAdapter = new Rvadapter(MainActivity.this, result);
            recyclerView.setAdapter(listAdapter);
              recyclerView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new         Intent(MainActivity.this,SeconActivity.class);
                    i.putExtra("data",Tag_id);
                    startActivity(i);
                }
            });
        }
    }

    public class Rvadapter extends RecyclerView.Adapter<Rvadapter.Myadapter> {
        List<Your> yours = Collections.emptyList();
        Context c;
        public class Myadapter extends RecyclerView.ViewHolder {
            TextView id;
            TextView name;
            CardView cv;
            public Myadapter(final View itemView) {
                super(itemView);
                id = (TextView) itemView.findViewById(R.id.texts);
                name = (TextView) itemView.findViewById(R.id.textsa);
                cv = (CardView)itemView.findViewById(R.id.card);

            }
        }
        public Rvadapter(Context c, List<Your> yours) {
            this.yours = yours;
            this.c = c;
        }
        @Override
        public Myadapter onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.lists, parent, false);
            Myadapter m = new Myadapter(v);
            return m;
        }

        @Override
        public void onBindViewHolder(Myadapter holder, int position) {
            Your  y = yours.get(position);
            holder.id.setText(y.category_id);
            holder.name.setText(y.category_name);
        }

        @Override
        public int getItemCount() {
            return yours == null ? 0 : yours.size();
        }

        @Override
        public void onAttachedToRecyclerView(RecyclerView recyclerView) {
            super.onAttachedToRecyclerView(recyclerView);
        }
    }
   }

SecondActivity.java

    public class SeconActivity extends Activity 
    {
    RecyclerView recyclerView;
    List<Shop> shops;
    String serverUrl = "http://yoursubshop.com/webservices/categories-shop.php?category=";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.seocnd_main);
        recyclerView = (RecyclerView) findViewById(R.id.secondmain);
        LinearLayoutManager ll = new LinearLayoutManager(SeconActivity.this);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(ll);
        Intent in = getIntent();
        String serverUrlId = in.getStringExtra("data");
        serverUrl = serverUrl.concat(serverUrlId);
        new JsonTask().execute();
    }
    public class JsonTask extends AsyncTask<String, String, List<Shop>> {
        @Override
        protected List<Shop> doInBackground(String... params) {
            HttpURLConnection httpURLConnection = null;
            BufferedReader bufferedReader = null;
            try {
                URL url = new URL(serverUrl);
                httpURLConnection = (HttpURLConnection) url.openConnection();
                InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());
                bufferedReader = new BufferedReader(inputStreamReader);
                StringBuffer sb = new StringBuffer();
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    sb.append(line);
                }
                String finaljson = sb.toString();
                JSONObject firstobject = new JSONObject(finaljson);
                JSONArray firstarray = firstobject.getJSONArray("data");
                List<Shop> yo = new ArrayList<>();
                for (int i = 0; i < firstarray.length(); i++) {
                    JSONObject finalobject = firstarray.getJSONObject(i);
                    Shop yos = new Shop();
                    yos.shop_id = finalobject.getString("shop_id");
                    yos.shop_name= finalobject.getString("shop_name");
                    yo.add(yos);
                }
                return yo;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } finally {
                if (httpURLConnection != null) {
                    httpURLConnection.disconnect();
                }
                try {
                    if (bufferedReader != null) {

                        bufferedReader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(List<Shop> result) {
            super.onPostExecute(result);
            Rvadapter listAdapter = new Rvadapter(SeconActivity.this, result);
            recyclerView.setAdapter(listAdapter);

        }
    }

    public class Rvadapter extends RecyclerView.Adapter<Rvadapter.Myadapter> {
        List<Shop> yours = Collections.emptyList();
        Context c;
        public class Myadapter extends RecyclerView.ViewHolder {
            TextView id;
            TextView name;
            CardView cv;
            public Myadapter(final View itemView) {
                super(itemView);
                id = (TextView) itemView.findViewById(R.id.shopt);
                name = (TextView) itemView.findViewById(R.id.shopr);
                cv = (CardView)itemView.findViewById(R.id.card);

            }
        }
        public Rvadapter(Context c, List<Shop> yours) {
            this.yours = yours;
            this.c = c;
        }
        @Override
        public Myadapter onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.shopss, parent, false);
            Myadapter m = new Myadapter(v);
            return m;
        }

        @Override
        public void onBindViewHolder(Myadapter holder, int position) {
            Shop  y = yours.get(position);
            holder.id.setText(y.shop_id);
            holder.name.setText(y.shop_name);
        }

        @Override
        public int getItemCount() {
            return yours == null ? 0 : yours.size();
        }

        @Override
        public void onAttachedToRecyclerView(RecyclerView recyclerView) {
            super.onAttachedToRecyclerView(recyclerView);
        }
    }
}
mehrdad khosravi
  • 2,228
  • 9
  • 29
  • 34
A.asha
  • 45
  • 6
  • I hope you can start from [here](http://stackoverflow.com/questions/24885223/why-doesnt-recyclerview-have-onitemclicklistener-and-how-recyclerview-is-dif) – Raghavendra Jun 29 '16 at 13:01
  • i cannt understand will u tell based upon my programme sir – A.asha Jun 29 '16 at 13:03
  • You have cardview in your adapter right. Implement OnClickListener and set the listener. In the onClick method use the intent to move to next screen – Raghavendra Jun 29 '16 at 13:05
  • If your question is how to navigate to next screen from recyclerview the above comment is my suggestion. If your question is different please let me know – Raghavendra Jun 29 '16 at 13:07
  • my issue is when i am click on single item on list of items in recyclerview.that single item has some particular id in server json based on that id.information is retrived from server and it display in next activity. – A.asha Jun 29 '16 at 13:10
  • reply sir @Raghavendra – A.asha Jun 29 '16 at 17:26
  • use getAdapterPosition() to get the position. i.e., CURRENT_POSITION = getAdapterPosition(); in onClick method remove the old initialization and try – Raghavendra Jun 30 '16 at 04:54

2 Answers2

0

set click listener to your root view of item layout, I change your onBindViewHolder() method

@Override
        public void onBindViewHolder(Myadapter holder, int position) {
            Shop  y = yours.get(position);
            holder.id.setText(y.shop_id);
            holder.name.setText(y.shop_name);

            holder.ROOT_VIEW.setTag(position); // here ROOT_VIEW is your inflated layout root 
            holder.ROOT_VIEW.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = (int) v.getTag(); // use this pos to get this perticular view related data
                    Intent i = new Intent(MainActivity.this,SeconActivity.class);
                    i.putExtra("data",Tag_id);
                    startActivity(i);
                }
            });
        }

declare ROOT_VIEW in your holder class, get tag_id by pos from your list or what u have

Divyang Panchal
  • 1,889
  • 1
  • 19
  • 27
0
public class Rvadapter extends RecyclerView.Adapter<Rvadapter.Myadapter> implements View.OnClicklistener{
        List<Your> yours = Collections.emptyList();
        Context c;
        public class My
        private int CURRENT_POSITION = 0;

        public class Myadapter extends RecyclerView.ViewHolder  {

            TextView id;
            TextView name;
            CardView cv;
            public Myadapter(final View itemView) {
                super(itemView);
                id = (TextView) itemView.findViewById(R.id.texts);
                name = (TextView) itemView.findViewById(R.id.textsa);
                cv = (CardView)itemView.findViewById(R.id.card);
                cv.setOnClicklistener(this);
            }
        }


        ...

        @Override
        public void onBindViewHolder(Myadapter holder, int position) {
        CURRENT_POSITION = position;
        ...
        //your code
        }


        @Override
        public void onClick(View view){

                Your y = yours.get(CURRENT_POSITION);
                if(y != null){}
                    Intent i = new Intent(this, ,SeconActivity.class);
                    i.putExtra("id", y.category_id);
                    startActivity(i);
                }
        }

        ...
        //your code
}

In the second activity get the category id from the intent and get the details from it.

ImGenie
  • 323
  • 1
  • 15