我需要一个明确的示例,说明从main_activity之外的另一个活动调用此功能时如何使用此功能。 我已经在网上搜索过,但没有找到完整的示例。它始终是面向高级用户的代码部分。
我需要一个示例,其中包括适配器代码以及如何从其他活动中调用该功能。
编辑:添加了到目前为止的代码,但我真的不知道如何使它工作
适配器:
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();
}
}
主要活动:
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();
}
}
在“其他活动”中,我要在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();
}
});
}
}
和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;
}
}
在此先感谢您的帮助
答案 0 :(得分:1)
最好在android中使用将来的onactivityresult
在点击项目的回收方法中,很容易进行以下更改
使电影模型类实现serializable
。
将以下更改应用于您的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
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
}
}
}
}
将以下更改应用到您的ChangeMovie中的更改
在创建方法中获取列表位置
int pos = -1;
int mposition = getIntent().getExtra().getInt("mposition");
在保存按钮中单击
newmovie=newmoviefield.getText().toString();
Intent intent = new Intent();
intent.putExtra("movie", changemovie);
intent.putExtra("passposition",mposition);
setResult(RESULT_OK, intent);
finish();
让我知道是否仍然存在问题,因为手动编写代码可能需要更正一些区分大小写的内容