我有一个类,我用一些按钮定义了一个回收站视图。在相对适配器中,我为每个按钮添加一个onClick方法。从onClick方法,我需要在另一个活动中调用一个方法。我能怎么做?我尝试了这个但是它没有用,除非我在第一个活动中调用该方法。
第一个活动,我宣布使用RecyclerView:
公共类Classici_Tab扩展了Fragment {
LinkedList<Book> catalog = new LinkedList<>();
RecyclerView mRecyclerView;
BookListAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.classici_tab, container, false);
fillCatalog("catalog.csv"); //fill the LinkedList
mRecyclerView = (RecyclerView) fragmentView.findViewById(R.id.classici_rv);
mAdapter = new BookListAdapter(getContext(), catalog);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
return fragmentView;
}
}
}
适配器:
public class BookListAdapter extends RecyclerView.Adapter<BookListAdapter.BookViewHolder> {
static Drawable borrow_top;
static Drawable reserve_top;
public BookListAdapter()
{}
// View Holder
class BookViewHolder extends RecyclerView.ViewHolder
{
public final Button borrow;
public final Button addfavorites;
final BookListAdapter mAdapter;
public BookViewHolder(final View itemView, BookListAdapter adapter) {
super(itemView);
borrow = (Button)itemView.findViewById(R.id.borrow);
addfavorites = (Button)itemView.findViewById(R.id.addfavorites);
this.mAdapter = adapter;
addfavorites.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int cover_book = R.id.cover;
final String title_book = title.getText().toString();
final String author_book = author.getText().toString();
add_to_fav(itemView, new Book(cover_book, title_book, author_book)); //METHOD TO BE CALLED
}
});
}
}
private LinkedList<Book> mBookList = new LinkedList<>(); //per Classici_Tab TODO
private LinkedList<Book> mBookListFavorites = new LinkedList<>(); //per Favorites_page
private LayoutInflater mInflater;
boolean isVisible = false;
public ItemTouchHelper.Callback callback;
ClickListener clickListener;
public BookListAdapter(Context context, LinkedList<Book> bookList) {
mInflater = LayoutInflater.from(context);
this.mBookList = bookList;
}
@Override
public BookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mItemView = mInflater.inflate(R.layout.tab_list_item, parent, false);
return new BookViewHolder(mItemView, this);
}
@Override
public void onBindViewHolder(final BookViewHolder holder, int position) {
...
}
@Override
public int getItemCount() {
...
}
//INTERFACE TO CALL THE METHOD (IN THE SECOND ACTIVITY)
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
public void add_to_fav(View v, Book book)
{
if(clickListener != null)
{
clickListener.itemClicked(v, book);
}
}
public interface ClickListener
{
void itemClicked(View view, Book book);
}
}
第二个活动,我宣布方法:
public class Favorites_Page extends Activity implements BookListAdapter.ClickListener {
RecyclerView favorites_rv;
BookListAdapter mAdapter_List;
LinkedList<Book> favorites = new LinkedList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favorites_page);
favorites_rv = (RecyclerView)findViewById(R.id.favorites_rv);
favorites_rv.setAdapter(mAdapter);
favorites_rv.setLayoutManager(new LinearLayoutManager(this));
mAdapter_List = new BookListAdapter();
mAdapter_List.clickListener = (BookListAdapter.ClickListener)this;
}
@Override
public void itemClicked(View view, Book book) {
Toast.makeText(getApplicationContext(), "the method has been called successfully", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
您需要序列化才能在两个活动之间进行通信。例如:
addfavorites.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int cover_book = R.id.cover;
final String title_book = title.getText().toString();
final String author_book = author.getText().toString();
Intent intent = new Intent( CurrentActivityClass.this, Favorites_Page.class )
intent.putExtra(Favorites_Page.KEY_BOOK , Book) // Book need to implements Parcelable (or Serializable if all content is Serializable)
CurrentActivityClass.this.startActivity(intent)
}
});
然后从您的fav活动中获得如下数据:
public class Favorites_Page extends Activity {
public static final String KEY_BOOK = "book"
RecyclerView favorites_rv;
BookListAdapter mAdapter_List;
LinkedList<Book> favorites = new LinkedList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favorites_page);
...
Book book = getIntent().getExtra(KEY_BOOK)
...
}
...