Java 如何使用房间数据库使进度条影响UI上的数据?
我使用的是一个SQLite数据库。我希望SeekBar的进度能够影响用户界面上检索/显示的数据。我提出了以下解决方案,但担心它不可扩展。我认为,如果seekbar的progress值能够查询数据库本身并返回数据,而不是遍历所有检索到的数据,排除或包含给定条件的数据,那就更好了Java 如何使用房间数据库使进度条影响UI上的数据?,java,android,sqlite,android-room,Java,Android,Sqlite,Android Room,我使用的是一个SQLite数据库。我希望SeekBar的进度能够影响用户界面上检索/显示的数据。我提出了以下解决方案,但担心它不可扩展。我认为,如果seekbar的progress值能够查询数据库本身并返回数据,而不是遍历所有检索到的数据,排除或包含给定条件的数据,那就更好了 public class DetailsFirstTabFragment extends Fragment { private DetailsFirstTabViewModel viewModel; pr
public class DetailsFirstTabFragment extends Fragment {
private DetailsFirstTabViewModel viewModel;
private RecyclerView recentThings;
private TextView progressTextView;
final RecentThings adapter = new RecentThings();
private List<ThingEntity> thingEntities;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewModel =
ViewModelProviders.of(this).get(DetailsFirstTabViewModel.class);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_first_page_expense_details, container, false);
recentThings = root.findViewById(R.id.recycler_view_recent_things);
recentThings.setLayoutManager(new LinearLayoutManager(getContext()));
recentThings.setHasFixedSize(true);
recentThings.setAdapter(adapter);
final float[] totalSum = {0};
final TextView textViewTotalSum = root.findViewById(R.id.textViewStuffTotalSum);
SeekBar seekBar = root.findViewById(R.id.seekBarStuffDetails);
seekBar.setOnSeekBarChangeListener(seekBarChangeListener);
progressTextView = root.findViewById(R.id.textViewProgress);
viewModel.getAllThings().observe(this, new Observer<List<ThingEntity>>() {
// I wonder if the progress bar value could be passed here to call my repository
@Override
public void onChanged(List<ThingEntity> thingEntities) {
for (ThingEntity thing : thingEntities) {
totalSum[0] = totalSum[0] + thing.getThingAmount();
}
textViewTotalSum.setText(String.valueOf(totalSum[0]));
adapter.setThingEntityList(thingEntities);
setThingEntities(thingEntities);
}
});
return root;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Bundle args = getArguments();
}
public void setThingEntities(List<ThingsEntity> thingEntities) {
this.thingEntities = thingEntities;
}
SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
int progressValue = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
String progressText = "Date " + String.valueOf(progress);
progressValue = progress;
progressTextView.setText(progressText);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// this is the current implementation. It simply loops over every thing queried from database and sends the result to the adapter for drawing on the UI.
List<ThingEntity> shownThings = new ArrayList<>();
for (ThingEntity entity : thingEntities) {
if (entity.getThingAmount() > progressValue) {
shownThings.add(entity);
}
}
adapter.setThingEntityList(shownThings);
}
};
}
将您的
dao
的getAllThings
更改为接受参数
@Query("SELECT * FROM things_table WHERE thingAmount >:minValue ORDER BY thingDate ASC")
LiveData<List<ThingEntity>> getAllThings(int minValue);
public class DetailsFirstTabViewModel extends AndroidViewModel {
private MutableLiveData<List<ThingEntity>> things = new MutableLiveData<>();
...
public MutableLiveData<List<ThingEntity>> getThings() {
return things;
}
public void getAllThings(int minValue) {
things.postValue(repository.getAllThings(minValue).getValue());
}
}
并通过onStopTrackingTouch
调用progressValue
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//Just call to fetch content with progressValue and update adapter inside onChanged
viewModel.getAllThings(progressValue);
}
添加您的
viewModel
和Dao
implementation@Md.Asaduzzaman添加了viewModel和Dao。应该可以正常工作。在查询中未发现任何问题。您是否也可以发布ThingEntity
和存储库
?
public LiveData<List<ThingEntity>> getSpecifiedThings(int minValue) {
specifiedThings = thingDao.getSpecificThings(minValue);
Log.d(TAG, "getSpecifiedThings: " + specifiedThings.getValue()); // returns null
return specifiedThings;
}
private MutableLiveData<List<ThingEntity>> thingsList = new MutableLiveData<>();
public MutableLiveData<List<ThingEntity>> getThingsList() {
return thingsList;
}
public void getSpecifiedThings(int minValue) {
thingsList.postValue(repository.getSpecifiedThings(minValue).getValue());
}
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
....
Log.d(TAG, "onStopTrackingTouch: " + viewModel.getAllThings().getValue()); // returns null
viewModel.getSpecifiedThings(1); //this should retrieve every entity in my database
viewModel.getThingsList().observe(this, new Observer<List<ThingEntity>>() {
@Override
public void onChanged(List<ThingEntity> thingEntities) {
if (thingEntities != null) { // returns null
adapter.setThingEntityList(thingEntities);
}
Log.d(TAG, "onChanged: " + thingEntities); // returns null
}
});
....
}
@Entity(tableName = "things_table")
public class ThingEntity {
@PrimaryKey(autoGenerate = true)
private int id;
private Date thingDate;
private float thingAmount;
private String classification;
private String category;
private String subcategory;
private String placeName;
private int placeCode;
public ThingEntity(Date thingDate, float thingAmount, String classification,
String category, String subcategory, String placeName, int placeCode) {
this.thingDate = thingDate;
this.thingAmount = thingAmount;
this.classification = classification;
this.category = category;
this.subcategory = subcategory;
this.placeName = placeName;
this.placeCode = placeCode;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public Date getThingDate() {
return thingDate;
}
public float getThingAmount() {
return thingAmount;
}
public String getClassification() {
return classification;
}
public String getCategory() {
return category;
}
public String getSubcategory() {
return subcategory;
}
public String getPlaceName() {
return placeName;
}
public int getPlaceCode() {
return placeCode;
}
}
@Query("SELECT * FROM things_table WHERE thingAmount >:minValue ORDER BY thingDate ASC")
LiveData<List<ThingEntity>> getAllThings(int minValue);
public class DetailsFirstTabViewModel extends AndroidViewModel {
private MutableLiveData<List<ThingEntity>> things = new MutableLiveData<>();
...
public MutableLiveData<List<ThingEntity>> getThings() {
return things;
}
public void getAllThings(int minValue) {
things.postValue(repository.getAllThings(minValue).getValue());
}
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_first_page_expense_details, container, false);
....
//Observe things and update data accordingly
viewModel.getThings().observe(this, new Observer<List<ThingEntity>>() {
// I wonder if the progress bar value could be passed here to call my repository
@Override
public void onChanged(List<ThingEntity> thingEntities) {
//check null here and skip
if(thingEntities != null) {
for (ThingEntity thing : thingEntities) {
totalSum[0] = totalSum[0] + thing.getThingAmount();
}
textViewTotalSum.setText(String.valueOf(totalSum[0]));
adapter.setThingEntityList(thingEntities);
setThingEntities(thingEntities);
}
}
});
//Initially call with default min 0
viewModel.getAllThings(0);
return root;
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//Just call to fetch content with progressValue and update adapter inside onChanged
viewModel.getAllThings(progressValue);
}