1

I need a clear example of how to use this feature when calling it from another activity outside main_activity. I have searched online but I have not found a complete example. It is always portions of code intended for advanced users.

I need an example including adapter code and how to call the feature from another activity.

Edit: Added the code I have so far but I do not really know how to make it work

Adapter:

public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder>{
private List<Movie> moviesList;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView title, year, genre;

    public MyViewHolder(View view) {
        super(view);
        title = (TextView) view.findViewById(R.id.title);
        genre = (TextView) view.findViewById(R.id.genre);
        year = (TextView) view.findViewById(R.id.year);
    }
}


public MoviesAdapter(List<Movie> moviesList) {
    this.moviesList = moviesList;
}

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

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Movie movie = moviesList.get(position);
    holder.title.setText(movie.getTitle());
    holder.genre.setText(movie.getGenre());
    holder.year.setText(movie.getYear());
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position, List<Object> payloads) {
    if (!payloads.isEmpty()) {
        if (payloads.get(0) instanceof Movie){
            Movie mymovie=(Movie)payloads.get(0);
            holder.title.setText(mymovie.getTitle());}

    } else {
        super.onBindViewHolder(holder, position, payloads);
    }
}

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

}

Main Activity:

public class MainActivity extends AppCompatActivity {
private List<Movie> movieList = new ArrayList<>();
private RecyclerView recyclerView;
private MoviesAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    mAdapter = new MoviesAdapter(movieList);

    recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());

    recyclerView.setLayoutManager(mLayoutManager);


    recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));


    recyclerView.setItemAnimator(new DefaultItemAnimator());

    recyclerView.setAdapter(mAdapter);


    recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {
        int mposition;
        @Override
        public void onClick(View view, int position) {
            mposition=position;
            Movie movie = movieList.get(position);
            Toast.makeText(getApplicationContext(), movie.getTitle() + " is selected!", Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(MainActivity.this,ChangeMovie.class);
            intent.putExtra("passposition",mposition);
            startActivity(intent);
        }

        @Override
        public void onLongClick(View view, int position) {

        }
    }));

    prepareMovieData();
}


private void prepareMovieData() {
    Movie movie = new Movie("Mad Max: Fury Road", "Action & Adventure", "2015");
    movieList.add(movie);

    movie = new Movie("Inside Out", "Animation, Kids & Family", "2015");
    movieList.add(movie);

    movie = new Movie("Star Wars: Episode VII - The Force Awakens", "Action", "2015");
    movieList.add(movie);

    movie = new Movie("Shaun the Sheep", "Animation", "2015");
    movieList.add(movie);

    movie = new Movie("The Martian", "Science Fiction & Fantasy", "2015");
    movieList.add(movie);

    movie = new Movie("Mission: Impossible Rogue Nation", "Action", "2015");
    movieList.add(movie);

    movie = new Movie("Up", "Animation", "2009");
    movieList.add(movie);

    movie = new Movie("Star Trek", "Science Fiction", "2009");
    movieList.add(movie);

    movie = new Movie("The LEGO Movie", "Animation", "2014");
    movieList.add(movie);

    movie = new Movie("Iron Man", "Action & Adventure", "2008");
    movieList.add(movie);

    movie = new Movie("Aliens", "Science Fiction", "1986");
    movieList.add(movie);

    movie = new Movie("Chicken Run", "Animation", "2000");
    movieList.add(movie);

    movie = new Movie("Back to the Future", "Science Fiction", "1985");
    movieList.add(movie);

    movie = new Movie("Raiders of the Lost Ark", "Action & Adventure", "1981");
    movieList.add(movie);

    movie = new Movie("Goldfinger", "Action & Adventure", "1965");
    movieList.add(movie);

    movie = new Movie("Guardians of the Galaxy", "Science Fiction & Fantasy", "2014");
    movieList.add(movie);

    // notify adapter about data set changes
    // so that it will render the list with new data
    mAdapter.notifyDataSetChanged();
}

}

