0

I have a JSON file with categories and I have showed it in a list view and I have another JSON file that contains the "sub-categories" of the specific item that is clicked. I want to show the item that has been clicked in a seconed activity with the the "sub-categories".

Here Is My Code

public class MainActivity extends AppCompatActivity {
   private GridView listview_category;

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

        listview_category = (GridView) findViewById(R.id.listview_category);
    }

            public class JsonTask extends AsyncTask<String,String,List<MainCategory>>{

                @Override
                protected List<MainCategory> doInBackground(String... params) {
                    HttpURLConnection connection = null;
                    BufferedReader reader = null;
                    URL url = null;
                    try {
                        url = new URL(params[0]);
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }

                    try {
                        connection = (HttpURLConnection) url.openConnection();
                        connection.connect();
                        InputStream stream = connection.getInputStream();
                        reader = new BufferedReader(new InputStreamReader(stream));
                        StringBuffer buffer = new StringBuffer();
                        String line = "";
                        while ((line = reader.readLine()) != null) {
                            buffer.append(line);
                        }

                        String finalJson = buffer.toString();
                        JSONObject parentObject = new JSONObject(finalJson);
                        JSONArray parentArray = parentObject.getJSONArray("results");

                        List<MainCategory> mainCategorieList = new ArrayList<>();
                          for (int i = 0; i < parentArray.length(); i++) {
                              JSONObject finalObject = parentArray.getJSONObject(i);
                              MainCategory mainCategory = new MainCategory();
                                mainCategory.setName(finalObject.getString("name"));
                                mainCategory.setOrdering(finalObject.getInt("ordering"));
                                mainCategory.setId(finalObject.getInt("id"));
                                mainCategory.setImages(finalObject.getString("images"));




                              mainCategorieList.add(mainCategory);


                          }
                        return mainCategorieList;


                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    } finally {
                        if (connection != null) {
                            connection.disconnect();
                        }
                        try {
                            if(reader!= null) {
                                reader.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    return null;
                }
                @Override
                protected void onPostExecute(List<MainCategory> result) {
                    super.onPostExecute(result);

                    categoryAdapter adapter = new categoryAdapter(getApplicationContext() , R.layout.category_row , result);
                    listview_category.setAdapter(adapter);

                }
            }

        public class categoryAdapter extends ArrayAdapter{


        private List<MainCategory> categoryList;
        private int resource;
        private LayoutInflater inflater;

        public categoryAdapter(Context context, int resource, List<MainCategory> objects) {
            super(context, resource, objects);
            categoryList = objects;
            this.resource = resource;
            inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        }

        @NonNull
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null){
                convertView = inflater.inflate(resource , null);
            }
            ImageView imageView;
            TextView textView;

            imageView = (ImageView)convertView.findViewById(R.id.imageView_category);
            textView = (TextView) convertView.findViewById(R.id.textView_categoryname);

            Glide.with(getContext()).load(categoryList.get(position).getImages()).into(imageView);
            textView.setText(categoryList.get(position).getName());



            return convertView;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu , menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if(id == R.id.action_refresh){
            new JsonTask().execute("https://api.myjson.com/bins/bzs57");
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Here is example of the "Sub-category"

category 1 pressed

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Waseem Ha
  • 127
  • 3
  • 13
  • @KNeerajLal i need to get the specific clicked item and start a new activity with JSON file that contains the sun-categories of the specific item , i'll edit my post on how it would be – Waseem Ha Dec 31 '16 at 16:56
  • @KNeerajLal i add a picture , how should i do that – Waseem Ha Dec 31 '16 at 17:23
  • how will you get the list of sub categories ? based on the category id? – Saurabh Padwekar Dec 31 '16 at 17:51
  • @SaurabhPadwekar Yes , By The ID – Waseem Ha Dec 31 '16 at 17:55
  • @SaurabhPadwekar ok i did it but what should i do with the json file here is the JSON file https://api.myjson.com/bins/ldief ? – Waseem Ha Dec 31 '16 at 19:02
  • Those are the sub Category of which Main category or is it common for all? – Saurabh Padwekar Dec 31 '16 at 19:36
  • @SaurabhPadwekar the code is categories , in the picture below the code is how sub category will be when i press it , every category have a different sub category , the boss of the company sent me this JSON file to try to figure how to open a category and show the sub category here is the categories JSON https://api.myjson.com/bins/bzs57 but i dont have any idea how to open this file , and i need this job so bad – Waseem Ha Dec 31 '16 at 21:38
  • @WaseemHa You don't have anything in the category `JSON` that links to a sub category. – K Neeraj Lal Jan 01 '17 at 06:08
  • @KNeerajLal [This Link](https://api.myjson.com/bins/bzs57) Is For The categories And [This Link](https://api.myjson.com/bins/ldief) Is The Sub Categories – Waseem Ha Jan 01 '17 at 09:53

1 Answers1

0

1.Make the mainCatagoryList global

 List<MainCategory> mainCategorieList = new ArrayList<>();

2.Add OnItemClicklistener to your gridView

listview_category.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
              MainCategory mainCatagory = mainCategorieList.get(position);
              Intent intent = new Intent(this,YourActivity.class);
              intent.putExtra("categoryId",mainCatagory.getId());
              intent.putExtra("categoryName",mainCatagory.getName());
              startActivity(intent);
            }
        });

3. Retrieve the categoryId in YourActivity(The new activity that you started). And get sub-categories

int mainCategoryId = getIntent().getIntExtra(getIntent().getIntExtra("categoryId",0);
String mainCategoryName = getIntent().getStringExtra(getIntent().getIntExtra("categoryName","");
Saurabh Padwekar
  • 3,888
  • 1
  • 31
  • 37