我需要过滤listview使用recyclerView按日期(开始日期和结束日期之间)存储在房间数据库



我在Roomdatabase中存储当前日期的金额数据。该应用程序成功地存储数据,并显示我列表中存储的项目在recylerview与当前日期。现在我需要过滤总金额的结果和我添加的日期像我想显示列表的开始日期和结束日期。我可以使用查询得到以上的和,但不能填充日期之间的列表。

我希望我描述了这个问题以便你能理解。这是我的代码
public class MainActivity extends AppCompatActivity {

TextView cash,card,tAmount,tDate;
RecyclerView recyclerView;
RoomAdapter roomAdapter;
Button btn;

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

btn =(Button)findViewById ( R.id.fbtn );
recyclerView=(RecyclerView)findViewById ( R.id.myrecView );
recyclerView.setLayoutManager ( new LinearLayoutManager (this  ) );
cash=(TextView)findViewById ( R.id.cashTxt );
card=(TextView)findViewById ( R.id.cardText );
tAmount=(TextView)findViewById ( R.id.total_amount );


btn.setOnClickListener ( new View.OnClickListener () {
@Override
public void onClick(View view)
{

MaterialDatePicker.Builder<Pair<Long, Long>> materialDateBuilder = MaterialDatePicker.Builder.dateRangePicker();
materialDateBuilder.setTitleText ( "Select date Range" );
final MaterialDatePicker materialDatePicker=materialDateBuilder.build();
materialDatePicker.show ( getSupportFragmentManager (), "MATERIAL_DATE_PICKER");
materialDatePicker.addOnPositiveButtonClickListener( new MaterialPickerOnPositiveButtonClickListener ()
{
@SuppressLint("SetTextI18n")
@Override
public void onPositiveButtonClick(Object selection) {
Pair selectedDates = (Pair) materialDatePicker.getSelection();
final Pair<Date, Date> rangeDate = new Pair<>(new Date((Long) selectedDates.first), new Date((Long) selectedDates.second));
Date startDate=rangeDate.first;
Date endDate=rangeDate.second;
//SimpleDateFormat simpleFormat = new SimpleDateFormat ("dd MMM yyyy HH:mm");
long sDates= startDate.getTime ();
long eDates=endDate.getTime ();

AppDatabase db = Room.databaseBuilder(getApplicationContext (), AppDatabase.class, "myincome").allowMainThreadQueries ().build();
UserDao userDao = db.userDao();
userDao.newAllExpensesFromTo ( sDates,eDates );
String cIncome= String.valueOf ( userDao.newAllExpensesFromTo ( sDates,eDates ) );
tAmount.setText ( cIncome );
// ArrayList<Income> incomes= new ArrayList<> (userDao.newAllExpensesFromTo ( sDates,eDates ));

ArrayList incomes=new ArrayList (userDao.newAllExpensesFromTo ( sDates,eDates ));

roomAdapter= new RoomAdapter ( incomes );
recyclerView.setAdapter ( roomAdapter );
roomAdapter.notifyDataSetChanged ();

//  Log.d ("SELECTED DATE :","Converted Dates"+  simpleFormat.format(sDates) + " Second : " + simpleFormat.format(eDates));
Log.d ("SELECTED DATE :","Converted Dates"+userDao.newAllExpensesFromTo ( sDates,eDates )+incomes.size () );
}
});


}
} );
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener ( new View.OnClickListener () {
@Override
public void onClick(View view) {
getRoomData();
getRoomDataSum();
getCashSum();
BottomSheet bottomSheet=new BottomSheet ();
bottomSheet.show ( getSupportFragmentManager () ,bottomSheet.getTag () );
}
} );
getRoomData();
getRoomDataSum();
getCashSum();
}

这是一个返回我所添加的金额总和的查询

@Query("SELECT SUM (Amount) FROM myIncome WHERE Date BETWEEN :startDate AND :endDate")
int  newAllExpensesFromTo(Long startDate,Long endDate);

这是适配器

public class RoomAdapter extends RecyclerView.Adapter<RoomAdapter.myViewHolder>{
List<Income> incomes;
Context context ;
List<Integer> customList;
public RoomAdapter(List<Income> incomes) {
this.incomes = incomes;
}

public RoomAdapter(ArrayList<Income> cincomes) {
this.incomes = cincomes;
}
@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from ( parent.getContext () ).inflate ( R.layout.fragment_blank ,parent,false);
return new myViewHolder ( view );
}
@Override
public void onBindViewHolder(@NonNull myViewHolder holder, int position)
{

Date date = incomes.get ( position ).getDate ();
DateFormat dateFormat = new SimpleDateFormat ("dd-MM-yyyy HH:mm");
String stringDate = dateFormat.format ( date );

holder.AmountType.setText ( incomes.get ( position ).getType ()  );
holder.textViewAmount.setText ( String.valueOf (  incomes.get (  position).getAmount () ));
holder.company.setText ( incomes.get ( position ).getCompany ());
String cName= (String) holder.company.getText ();

holder.dateText.setText (stringDate);
}
@Override
public int getItemCount() {
return incomes.size ();
}
class myViewHolder extends RecyclerView.ViewHolder {
TextView textViewAmount,AmountType,company,dateText;
ImageView imageView;
public myViewHolder(@NonNull View itemView) {
super ( itemView );
AmountType=itemView.findViewById ( R.id.AmountType );
textViewAmount=itemView.findViewById ( R.id.txtViewAmount );
company=itemView.findViewById ( R.id.Company );
imageView=(ImageView)itemView.findViewById ( R.id.img1 );
dateText=(TextView)itemView.findViewById ( R.id.dateText);
}
}

}

嗨,我终于找到了解决方案。也许没有人能回答我的问题。我发现的问题是在我的查询在上面的查询,我试图得到结果的总和,但我想填充列表也。但是上面的查询只会显示总和,而不会显示结果列表。

所以正确的查询是

@Query("SELECT * FROM myIncome WHERE Date BETWEEN :startDate AND :endDate")
List<Income>  newAll(Long startDate,Long endDate);

这将显示列表和列表视图之间的项目筛选日期。谢谢

相关内容

  • 没有找到相关文章

最新更新