当微调器在listview适配器中显示时,按钮无法隐藏

时间:2017-07-18 08:04:47

标签: android listview button adapter hidden

我在适配器列表视图中有一个代码。我想在显示列表视图时隐藏“image2”按钮,但它不起作用。我的错在哪里?有人可以帮帮我吗?

@Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        Database = new SQLite(context);
        db = Database.getReadableDatabase();

        if (convertView == null) {
            inflater = context.getLayoutInflater();
            convertView = inflater.inflate(resource, null);
        }

        final Model_CheckStock itempos = ListViewCheckStock.get(position);

        TextView txproduk = (TextView) convertView.findViewById(R.id.txproduk);
        final TextView txqty = (TextView) convertView.findViewById(R.id.txqty);
        final TextView txexpired = (TextView) convertView.findViewById(R.id.txexpired);
        TextView txidproduk = (TextView) convertView.findViewById(R.id.txidproduk);
        final Spinner spin = (Spinner) convertView.findViewById(R.id.spin);

        ImageView imgexpired = (ImageView) convertView.findViewById(R.id.imgexpired);
        Button image = (Button) convertView.findViewById(R.id.image);
        final Button image2 = (Button) convertView.findViewById(R.id.image2);

        txproduk.setText(ListViewCheckStock.get(position).gettxproduk());
        txidproduk.setText("Produk " + ListViewCheckStock.get(position).getjnsprod());
        txqty.setText(ListViewCheckStock.get(position).gettxqty());
        txexpired.setText(ListViewCheckStock.get(position).getexpired());

//        image2.setVisibility(View.INVISIBLE);
        if (txidproduk.getText().toString().contains("NON")) {
            Log.w("ADAPTERCHECKSTOCKLIST", "getView non: "+txidproduk.getText().toString() );
            List<Model_Unit> chekstok = Database.getUnitAll();
            ArrayAdapter<Model_Unit> dataAdapter = new ArrayAdapter<Model_Unit>(
                    context, android.R.layout.simple_spinner_item, chekstok);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spin.setAdapter(dataAdapter);
            spin.setSelection(getIndex(spin, ListViewCheckStock.get(position).getunit()));
            image2.setVisibility(View.INVISIBLE);
            notifyDataSetChanged();
        } else {
            Log.w("ADAPTERCHECKSTOCKLIST", "getView sgf: "+txidproduk.getText().toString() );
            List<Model_Unit> chekstok = Database.getUnit(ListViewCheckStock.get(position).getidprod());
            ArrayAdapter<Model_Unit> dataAdapter = new ArrayAdapter<Model_Unit>(
                    context, android.R.layout.simple_spinner_item, chekstok);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spin.setAdapter(dataAdapter);
            spin.setSelection(getIndex(spin, ListViewCheckStock.get(position).getunit()));
            image2.setVisibility(View.INVISIBLE);
            notifyDataSetChanged();
        }
        imgexpired.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                DatePickerDialog();
                myCalendar = Calendar.getInstance();
                dialog = new DatePickerDialog(context,
                        dateListener, year, month, day);
                dialog.show();
                image2.setVisibility(View.VISIBLE);

            }
        });

        image2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                android.app.AlertDialog.Builder dialog = new android.app.AlertDialog.Builder(
                        context);
                dialog.setMessage("Anda Yakin Ingin Menyimpan Perubahan Data Ini ?");
                dialog.setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                String saving;
                                saving = String.valueOf((ListViewCheckStock.get(position).getidtable()));
                                System.out.println("saving" + saving);

                                db = Database.getWritableDatabase();
                                Database.updateSavingCheckstok(ListViewCheckStock.get(position).getunit(), saving, ListViewCheckStock.get(position).gettxqty(), ListViewCheckStock.get(position).getexpired());
                               image2.setVisibility(View.GONE);
                            }
                        });
                dialog.setNegativeButton("Kembali",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                dialog.show();

            }
        });


        spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view,
                                       int arg2, long id) {

                if ((txqty.getText().toString().contains(".") && !spin.getSelectedItem().toString().equals("CAR")) &&
                        (txqty.getText().toString().contains(".") && !spin.getSelectedItem().toString().equals("KG"))) {
                    CustomDialog.init.setDialog(context,
                            "Format angka", "Desimal tidak diperbolehkan dalam unit ini.",
                            "ok", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    txqty.setText("");
                                }
                            });
                }
                String prod;

                ListViewCheckStock.get(position).setunit(adapterView.getSelectedItem().toString());

                Log.w("TAG >>", "onItemSelected: " + ListViewCheckStock.get(position).getunit());

               spin.setSelection(arg2);
                image2.setVisibility(View.VISIBLE);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }

        });


    private int getIndex(Spinner spinner, String myString) {
        int index = -1;

        for (int i = 0; i < spinner.getCount(); i++) {
            if (spinner.getItemAtPosition(i).toString()
                    .equalsIgnoreCase(myString)) {
                index = i;
                break;
            }
        }
        return index;
    }

}

1 个答案:

答案 0 :(得分:0)

我希望在列表视图显示时隐藏保存按钮(image2)。但按钮始终显示。

然后,当flagstatus不为0时,按钮仍会显示。

enter image description here