为什么我的ExpandableListVIew没有显示

时间:2015-10-23 09:45:44

标签: android expandablelistview

我希望ExpandableListView点击{{1>}点击 amozon.in 应用。我给出一个参考图像: -

enter image description here

但我点击textview的申请未显示

我的ProductsCategory.java: -

expandablelistview

我的xml: -

public class ProductsCategory extends AppCompatActivity {

    private ListView listView;
    private GridView gridView;
    ProductsAdapter  adapter;
    ProductsAdapterGridView adapterGridView;
    ArrayList<Products> productsList = new ArrayList<>();
    String menu_id;
    TextView textView,textViewfilter;

    // Filter Class
    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader = new ArrayList<>();
    HashMap<String, List<String>> listDataChild = new HashMap<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.produts_category);

        Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setTitle("Shopping Mazza");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        Intent i = getIntent();
        menu_id = i.getStringExtra("category");
        adapter = new ProductsAdapter(ProductsCategory.this,R.layout.product_page_list_view,productsList);
        adapterGridView = new ProductsAdapterGridView(ProductsCategory.this,R.layout.products_page_grid_view,productsList);
        listView = (ListView)findViewById(R.id.list_product);
        gridView = (GridView)findViewById(R.id.list_product_grid);
        textView = (TextView)findViewById(R.id.change_view);
        textViewfilter = (TextView)findViewById(R.id.textView);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listView.getVisibility()==View.VISIBLE){
                    listView.setVisibility(View.GONE);
                    gridView.setVisibility(View.VISIBLE);
                }
                else {
                    listView.setVisibility(View.VISIBLE);
                    gridView.setVisibility(View.GONE);
                }
            }
        });

        textViewfilter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                expListView = (ExpandableListView) findViewById(R.id.filter_expandable_list_view);
                expListView.setVisibility(View.VISIBLE);

                // preparing list data
                //    prepareListData();
                new PrepareListData().execute("http://opencart.codeniques.com/shopping/?route=feed/web_api/filters&key=test123$");





                // Listview Group click listener
                expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

                    @Override
                    public boolean onGroupClick(ExpandableListView parent, View v,
                                                int groupPosition, long id) {
                        // Toast.makeText(getApplicationContext(),
                        // "Group Clicked " + listDataHeader.get(groupPosition),
                        // Toast.LENGTH_SHORT).show();
                        return false;
                    }
                });

                // Listview Group expanded listener
                expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

                    @Override
                    public void onGroupExpand(int groupPosition) {
                        Toast.makeText(getApplicationContext(),
                                listDataHeader.get(groupPosition) + " Expanded",
                                Toast.LENGTH_SHORT).show();
                    }
                });

                // Listview Group collasped listener
                expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

                    @Override
                    public void onGroupCollapse(int groupPosition) {
                        Toast.makeText(getApplicationContext(),
                                listDataHeader.get(groupPosition) + " Collapsed",
                                Toast.LENGTH_SHORT).show();

                    }
                });

                // Listview on child click listener
                expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

                    @Override
                    public boolean onChildClick(ExpandableListView parent, View v,
                                                int groupPosition, int childPosition, long id) {

                        Toast.makeText(
                                getApplicationContext(),
                                listDataHeader.get(groupPosition)
                                        + " : "
                                        + listDataChild.get(
                                        listDataHeader.get(groupPosition)).get(
                                        childPosition), Toast.LENGTH_SHORT)
                                .show();
                        return false;
                    }
                });

             /*   Intent i = new Intent(ProductsCategory.this,FilterClass.class);
                i.putExtra("category_id",menu_id);
                startActivity(i);*/
            }
        });

        new Product().execute("http://opencart.codeniques.com/shopping/?route=feed/web_api/products&key=test123$");
    }
    public class PrepareListData extends AsyncTask<String,Void,Void>{
        ProgressDialog dialog;
        @Override
        protected synchronized void onPreExecute() {
            super.onPreExecute();
            dialog = new ProgressDialog(ProductsCategory.this);
            dialog.setCancelable(false);
            dialog.show();
        }

        @Override
        protected synchronized  Void doInBackground(String... params) {
            try {
                HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost(params[0]);
                List<NameValuePair> nameValuePairs = new ArrayList<>(1);

                nameValuePairs.add(new BasicNameValuePair("category",menu_id));
                post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = client.execute(post);

                if(response.getStatusLine().getStatusCode()==200){

                    HttpEntity entity = response.getEntity();
                    String data = EntityUtils.toString(entity);

                    JSONObject jsonObject = new JSONObject(data);
                    JSONArray jsonArray = jsonObject.getJSONArray("filters");

                    //     Log.d("jsonarray",jsonArray.length()+"");

                    Log.d("filter",jsonArray+"");

                    for(int i=0;i<jsonArray.length();i++){

                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);

                        String s = jsonObject1.getString("filter_group_id");
                        listDataHeader.add(jsonObject1.getString("name"));

                        JSONArray jsonArray1 = jsonObject1.getJSONArray("filter");

                        List<String> firstlevel = new ArrayList<>();
                        for(int j=0;j<jsonArray1.length();j++){

                            JSONObject jsonObject11 = jsonArray1.optJSONObject(j);

                            String s1 = jsonObject11.getString("filter_id");
                            String colorcode = jsonObject11.getString("colorcode");
                            firstlevel.add(jsonObject11.getString("name"));
                        }
                        listDataChild.put(listDataHeader.get(i), firstlevel);
                        Log.d("listDataHeader", listDataHeader.get(i)+"");
                        Log.d("value of", firstlevel + "");
                        Log.d("hash map", listDataChild.size() + "");
                        //  firstlevel.clear();
                    }
                }
            }catch (IOException |JSONException e){
                Log.e("Error :", e.getMessage());
            }
            return null;
        }
        @Override
        protected synchronized void onPostExecute(Void aVoid) {
            //  super.onPostExecute(aVoid);
            dialog.dismiss();
            listAdapter = new ExpandableListAdapter(ProductsCategory.this, listDataHeader, listDataChild);
            // setting list adapter
            expListView.setAdapter(listAdapter);

        }
    }
    public class Product extends AsyncTask<String,Void,Boolean>{
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Boolean doInBackground(String... params) {
            try {
                HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost(params[0]);
                List<NameValuePair> nameValuePairs = new ArrayList<>(1);

                nameValuePairs.add(new BasicNameValuePair("category",menu_id));
                post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = client.execute(post);

                int status = response.getStatusLine().getStatusCode();
                if(status==200){
                    HttpEntity entity = response.getEntity();
                    String data = EntityUtils.toString(entity);

                    JSONObject jsonObject = new JSONObject(data);
                    JSONArray jsonArray = jsonObject.getJSONArray("products");

                    for(int i=0;i<jsonArray.length();i++){

                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);

                         Products product = new Products();

                        product.setId(jsonObject1.getString("id"));
                        product.setName(jsonObject1.getString("name"));
                        product.setPirce(jsonObject1.getString("pirce"));
                        product.setDiscountprice(jsonObject1.getString("discountprice"));
                        product.setHref(jsonObject1.getString("href"));
                        product.setThumb(jsonObject1.getString("thumb"));
                        product.setSpecial(jsonObject1.getString("special"));
                        product.setRating(jsonObject1.getString("rating"));

                        productsList.add(product);
                    }
                    return true;
                }

            }catch (IOException |JSONException e){
                Log.e("Error :", e.getMessage());
            }
            return null;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
          //  super.onPostExecute(aBoolean);
            adapter.notifyDataSetChanged();
            if(!aBoolean){
                Toast.makeText(ProductsCategory.this, "Data is not Parsed", Toast.LENGTH_LONG).show();
            }
            else {
                ProductsAdapter adapter = new ProductsAdapter(getApplicationContext(),R.layout.product_page_list_view,productsList);
                listView.setAdapter(adapter);

                ProductsAdapterGridView adapterGridView = new ProductsAdapterGridView(getApplicationContext(),R.layout.products_page_grid_view,productsList);
                gridView.setAdapter(adapterGridView);

            }
        }
    }
    public boolean onCreateOptionsMenu(android.view.Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
}

我是android开发的新手。谢谢你!

0 个答案:

没有答案