lundi 7 mars 2016

RecyclerView don't show inserted SQLite data instantly

When the user from a FragmentDialog type his data into the edittexts and press the save button, the data should instantly show in the recyclerView, but that dosn't happend. To get/show the latest data, you have to restart the app

I have used a temporary solution where I pass data from the FragmentDialog to the mainActivity via a interface and then I directly pass that data into the arraylist for the recyclerView. This work, but dosn't look like a propor solution. I wish a "correct" way to do it

I have also tried to set adapter.notifyDataSetChanged(); different places without any succses

DialogFragment class where the user type his data and is then insertet to SQLite database

public class DialogAdd extends DialogFragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final View rootView = inflater.inflate(R.layout.add_productdialog,container, false);



    name = (EditText) rootView.findViewById(R.id.dialog_productname);
    quantity = (EditText) rootView.findViewById(R.id.dialog_qantity);
    location = (EditText) rootView.findViewById(R.id.dialog_location);
    normalPrice = (EditText) rootView.findViewById(R.id.dialog_normalPrice);
    offerPrice = (EditText) rootView.findViewById(R.id.dialog_offerPrice);

    okButton = (Button) rootView.findViewById(R.id.dialog_okButton);
    okButton.getBackground().setColorFilter(Color.parseColor("#2fbd4b"), PorterDuff.Mode.MULTIPLY);
    okButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            if (name.getText().toString().isEmpty()) {
                Toast.makeText(context, "You must add a name", Toast.LENGTH_LONG).show();
            } else {


                DialogAddListener addListener = (DialogAddListener) getActivity();

                dbHelper.insertData(name.getText().toString(), quantity.getText().toString(), location.getText().toString(), normalPrice.getText().toString(), offerPrice.getText().toString());
                addListener.getDialogData(name.getText().toString(), quantity.getText().toString(), location.getText().toString(), normalPrice.getText().toString(), offerPrice.getText().toString());


                getDialog().dismiss();
            }


        }
    });

    return rootView;

}

The mainActivity class that instantiate the recyclerview,sqlite and adapters.

public class MainActivity extends AppCompatActivity implements DialogAdd.DialogAddListener{



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.shoppinglist_mainactivity);



    databaseHelper = new DatabaseHelper(this);

    addbutton = (ImageButton) findViewById(R.id.addbtn);
    addbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialogAdd = new DialogAdd();
            dialogAdd.show(getSupportFragmentManager(), "addDialog");
        }
    });



    //RecyclerView
    recyclerView = (RecyclerView)findViewById(R.id.rv_shoppinglist);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(App.getAppContex());
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(linearLayoutManager);



    initializeData();
    adapter = new ShoplistAdapter(shopListItems);
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();

}



private void initializeData(){

    shopListItems = new ArrayList<>();
    resultset = databaseHelper.getAllData();

    if (resultset.moveToFirst()){
        while(!resultset.isAfterLast()){
            shopListItems.add(new ShopListItem(resultset.getString(1), resultset.getString(2), resultset.getString(3), resultset.getString(4), resultset.getString(5)));
            resultset.moveToNext();

            totalPrice.setText("Total cost: $" + "26");
            totalItems.setText("Total items: " + resultset.getCount());
        }
    }
    resultset.close();
}


//This is only used to show the data instantly
@Override
public void getDialogData(String name, String qty, String location, String price1, String price2) {
    shopListItems.add(new ShopListItem(name, qty, location, price1, price2));
}

}

Aucun commentaire:

Enregistrer un commentaire