Recyclerview不显示“警报”对话框中的数据列表

时间:2019-03-28 17:51:32

标签: android android-recyclerview

我正在做一个用来做收据的活动。活动中有一个警报对话框,此警报对话框的用途是添加要添加到收据中的物料的清单数据。我已经制作了代码并且可以运行,但是问题是当我从警报对话框添加项目时,它没有显示在“活动/收据RecyclerView”中。

有人可以修复此代码吗?

代码:

public class CreateReceiptActivity extends AppCompatActivity {

    @BindView(R.id.btn_receipt_add_item)
    ImageButton addItem;
    @BindView(R.id.receipt_view_recycler)
    RecyclerView recyclerView;

    List<ListReceiptItem> receiptItemList;
    ListReceiptItem listReceiptItem;
    ReceiptItemAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_receipt);
        ButterKnife.bind(this);

        receiptItemList = new ArrayList<>();
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ReceiptItemAdapter(this, receiptItemList);
        recyclerView.setAdapter(adapter);

        addItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LayoutInflater li = CreateReceiptActivity.this.getLayoutInflater();

                View v = li.inflate(R.layout.alertdialog_create_receipt, null);
                AlertDialog.Builder builder = new AlertDialog.Builder(CreateReceiptActivity.this);
                builder.setView(v);

                final EditText addItemType = v.findViewById(R.id.alertdialog_receipt_type);
                final EditText addItemQty = v.findViewById(R.id.alertdialog_receipt_qty);
                final EditText addItemPrice = v.findViewById(R.id.alertdialog_receipt_price);
                Button btnSubmit = v.findViewById(R.id.alertdialog_receipt_submit);



                final AlertDialog alertDialog = builder.show();
//
//                receiptItemList = new ArrayList<>();
//                listReceiptItem = new ListReceiptItem();
                btnSubmit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        final String itemType = addItemType.getText().toString();
                        final String itemQty = addItemQty.getText().toString();
                        final String itemPrice = addItemPrice.getText().toString();
                        listReceiptItem = new ListReceiptItem(itemType, itemQty, itemPrice,"0");
                        receiptItemList.add(listReceiptItem);
                        adapter = new ReceiptItemAdapter(CreateReceiptActivity.this, receiptItemList);
                        recyclerView.setAdapter(adapter);
                        alertDialog.dismiss();
                    }
                });
            }
        });
    }
}

3 个答案:

答案 0 :(得分:1)

您已经在活动中拥有适配器,因此无需再次重新初始化适配器。从buttonClick删除此代码

adapter = new ReceiptItemAdapter(CreateReceiptActivity.this, receiptItemList);
recyclerView.setAdapter(adapter);

在列表中添加新项目后,只需通知适配器即可。像这样

receiptItemList.add(listReceiptItem);
adapter.notifyDataSetChanged()

希望这会有所帮助。

答案 1 :(得分:1)

在OnClickListener事件中添加以下行

error[E0271]: type mismatch resolving `<ndarray::data_repr::OwnedRepr<f64> as ndarray::data_traits::RawData>::Elem == ndarray::ArrayBase<ndarray::data_repr::OwnedRepr<f64>, ndarray::dimension::dim::Dim<[usize; 2]>>`
    --> src\main.rs:11:12
    |
11  |     matrix += additional;
    |            ^^ expected `f64`, found struct `ndarray::ArrayBase`
    |
    = note: expected type `f64`
            found struct `ndarray::ArrayBase<ndarray::data_repr::OwnedRepr<f64>, ndarray::dimension::dim::Dim<[usize; 2]>>`
    = note: required because of the requirements on the impl of `std::ops::AddAssign` for `ndarray::ArrayBase<ndarray::data_repr::OwnedRepr<f64>, ndarray::dimension::dim::Dim<[usize; 2]>>`

error[E0277]: the trait bound `ndarray::ArrayBase<ndarray::data_repr::OwnedRepr<f64>, ndarray::dimension::dim::Dim<[usize; 2]>>: ndarray::impl_ops::ScalarOperand` is not satisfied
    --> src\main.rs:11:12
    |
11  |     matrix += additional;
    |            ^^ the trait `ndarray::impl_ops::ScalarOperand` is not implemented for `ndarray::ArrayBase<ndarray::data_repr::OwnedRepr<f64>, ndarray::dimension::dim::Dim<[usize; 2]>>`
    |
    = note: required because of the requirements on the impl of `std::ops::AddAssign` for `ndarray::ArrayBase<ndarray::data_repr::OwnedRepr<f64>, ndarray::dimension::dim::Dim<[usize; 2]>>`

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0271, E0277.
For more information about an error, try `rustc --explain E0271`.

答案 2 :(得分:0)

请在onclick侦听器中的@media (max-width:600px) { /*Your logic here*/ } 之后添加adapter.notifyDataSetChanged();