【问题标题】:OnItemclickListner Toast message not showing in androidOnItemclickListner Toast 消息未在 android 中显示
【发布时间】:2020-06-18 07:56:53
【问题描述】:

您好,在下面的代码中动态实现了自动完成 textview 并在本地保存响应。现在如果正在搜索字母“a”,那么它会显示并将 settext 设置为自动完成 textview。当我选择文本时,settext 工作正常获取 ID

Onitemclick Toast 消息未显示。

谁能帮我解决我的错误

 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(",");
        }

【问题讨论】:

  • 您好,您在哪里添加了 OnItemClickListener?它是项目选择侦听器,其工作方式不同。
  • setOnItemSelectedListener
  • 那是不同的,当你选择一些东西时它会起作用。您需要将其更改为 adapter.OnItemClickListener 。
  • 请查看我的答案,它现在应该可以解决您的问题。 :)
  • 这能回答你的问题吗? setOnItemClickListener on custom ListView

标签: android android-toast


【解决方案1】:

这是这个答案的解决方案

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

【讨论】:

  • 上面的代码给了我错误的 id 和错误的产品名称设置为 autocompletetetextview
  • 所以在这里你可以添加你的逻辑,我不知道你想在这里实现什么。代码中的If语句你可以考虑一下。在那里你可以编辑它:)
猜你喜欢
  • 2011-12-09
  • 1970-01-01
  • 2015-02-12
  • 1970-01-01
  • 1970-01-01
  • 2014-07-04
  • 2014-09-21
  • 2020-07-30
相关资源
最近更新 更多