我正在構建一個應用程序,它將用戶輸入的開銷存儲到SQLite數據庫中,並將它們顯示在RecyclerView中。我的應用程序目前有一個帶3個選項卡的視圖分頁器。每個選項卡切換到一個新的片段,該片段從SQLite數據庫中獲取來自不同時間段的費用,特別是「今日,本週」和所有顯示在RecyclerView中的費用。在每個列表中,我可以長按一個項目並刪除它,它將從SQLite數據庫和當前的RecyclerView中刪除它並更新列表。因爲有可能一個列表中的項目會出現在其他列表中,所以我想實現一種方法,以便我可以檢查其他兩個片段中的列表,以便刪除任何項目,並將其從中刪除,但我有難以找出最好的方法來做到這一點。使用viewpager同時更新多個相同數據的列表
現在,我可以很容易地認爲尋呼機重建每一個將再次調用數據庫,並創造全新的列表片段,但這是低效的。有沒有人對我如何實現這一目標有任何想法?下面是我當前的代碼:
ExpensePagerAdapter
class ExpensePagerAdapter extends FragmentPagerAdapter {
private String[] tabTitles = {getString(R.string.label_today),
getString(R.string.label_week), getString(R.string.label_all)};
ExpensePagerAdapter(FragmentManager fm){
super(fm);
}
@Override
public int getCount() {
return tabTitles.length;
}
@Override
public Fragment getItem(int position) {
return ExpenseListFragment.newInstance(position);
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
ExpenseListFragment
public class ExpenseListFragment extends Fragment{
private static final String LOG_TAG = "ExpenseListFragment";
private static final String ARG_TYPE = "viewType";
private RecyclerView rv;
private List<Expense> dayViewExpenses;
private List<LocalDate> weekViewDay;
private List<List<Expense>> weekViewExpenses;
private List<Expense> allExpenses;
public static ExpenseListFragment newInstance(int viewType){
Bundle bundle = new Bundle();
bundle.putInt(ARG_TYPE, viewType);
ExpenseListFragment f = new ExpenseListFragment();
f.setArguments(bundle);
return f;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.d(LOG_TAG, "onCreateView");
View v = inflater.inflate(R.layout.fragment_list, container, false);
rv = (RecyclerView) v.findViewById(R.id.rv_list);
rv.setLayoutManager(new LinearLayoutManager(getActivity()));
int viewType = getArguments().getInt(ARG_TYPE);
switch(viewType) {
case 0:
setupDayView();
break;
case 1:
setupWeekView();
break;
case 2:
setUpAllView();
break;
default:
}
return v;
}
private void setupDayView(){
dayViewExpenses = new ArrayList<>();
LocalDate currentDate = new LocalDate();
for (Expense e : ExpenseDb.getInstance(getActivity()).getAllExpenses()) {
if (currentDate.getMonthOfYear() == e.getMonth()
&& currentDate.getDayOfMonth() == e.getDay() && currentDate.getYear() == e.getYear()) {
dayViewExpenses.add(e);
}
}
rv.setAdapter(new ExpenseAdapter(getActivity(), dayViewExpenses));
}
private void setupWeekView(){
weekViewDay = new ArrayList<>();
weekViewExpenses = new ArrayList<>();
LocalDate weekStart = new LocalDate().withDayOfWeek(DateTimeConstants.MONDAY);
for (int i = 0; i < 7; i++) {
LocalDate dayOfWeek = weekStart.plusDays(i);
weekViewDay.add(dayOfWeek);
List<Expense> expensesForDay = new ArrayList<>();
for (Expense expense: ExpenseDb.getInstance(getActivity()).getAllExpenses()) {
if (expense.getDay() == dayOfWeek.getDayOfMonth() && expense.getMonth() == dayOfWeek.getMonthOfYear()
&& expense.getYear() == dayOfWeek.getYear()) {
expensesForDay.add(expense);
}
}
weekViewExpenses.add(expensesForDay);
}
rv.setAdapter(new WeekViewAdapter(getActivity(), weekViewDay, weekViewExpenses));
}
private void setUpAllView(){
allExpenses = ExpenseDb.getInstance(getActivity()).getAllExpenses();
rv.setAdapter(new ExpenseAdapter(getActivity(), allExpenses));
}
}
任何幫助將是巨大的,因爲我一直在努力找出一種理想的方式來完成這個。
感謝您的回覆,我將着眼於此並更新您的行動方式。 – BroccolliBeef