Search code examples
androidandroid-toast

OnItemclickListner Toast message not showing in android


Hi in the below code implemented autocomplete textview dynamically and saving the response locally.Now If am search for letter 'a' then from that is it displaying and settext to autocomplete textview.setext is working fine when I am selected the text want to get the Id

Onitemclick Toast message is not displaying.

Can any one help me where I did the mistake

 private void fetchProductnameJSON(){
        autoproduct_name.setHint(Html.fromHtml ( "Item Name"+""));
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // Write code for your refresh logic

                sessionId = getActivity().getIntent().getStringExtra("sessionId");
                String operation = "syncModuleRecords";
                String module = "Products";
                String syncToken = "";
                String mode = "public";
                final GetNoticeDataService service = RetrofitInstance.getRetrofitInstance().create(GetNoticeDataService.class);
                /** Call the method with parameter in the interface to get the notice data*/
                Call<SyncModule> call = service.GetSyncModuleList(operation, sessionId, module, syncToken, mode);

                /**Log the URL called*/
                Log.i("URL Called", call.request().url() + "");

                call.enqueue(new Callback<SyncModule>() {
                    @Override
                    public void onResponse(Call<SyncModule> call, Response<SyncModule> response) {

                        Log.e("response", new Gson().toJson(response.body()));
                        if (response.isSuccessful()) {
                            Log.e("response", new Gson().toJson(response.body()));

                            SyncModule syncModule = response.body();
                            Gson g = new Gson();
                            String jsonAllProducts = g.toJson(syncModule);
                            tinydb.putString("jsonAllProducts",jsonAllProducts);
                            workingOnResponseProduct(syncModule);
                        }
                    }

                    @Override
                    public void onFailure(Call<SyncModule> call, Throwable t) {

                    }
                    //     progressDialog.dismiss();
                });
            }
        }, 0);
        return ;
    }

    private void workingOnResponseProduct(SyncModule syncModule) {
        autoproduct_name.setHint(Html.fromHtml ( "Item Name"+""));
        String success = syncModule.getSuccess();

        if (success.equals("true")) {

            SyncResults results = syncModule.getResult();

            Sync sync = results.getSync();

            ArrayList<SyncUpdated> syncUpdateds = sync.getUpdated();

            for (SyncUpdated syncUpdated : syncUpdateds) {

                String unitprice="";
                product_id = syncUpdated.getId();
                ArrayList<SyncBlocks> syncBlocks = syncUpdated.getBlocks();
                for (SyncBlocks syncBlocks1 : syncBlocks) {
                    String label = syncBlocks1.getLabel();
                    //Basic Information
                    if (label.equals("Product Details")) {
                        ArrayList<SynFields> synFields = syncBlocks1.getFields();

                        for (SynFields synFields1 : synFields) {

                            String name = synFields1.getName();
                            values = synFields1.getValue();

                            if (name.equals("productname")) {
                                productname = String.valueOf(values);
                                product_name.add(productname);
                                Log.d("products",String.valueOf(product_name.size()));
                                RecordsProducts records = new RecordsProducts(product_id,productname);
                                recordsListProduct.add(records);

                            }
                        }
                    }
                    //pricing information
                    else if (label.equals("Pricing Information")) {
                        ArrayList<SynFields> synFields = syncBlocks1.getFields();

                        for (SynFields synFields1 : synFields) {

                            String name = synFields1.getName();
                            values = synFields1.getValue();

                            if (name.equals("unit_price")) {
                                unitprice = String.valueOf(values);
                                Log.d("unitprice",unitprice);
                                //

                            }
                        }
                    }
                }

            }
        }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1, product_name);
            autoproduct_name.setAdapter(adapter);

            autoproduct_name.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    if(position+1 > 0) {
                        RecordsProducts recordsProducts=recordsListProduct.get(position);
                        String product_id=recordsProducts.getId();
                        Log.d("productid",product_id);
                        String productnames=recordsProducts.getProductname();
                        autoproduct_name.setText(productnames);
                        Toast.makeText(getContext(),"selected"+product_id,Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                }
            });
                TableRow tbrow0 = new TableRow(getContext());
                Resources resource = getContext().getResources();
                tbrow0.setLayoutParams(getLayoutParams());
                tbrow0.addView(getTextView(0, "Product Name", Color.WHITE, Typeface.NORMAL, resource.getColor(R.color.tabs1)));
                tbrow0.addView(getTextView(0, "Quantity", Color.WHITE, Typeface.NORMAL,resource.getColor(R.color.tabs1)));
                tbrow0.addView(getTextView(0, "Unit Price", Color.WHITE, Typeface.NORMAL, resource.getColor(R.color.tabs1)));
                tbrow0.addView(getTextView(0, "Total", Color.WHITE, Typeface.NORMAL, resource.getColor(R.color.tabs1)));
                stk.addView(tbrow0,getLayoutParams());
                //String[] namesList = p.split(",");
                //String[] priceList = listprices.split(",");
        }

Solution

  • Here is the solution from this answer

    https://stackoverflow.com/a/4752309/3811162

    autoproduct_name.setOnItemClickListener(new AdapterView.OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         if(position+1 > 0) {
                        RecordsProducts recordsProducts=recordsListProduct.get(position);
                        String product_id=recordsProducts.getId();
                        Log.d("productid",product_id);
                        String productnames=recordsProducts.getProductname();
                        autoproduct_name.setText(productnames);
                        Toast.makeText(getContext(),"selected"+product_id,Toast.LENGTH_LONG).show();
                    }
       } 
    });