當用戶從FragmentDialog
輸入他的數據到edittext並按下保存按鈕時,數據應立即顯示在recyclerView
,但該dosn't發生。要獲得/顯示最新的數據,你必須重啓應用RecyclerView不會立即顯示插入的SQLite數據
我使用了一個臨時的解決方案,我通過一個接口傳遞從FragmentDialog
到mainActivity
數據,然後我直接將數據傳遞到ArrayList中的recyclerView。這項工作,但似乎不像一個propor解決方案。我希望有一個「正確」的方式來做到這一點
我也試圖設置adapter.notifyDataSetChanged();不同的地方,沒有任何succses
DialogFragment類當用戶鍵入他的數據並隨後insertet到SQLite數據庫
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;
}
該實例化recyclerview,SQLite和適配器 MainActivity類別。
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));
}
}
我剛剛實施辦法1#從這個例子: http://www.androiddesignpatterns.com/2012/05/correctly-managing-your-sqlite-database.html 但它不到風度說一下什麼觀察員。我應該在單身法中註冊一個Observer嗎? – Muddz
@Muddz我已經更新了我的答案。 –