androidstudio中的if语句有一个意外的结果,为什么?

yhuiod9q  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(306)

在我的可展开列表视图中,我编写了(if)语句,如果子行中包含两个单词,它将返回(dialog)。。
代码如下:

.......
    childObjects.add(new ChildObject(cetamol + " " + syr + " 160 " + mg5ml
                , cetamolDoseMini160));
    ..........

    elv = findViewById(R.id.elv);
    elv.setOnGroupExpandListener(onGroupExpandListenser);

    MyExpandableAdapter adapter = new MyExpandableAdapter(this, getData());
    elv.setAdapter(adapter);

    elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

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

            for (int i = 0; i < ((ViewGroup) v).getChildCount(); ++i) {
                View nextChild = ((ViewGroup) v).getChildAt(i);
                if (nextChild.getId() == R.id.txtChildName) {
                    String data = ((TextView) nextChild).getText().toString();

                    String paracetamol = getResources().getString(R.string.Paracetamol);

                    if (data.contains(paracetamol)|| data.contains("160")) {

                        AlertDialog.Builder builder = new AlertDialog.Builder(Analgesics.this);

                        builder.setTitle(" Paracetamol 160:");

                        String[] drugsTrades = {"Hayamol 160 ", "Paradrin 160 "};

                        builder.setItems(drugsTrades, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                        AlertDialog dialog = builder.create();
                        // dialog.getWindow().setBackgroundDrawableResource(R.color.translucent_black);
                        dialog.show();
                    }

                }
            }
                return true;
            }
    });

但是(if语句)只看到了第一部分“扑热息痛”,没有看到第二部分“160”

juzqafwq

juzqafwq1#

用这个代码替换整个代码。

.......
childObjects.add(new ChildObject(cetamol + " " + syr + " 160 " + mg5ml
            , cetamolDoseMini160));
..........

elv = findViewById(R.id.elv);
elv.setOnGroupExpandListener(onGroupExpandListenser);

MyExpandableAdapter adapter = new MyExpandableAdapter(this, getData());
elv.setAdapter(adapter);

elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

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

        for (int i = 0; i < ((ViewGroup) v).getChildCount(); ++i) {
            View nextChild = ((ViewGroup) v).getChildAt(i);
            if (nextChild.getId() == R.id.txtChildName) {
                String data = ((TextView) nextChild).getText().toString();

                String paracetamol = getResources().getString(R.string.Paracetamol);

                if (data.contains(paracetamol) && data.contains("160")) {

                    AlertDialog.Builder builder = new AlertDialog.Builder(Analgesics.this);

                    builder.setTitle(" Paracetamol 160:");

                    String[] drugsTrades = {"Hayamol 160 ", "Paradrin 160 "};

                    builder.setItems(drugsTrades, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    });
                    AlertDialog dialog = builder.create();
                    // dialog.getWindow().setBackgroundDrawableResource(R.color.translucent_black);
                    dialog.show();
                }

            }
        }
            return true;
        }
});

相关问题