Satyaki Mukherjee Satyaki Mukherjee - 9 days ago 10
Android Question

Update RecycleView particular position from Activity's startOnActivityResult

I would like to update a view of particular position of RecylerView but it does not happen due to some problem which are as follows-
1) when scroll up/down it created mess
2) I handle this by

holder.setIsRecyclable(false);
but when scroll up/down then again it does not show previously saved child state.

My code are as follows-

ADAPTER CLASS

public class EventNameAdapter extends RecyclerView.Adapter<EventNameAdapter.MyViewHolder> {

private List<EventNameModel> eventNameList;

public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView eventNameText;
public ImageView imageViewRadio;

public MyViewHolder(View view) {
super(view);
eventNameText = (TextView) view.findViewById(R.id.eventNameText);
imageViewRadio = (ImageView) view.findViewById(R.id.imageViewRadio);
}
}


public EventNameAdapter(List<EventNameModel> eventNameList) {
this.eventNameList = eventNameList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.event_name, parent, false);

return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
EventNameModel movie = eventNameList.get(position);
holder.setIsRecyclable(false);
holder.eventNameText.setText(movie.getEventName());
}

@Override
public int getItemCount() {
return eventNameList.size();
}


}

UPDATE POSITION CODE

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if(resultCode == RESULT_OK){
int responseInt = data.getIntExtra("SUBMIT_DATA",-2);
Toast.makeText(EventListActivity.this,"Response- "+responseInt,Toast.LENGTH_SHORT).show();
if(responseInt>=0){
EventNameAdapter.MyViewHolder viewHolder = (EventNameAdapter.MyViewHolder) nameRecyclerView.findViewHolderForAdapterPosition(responseInt);
if (viewHolder != null) {
Toast.makeText(EventListActivity.this,"Response 2 - "+responseInt,Toast.LENGTH_SHORT).show();
viewHolder.imageViewRadio.setImageResource(R.drawable.radio_button_on);
Log.d("hellotom", "if i=" + responseInt);

} else {
Log.d("hellotom", "else i=" + responseInt);
}
}
}
}
}


Please help me how I will solve this issue.

Answer

In your EventNameModel class, have a field to maintain the state of your radio button like the following

public class EventNameModel {
    // your other fields
    private boolean isRadioOn;

    public void setIsRadioOn(boolean value){
        this.isRadioOn = value;
    }

    public boolean isRadioOn(){
        return isRadioOn;
    }
}

Then adjust you onBindViewHolder method like follows

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    EventNameModel movie = eventNameList.get(position);
    holder.eventNameText.setText(movie.getEventName());
    if(movie.isRadioOn()){
        holder.imageViewRadio.setImageResource(R.drawable.radio_button_on);
    } else {
        holder.imageViewRadio.setImageResource(R.drawable.radio_button_off);
    }
}

Have a method like this in your adapter class to update the position retrieved from your other activity

public void updateItemAt(int position){
    EventNameModel movie = eventNameList.get(position);
    movie.setIsRadioOn(true);
    notifyItemChanged(position);
}

Now you can call this method from onActivityResult like follows

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 101) {
        if(resultCode == RESULT_OK){
            int responseInt =   data.getIntExtra("SUBMIT_DATA",-2);
            if(responseInt>=0){
                mAdapter.updateItemAt(responseInt);
            }
        }
    }
}
Comments