如何使用行 ID 将数据加载到详细信息 activity 中?
how to load data into a detail activity using row ID?
我已经使用 android 房间架构构建了房间数据库。现在,当用户点击其中一个回收站项目时,从回收站视图片段中,应用会将他带到该项目的详细信息 activity。
现在我使用以下代码从片段发送项目 ID:
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(mContext.getContext(), MaterialItemView.class);
intent.putExtra("ID", String.valueOf(mMaterial.get(position).getId()));
mContext.startActivity(intent);
}
它正在工作,因为 ID 到达了另一边。
我需要帮助使用 ID 将此数据解析或加载到详细信息 activity。
这是我的详细信息 Activity:
TextView mMaterialName;
TextView mMaterialBrand;
private RawMaterialViewModel mMaterialViewModel;
private List<RawMaterialsEntity> mMaterial; // Cached copy of Materials
RawMaterialsEntity rawMaterialsEntity;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.material_item_view);
mMaterialName = findViewById(R.id.material_name_view);
mMaterialBrand = findViewById(R.id.material_brand_view);
mMaterialViewModel = new ViewModelProvider(this).get(RawMaterialViewModel.class);
// Update the cached copy of the words in the adapter.
Intent intent = getIntent();
if (intent != null && intent.hasExtra("ID")) {
int id = intent.getIntExtra("ID", -1);
// TODO: get material details based on material id
} else {
// ...
}
这是我的道:
@Dao
public interface RawMaterialsDao {
@Insert
void insertMaterial(RawMaterialsEntity rawMaterialsEntity);
@Query("DELETE FROM raw_materials")
void deleteAll();
@Query("SELECT * FROM raw_materials WHERE material_name = :name")
List<RawMaterialsEntity> findMaterial(String name);
@Query("SELECT * FROM raw_materials WHERE id = :id")
List<RawMaterialsEntity> findMaterialById(int id);
@Query("DELETE FROM raw_materials WHERE material_name = :name")
void deleteMaterial(String name);
@Query("SELECT * from raw_materials")
LiveData<List<RawMaterialsEntity>> getAllMaterials();
@Query ("UPDATE raw_materials SET cost_per_gm = material_cost/material_weight")
void divide();
@Query ("UPDATE raw_materials SET total_cost = material_cost*material_quantity")
void totalCost();
}
我的存储库:
public class ChefsBoxRepository {
private MutableLiveData<List<RawMaterialsEntity>> searchResults = new MutableLiveData<>();
private LiveData<List<RawMaterialsEntity>> allMaterials;
private RawMaterialsDao rawMaterialDao;
//public constructor
public ChefsBoxRepository(Application application) {
ChefsBoxRoomDataBase db;
db = ChefsBoxRoomDataBase.getDatabase(application);
rawMaterialDao = db.rawMaterialDao();
allMaterials = rawMaterialDao.getAllMaterials();
}
//used by the view model
public LiveData<List<RawMaterialsEntity>> getAllMaterials() {
return allMaterials;
}
//used by the view model
public MutableLiveData<List<RawMaterialsEntity>> getSearchResults() {
return searchResults;
}
public void insertMaterial(RawMaterialsEntity newMaterial) {
InsertAsyncTask task = new InsertAsyncTask(rawMaterialDao);
task.execute(newMaterial);
}
public void costPerGm(){
CostPerGmAsyncTask task = new CostPerGmAsyncTask(rawMaterialDao);
task.execute();
}
public void totalCost(){
TotalCostAsyncTask task = new TotalCostAsyncTask(rawMaterialDao);
task.execute();
}
public void deleteMaterial(String name) {
DeleteAsyncTask task = new DeleteAsyncTask(rawMaterialDao);
task.execute(name);
}
public void findMaterial(String name) {
QueryAsyncTask task = new QueryAsyncTask(rawMaterialDao);
task.delegate = this;
task.execute(name);
}
public void findMaterialById(int id) {
QueryAsyncTask2 task = new QueryAsyncTask2(rawMaterialDao);
task.delegate = this;
task.execute(id);
}
private void asyncFinished(List<RawMaterialsEntity> results) {
searchResults.setValue(results);
}
//QueryAsyncTask for calling items from database
private static class QueryAsyncTask extends
AsyncTask<String, Void, List<RawMaterialsEntity>> {
private RawMaterialsDao asyncTaskDao;
private ChefsBoxRepository delegate = null;
QueryAsyncTask(RawMaterialsDao dao) {
asyncTaskDao = dao;
}
@Override
protected List<RawMaterialsEntity> doInBackground(final String... params) {
return asyncTaskDao.findMaterial(params[0]);
}
@Override
protected void onPostExecute(List<RawMaterialsEntity> result) {
delegate.asyncFinished(result);
}
}
//QueryAsyncTask for calling items from database
private static class QueryAsyncTask2 extends
AsyncTask<Integer, Void, List<RawMaterialsEntity>> {
private RawMaterialsDao asyncTaskDao;
private ChefsBoxRepository delegate = null;
QueryAsyncTask2(RawMaterialsDao dao) {
asyncTaskDao = dao;
}
@Override
protected List<RawMaterialsEntity> doInBackground(final Integer... params) {
return asyncTaskDao.findMaterialById(params[0]);
}
@Override
protected void onPostExecute(List<RawMaterialsEntity> result) {
delegate.asyncFinished(result);
}
}
//InsertAsyncTask for inserting items to the database
private static class InsertAsyncTask extends AsyncTask<RawMaterialsEntity, Void, Void> {
private RawMaterialsDao asyncTaskDao;
InsertAsyncTask(RawMaterialsDao dao) {
asyncTaskDao = dao;
}
@Override
protected Void doInBackground(final RawMaterialsEntity... params) {
asyncTaskDao.insertMaterial(params[0]);
return null;
}
}
//DeleteAsyncTask for deleting items from the database
private static class DeleteAsyncTask extends AsyncTask<String, Void, Void> {
private RawMaterialsDao asyncTaskDao;
DeleteAsyncTask(RawMaterialsDao dao) {
asyncTaskDao = dao;
}
@Override
protected Void doInBackground(final String... params) {
asyncTaskDao.deleteMaterial(params[0]);
return null;
}
}
//CostPerGmAsyncTask for dividing the materialCost on the materialWeight
private static class CostPerGmAsyncTask extends AsyncTask<Void, Void, Void> {
private RawMaterialsDao asyncTaskDao;
CostPerGmAsyncTask(RawMaterialsDao dao) {
asyncTaskDao = dao;
}
@Override
protected Void doInBackground(Void... voids) {
asyncTaskDao.divide();
return null;
}
}
//TotalCostAsyncTask for dividing the materialCost on the materialWeight
private static class TotalCostAsyncTask extends AsyncTask<Void, Void, Void> {
private RawMaterialsDao asyncTaskDao;
TotalCostAsyncTask(RawMaterialsDao dao) {
asyncTaskDao = dao;
}
@Override
protected Void doInBackground(Void... voids) {
asyncTaskDao.totalCost();
return null;
}
}
}
和我的视图模型:
public class RawMaterialViewModel extends AndroidViewModel {
private ChefsBoxRepository repository;
private LiveData<List<RawMaterialsEntity>> allMaterials;
private MutableLiveData<List<RawMaterialsEntity>> searchResults;
private List<RawMaterialsEntity> oneRawMaterial;
public RawMaterialViewModel (Application application) {
super(application);
repository = new ChefsBoxRepository(application);
allMaterials = repository.getAllMaterials();
searchResults = repository.getSearchResults();
}
public MutableLiveData<List<RawMaterialsEntity>> getSearchResults() {
return searchResults;
}
public LiveData<List<RawMaterialsEntity>> getAllMaterials() {
return allMaterials;
}
public void insertMaterial(RawMaterialsEntity material) {
repository.insertMaterial(material);
}
public void findMaterial(String name) {
repository.findMaterial(name);
}
public void findMaterialById(int id) {
repository.findMaterialById(id);
}
public void deleteMaterial(String name) {
repository.deleteMaterial(name);
}
public void costPerGm(){
repository.costPerGm();
}
public void totalCost(){
repository.totalCost();
}
}
我找到了解决方案,为此我需要执行以下操作:
道中:
@Query("SELECT * FROM raw_materials WHERE id = :id")
LiveData<RawMaterialsEntity> findMaterialById(int id);
在存储库中:
public LiveData<RawMaterialsEntity> findMaterialById(int id) {
return rawMaterialDao.findMaterialById(id);
}
在视图模型中:
public LiveData<RawMaterialsEntity> findMaterialById(int id) {
return repository.findMaterialById(id);
}
最后是细节activity:
RawMaterialViewModel materialViewModel = new ViewModelProvider(this).get(RawMaterialViewModel.class);
// Update the cached copy of the words in the adapter.
Intent intent = getIntent();
if (intent != null && intent.hasExtra("ID")) {
id = intent.getIntExtra("ID", -1);
// TODO: get material details based on material id
// Set up the materialViewModel
materialViewModel = new ViewModelProvider(this).get(RawMaterialViewModel.class);
materialViewModel.findMaterialById(id).observe(this, new Observer<RawMaterialsEntity>() {
@Override
public void onChanged(RawMaterialsEntity rawMaterialsEntity) {
mMaterialName.setText(rawMaterialsEntity.getRawMaterialName());
mMaterialBrand.setText(rawMaterialsEntity.getRawMaterialBrand());
}
});
我已经使用 android 房间架构构建了房间数据库。现在,当用户点击其中一个回收站项目时,从回收站视图片段中,应用会将他带到该项目的详细信息 activity。
现在我使用以下代码从片段发送项目 ID:
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(mContext.getContext(), MaterialItemView.class);
intent.putExtra("ID", String.valueOf(mMaterial.get(position).getId()));
mContext.startActivity(intent);
}
它正在工作,因为 ID 到达了另一边。
我需要帮助使用 ID 将此数据解析或加载到详细信息 activity。
这是我的详细信息 Activity:
TextView mMaterialName;
TextView mMaterialBrand;
private RawMaterialViewModel mMaterialViewModel;
private List<RawMaterialsEntity> mMaterial; // Cached copy of Materials
RawMaterialsEntity rawMaterialsEntity;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.material_item_view);
mMaterialName = findViewById(R.id.material_name_view);
mMaterialBrand = findViewById(R.id.material_brand_view);
mMaterialViewModel = new ViewModelProvider(this).get(RawMaterialViewModel.class);
// Update the cached copy of the words in the adapter.
Intent intent = getIntent();
if (intent != null && intent.hasExtra("ID")) {
int id = intent.getIntExtra("ID", -1);
// TODO: get material details based on material id
} else {
// ...
}
这是我的道:
@Dao
public interface RawMaterialsDao {
@Insert
void insertMaterial(RawMaterialsEntity rawMaterialsEntity);
@Query("DELETE FROM raw_materials")
void deleteAll();
@Query("SELECT * FROM raw_materials WHERE material_name = :name")
List<RawMaterialsEntity> findMaterial(String name);
@Query("SELECT * FROM raw_materials WHERE id = :id")
List<RawMaterialsEntity> findMaterialById(int id);
@Query("DELETE FROM raw_materials WHERE material_name = :name")
void deleteMaterial(String name);
@Query("SELECT * from raw_materials")
LiveData<List<RawMaterialsEntity>> getAllMaterials();
@Query ("UPDATE raw_materials SET cost_per_gm = material_cost/material_weight")
void divide();
@Query ("UPDATE raw_materials SET total_cost = material_cost*material_quantity")
void totalCost();
}
我的存储库:
public class ChefsBoxRepository {
private MutableLiveData<List<RawMaterialsEntity>> searchResults = new MutableLiveData<>();
private LiveData<List<RawMaterialsEntity>> allMaterials;
private RawMaterialsDao rawMaterialDao;
//public constructor
public ChefsBoxRepository(Application application) {
ChefsBoxRoomDataBase db;
db = ChefsBoxRoomDataBase.getDatabase(application);
rawMaterialDao = db.rawMaterialDao();
allMaterials = rawMaterialDao.getAllMaterials();
}
//used by the view model
public LiveData<List<RawMaterialsEntity>> getAllMaterials() {
return allMaterials;
}
//used by the view model
public MutableLiveData<List<RawMaterialsEntity>> getSearchResults() {
return searchResults;
}
public void insertMaterial(RawMaterialsEntity newMaterial) {
InsertAsyncTask task = new InsertAsyncTask(rawMaterialDao);
task.execute(newMaterial);
}
public void costPerGm(){
CostPerGmAsyncTask task = new CostPerGmAsyncTask(rawMaterialDao);
task.execute();
}
public void totalCost(){
TotalCostAsyncTask task = new TotalCostAsyncTask(rawMaterialDao);
task.execute();
}
public void deleteMaterial(String name) {
DeleteAsyncTask task = new DeleteAsyncTask(rawMaterialDao);
task.execute(name);
}
public void findMaterial(String name) {
QueryAsyncTask task = new QueryAsyncTask(rawMaterialDao);
task.delegate = this;
task.execute(name);
}
public void findMaterialById(int id) {
QueryAsyncTask2 task = new QueryAsyncTask2(rawMaterialDao);
task.delegate = this;
task.execute(id);
}
private void asyncFinished(List<RawMaterialsEntity> results) {
searchResults.setValue(results);
}
//QueryAsyncTask for calling items from database
private static class QueryAsyncTask extends
AsyncTask<String, Void, List<RawMaterialsEntity>> {
private RawMaterialsDao asyncTaskDao;
private ChefsBoxRepository delegate = null;
QueryAsyncTask(RawMaterialsDao dao) {
asyncTaskDao = dao;
}
@Override
protected List<RawMaterialsEntity> doInBackground(final String... params) {
return asyncTaskDao.findMaterial(params[0]);
}
@Override
protected void onPostExecute(List<RawMaterialsEntity> result) {
delegate.asyncFinished(result);
}
}
//QueryAsyncTask for calling items from database
private static class QueryAsyncTask2 extends
AsyncTask<Integer, Void, List<RawMaterialsEntity>> {
private RawMaterialsDao asyncTaskDao;
private ChefsBoxRepository delegate = null;
QueryAsyncTask2(RawMaterialsDao dao) {
asyncTaskDao = dao;
}
@Override
protected List<RawMaterialsEntity> doInBackground(final Integer... params) {
return asyncTaskDao.findMaterialById(params[0]);
}
@Override
protected void onPostExecute(List<RawMaterialsEntity> result) {
delegate.asyncFinished(result);
}
}
//InsertAsyncTask for inserting items to the database
private static class InsertAsyncTask extends AsyncTask<RawMaterialsEntity, Void, Void> {
private RawMaterialsDao asyncTaskDao;
InsertAsyncTask(RawMaterialsDao dao) {
asyncTaskDao = dao;
}
@Override
protected Void doInBackground(final RawMaterialsEntity... params) {
asyncTaskDao.insertMaterial(params[0]);
return null;
}
}
//DeleteAsyncTask for deleting items from the database
private static class DeleteAsyncTask extends AsyncTask<String, Void, Void> {
private RawMaterialsDao asyncTaskDao;
DeleteAsyncTask(RawMaterialsDao dao) {
asyncTaskDao = dao;
}
@Override
protected Void doInBackground(final String... params) {
asyncTaskDao.deleteMaterial(params[0]);
return null;
}
}
//CostPerGmAsyncTask for dividing the materialCost on the materialWeight
private static class CostPerGmAsyncTask extends AsyncTask<Void, Void, Void> {
private RawMaterialsDao asyncTaskDao;
CostPerGmAsyncTask(RawMaterialsDao dao) {
asyncTaskDao = dao;
}
@Override
protected Void doInBackground(Void... voids) {
asyncTaskDao.divide();
return null;
}
}
//TotalCostAsyncTask for dividing the materialCost on the materialWeight
private static class TotalCostAsyncTask extends AsyncTask<Void, Void, Void> {
private RawMaterialsDao asyncTaskDao;
TotalCostAsyncTask(RawMaterialsDao dao) {
asyncTaskDao = dao;
}
@Override
protected Void doInBackground(Void... voids) {
asyncTaskDao.totalCost();
return null;
}
}
}
和我的视图模型:
public class RawMaterialViewModel extends AndroidViewModel {
private ChefsBoxRepository repository;
private LiveData<List<RawMaterialsEntity>> allMaterials;
private MutableLiveData<List<RawMaterialsEntity>> searchResults;
private List<RawMaterialsEntity> oneRawMaterial;
public RawMaterialViewModel (Application application) {
super(application);
repository = new ChefsBoxRepository(application);
allMaterials = repository.getAllMaterials();
searchResults = repository.getSearchResults();
}
public MutableLiveData<List<RawMaterialsEntity>> getSearchResults() {
return searchResults;
}
public LiveData<List<RawMaterialsEntity>> getAllMaterials() {
return allMaterials;
}
public void insertMaterial(RawMaterialsEntity material) {
repository.insertMaterial(material);
}
public void findMaterial(String name) {
repository.findMaterial(name);
}
public void findMaterialById(int id) {
repository.findMaterialById(id);
}
public void deleteMaterial(String name) {
repository.deleteMaterial(name);
}
public void costPerGm(){
repository.costPerGm();
}
public void totalCost(){
repository.totalCost();
}
}
我找到了解决方案,为此我需要执行以下操作:
道中:
@Query("SELECT * FROM raw_materials WHERE id = :id")
LiveData<RawMaterialsEntity> findMaterialById(int id);
在存储库中:
public LiveData<RawMaterialsEntity> findMaterialById(int id) {
return rawMaterialDao.findMaterialById(id);
}
在视图模型中:
public LiveData<RawMaterialsEntity> findMaterialById(int id) {
return repository.findMaterialById(id);
}
最后是细节activity:
RawMaterialViewModel materialViewModel = new ViewModelProvider(this).get(RawMaterialViewModel.class);
// Update the cached copy of the words in the adapter.
Intent intent = getIntent();
if (intent != null && intent.hasExtra("ID")) {
id = intent.getIntExtra("ID", -1);
// TODO: get material details based on material id
// Set up the materialViewModel
materialViewModel = new ViewModelProvider(this).get(RawMaterialViewModel.class);
materialViewModel.findMaterialById(id).observe(this, new Observer<RawMaterialsEntity>() {
@Override
public void onChanged(RawMaterialsEntity rawMaterialsEntity) {
mMaterialName.setText(rawMaterialsEntity.getRawMaterialName());
mMaterialBrand.setText(rawMaterialsEntity.getRawMaterialBrand());
}
});