Hello, I'd like to know answer to challenge of Chapter 10


#1

oh, I found topic board for 2nd Edition…
I couldn’t find a delete button…


Hello, I started studying Android programming a month ago.
I don’t know what I’m doing right after I solve the challenge. So leave this post.

this is part of my code for using notifyItemChanged(int).

    private int mCurrentPosition;
    private class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

         ...

        @Override
        public void onClick(View v) {
            mCurrentPosition = getAdapterPosition();
            Intent intent = CrimeActivity.newIntent(getActivity(), mCrime.getId());
            startActivity(intent);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        updateUI(mCurrentPosition);
    }

    private void updateUI(int position){
        Log.e(TAG, "updateUI(int)");
        mAdapter.notifyItemChanged(position);
    }

it seems running well. but I’d like to see your code and compare with mine.

I wish you to more understanding about my English. Thanks.


#2

Nice work on the challenge. We don’t have official solutions for any of the challenges.

There’s a discussion of this challenge and I’ve posted about what I would do here: Challenge Solution - having trouble


#3

@cstewart
Thanks, I already checked your solution and it was very helpful for me.
Thanks a lot!


#4

Good! Thank You for your solution. It works.
But I am interested how can I find methods like "getAdapterPosition()"
Is there some documentation with possibility of finding methods by keywords?

I feel myself stupid :frowning:


#5

well… I usually googled with keyword like ‘how to get selected position of recyclerview android’. haha…


#6

I use the 3rd edition, however, I followed the link you had posted that took me to the 2nd edition and posted 2 replies. Re-posting here since I actually use the 3rd edition. Sorry for the redundancy :sweat::grin:
I see that the underlying assumption behind clicking a item in the list fragment would necessarily mean that the user changes the details of the crime. However, I did not presume this. Instead, I detect for change in the fields that can be modified by the user (title and isSolved) using the existing infrastructure (text changed listener and checked changed listener). If there is actually a change in one of the fields then I pass on the information as a boolean via the setResult method from the Fragment and get that from the onActivityResult. Additionally, I always save a index of the clicked item. So, when I actually detect a change, I use the index position information force a item level list reload via the notifyItemChanged(changedItemPosition).

private void updateUI() {
CrimeLab crimeLab = CrimeLab.get(getActivity());
List crimes = crimeLab.getCrimes();

if (adapter == null) {
    adapter = new CrimeAdapter(crimes);
    crimeRecyclerView.setAdapter(adapter);
} else {
    Log.d("UpdateUI", "Position reported by click listener: " + clickedItemPosition);
    if (crimeRecordAltered) adapter.notifyItemChanged(clickedItemPosition);
}

}
I see that that I did not accommodate for the data refresh when the activity goes to the background and comes back to the screen again after a brief while. Besides this, is my solution correct.