简体   繁体   中英

android architecture: call dao method from activity and pass result to child activity for filtering

I am new in Android development and I need some help for resolving the following trouble. I have 2 entities: MuscleGroup and Practice; MuscleGroup can have several practices for training. In a first Activity I display all muscle groups in a recyclerview. When I tap on a group the onClick method should pass to a second activity the group name or database id in order to fill another recyclerview with only practices of selected group. I don't want observe any changed list because records are freezed.

I tried to use dao method in Activity with no Livedata and no observer, but I got error because methods cannot be used in UI thread.

public void showPractices(View view) {
    Intent practiceIntent = new Intent(PracticeGroupActivity.this,
            PracticeActivity.class);
    TextView group = view.findViewById(R.id.musclegroupname);
    Log.i(PracticeGroupActivity.class.getSimpleName(), "group selected: " + group.getText().toString());
    GinnasiumDatabase db = GinnasiumDatabase.getInstance(PracticeGroupActivity.this);
    MuscleGroup found = db.muscleGroupDAO().getMuscleGroupByName(group.getText().toString());
    if(found != null){
        practiceIntent.putExtra("idGroup", found.getId());
        startActivity(practiceIntent);
    }
}

Child Activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_practice);

    RecyclerView recyclerView = findViewById(R.id.recyclerpracticeview);
    final PracticeListAdapter adapter = new PracticeListAdapter(this);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.addItemDecoration(new DividerItemDecoration(this,
            DividerItemDecoration.VERTICAL));

    int idGroup = getIntent().getExtras().getInt("idGroup");
    practiceViewModel = ViewModelProviders.of(this).get(PracticeViewModel.class);
    practiceViewModel.getAllPracticesByGroup(idGroup).observe(this, practices -> { adapter.setPractices(practices);});
}

I hope my description is clear.

In your question i can't see what are you using for showing MuscleGroup but i suppose is RecyclerView for the Code of ChildActivity if so. You can write de click method in the Adapter of MuscleGroup, in this way you can avoid this line:

MuscleGroup found = db.muscleGroupDAO().getMuscleGroupByName(group.getText().toString());

Because i think your query to db freeze your app.

But in adapter you can send the id to activity without the query, because you can set the onclick method when the view is filled.

You can do this in the onBindViewHolderMethod.

Something like this:

@Override
    public void onBindViewHolder(VODViewHolder viewHolder, int i) {
        ...
        final MuscleGroup muscleGroup = muscleGroupList.get(i);
        ...
        viewHolder.wrapper.setOnClickListener(new View.OnClickListener() {
              Intent practiceIntent = new Intent(PracticeGroupActivity.this, PracticeActivity.class);
              practiceIntent.putExtra("idGroup", muscleGroup.getId());
              startActivity(practiceIntent);
        });
}

Supposing that you have a List called muscleGroupList like your DataSet in the Adapter and a wrapper view that wraps all the MuscleGroup Items, linked in the VODViewHolder class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM