Justin jant
Justin jant

Reputation: 75

Recyclerview doesn't show list of data from Alert Dialog

I am making a activity which is use to make a receipt. The Activity have a alertdialog in it, the use of this alertdialog is to add List data of item that will be added to Receipt. I already make the code and it runs but the problem is when I add item from the alert dialog, it doesn't show in the Activity/Receipt RecyclerView.

can somebody fix this code ?

Code:

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

Upvotes: 1

Views: 397

Answers (3)

user13898897
user13898897

Reputation:

Add below lines in OnClickListener event

adapter.notifyDataSetChanged()

Upvotes: 1

amitava
amitava

Reputation: 505

Please add adapter.notifyDataSetChanged(); after recyclerView.setAdapter(adapter); inside onclick listener.

Upvotes: 0

Faysal Ahmed
Faysal Ahmed

Reputation: 7669

You already have an adapter in the activity then no need to reinitialise adapter again. Remove this code from buttonClick

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

After adding new item on the list just notify the adapter. Like this

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

Hope this will help.

Upvotes: 1

Related Questions