The "Other activity" where I enter a new movie title which I want changed in the recyclerview

public class ChangeMovie extends AppCompatActivity {

EditText newmoviefield;
String newmovie;
Button savebutton;
MoviesAdapter mAdapter;
private static final String TAG="changemovie";
private List<Movie> movieList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_change_movie);
    mAdapter = new MoviesAdapter(movieList);

    final int position=(int)getIntent().getExtras().get("passposition");
    newmoviefield=(EditText)findViewById(R.id.Title_field);
    savebutton=(Button)findViewById(R.id.btn_save);



    savebutton.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View view){
            newmovie=newmoviefield.getText().toString();
            Movie changemovie=new Movie(newmovie,"test1","test2");
            Log.v(TAG,"movieafter"+changemovie.getTitle());
            mAdapter.notifyItemChanged(position,(Object)changemovie);
            finish();
        }
    });
}

}

and the movie.class

public class Movie {
private String title, genre, year;

public Movie() {
}

public Movie(String title, String genre, String year) {
    this.title = title;
    this.genre = genre;
    this.year = year;
}

public String getTitle() {
    return title;
}

public void setTitle(String name) {
    this.title = name;
}

public String getYear() {
    return year;
}

public void setYear(String year) {
    this.year = year;
}

public String getGenre() {
    return genre;
}

public void setGenre(String genre) {
    this.genre = genre;
}

}

Thanks in advance for any help

luis
  • 107
  • 1
  • 8
  • Hello luis. It seems that essentially you are asking us to write some code for you as an "example". What code have you used to solve this yourself? Please post the code along with your error. You should read [this](https://stackoverflow.com/help/mcve) on how to create a [Minimal, Complete, and Verifiable Question](https://stackoverflow.com/help/mcve). – Ishaan Javali Jan 12 '19 at 02:16
  • Possible duplicate of [Need an example about RecyclerView.Adapter.notifyItemChanged(int position, Object payload)](https://stackoverflow.com/questions/33176336/need-an-example-about-recyclerview-adapter-notifyitemchangedint-position-objec) – Rajarshi Jan 12 '19 at 03:16

1 Answers1

1

It is better to used the future onactivityresult in android

It is easy to used make the changes below in your recycle on item clicked method

Make movie model class implement serializable.

Apply below changes to your MainActivity:

Movie movie = movieList.get(mposition);
Intent intent=new Intent(MainActivity.this,ChangeMovie.class);
intent.putExtra("passposition",mposition);
intent.putExtra("movie",movie);
startActivityForResult(i, 100);

Override the below method

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 100 && resultCode == Activity.RESULT_OK) {

    int listpos = mIntent.getIntExtra("listpos", --1);
    if(listpos!=-1){

        Movie movie = (Movie) data.getSerializable("movie");

        movieList.set(listpos,movie);

        if(mAdapter!=null){
        mAdapter.notifyDataSetChanged
        }
    }
}
}

apply below changes in your ChangeMovie

get the list position in your on create method

int pos = -1;
int mposition = getIntent().getExtra().getInt("mposition");

in your save button clicked

newmovie=newmoviefield.getText().toString();
Intent intent = new Intent();
intent.putExtra("movie", changemovie);
intent.putExtra("passposition",mposition);
setResult(RESULT_OK, intent);
finish();

Let me know if still have issues, as code is written manually might be you have to correct some case sensitive thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Navin
  • 285
  • 1
  • 8
  • The above code should work , as it's match your requirement. onActivityResult() method will help you to refresh only the item that you selected not the whole recycleview, i had modified that item with the position so in the list only that item value will be change and adapter will notify only that item to recycleview not the all the items in recycleview – Navin Jan 13 '19 at 03:24
  • just check it and let me know if still you have face issues – Navin Jan 13 '19 at 03:27
  • great, if this is helpful then up it – Navin Jan 13 '19 at 18:17