android按ID列出列表中的项目

时间:2016-12-31 16:31:00

标签: android json listview postman

我有a JSON file类别,我已在列表视图中显示它 我有另一个JSON文件,其中包含单击的特定项的“子类别”。我想显示已使用“子类别”在seconed活动中单击的项目。

这是我的代码

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);
    }
}

以下是“子类别”

的示例

category 1 pressed

1 个答案:

答案 0 :(得分:0)

1.制作mainCatagoryList global

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

2.将OnItemClicklistener添加到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。检索YourActivity中的categoryId(您启动的新活动)。并获得子类别

int mainCategoryId = getIntent().getIntExtra(getIntent().getIntExtra("categoryId",0);
String mainCategoryName = getIntent().getStringExtra(getIntent().getIntExtra("categoryName","");