我如何使用 arrayAdapter 的意图进入第三个 activity?
How do I use intent from arrayAdapter to go into a third activity?
我几乎完成了这个项目,代码很糟糕,但我正在尝试通过 arrayadapter 将数据传递给第三个 activity,但即使没有传递数据,intent 也不会将其传递给第三个activity.
我一直在查看 Whosebug,我已经更改了上下文。我试过添加标志等。
我有三个主要活动,一个适配器和两个自定义 类。
另外,如果重要的话,列表视图会根据输入的数据使用自定义适配器到不同的卡片视图。
public class BookAdaptor extends ArrayAdapter {
int mLayoutID;
List<BookList> dataset;
Context mContext;
public BookAdaptor(Context context, int resource, List<BookList> objects) {
super(context, resource, objects);
mContext = context;
dataset = objects;
mLayoutID = resource;
}
public class pickedAuthor implements Serializable {
private String genre;
private String bookTitle;
private String synopsis;
private String bookAuthor;
private String bookPublisher;
private String bookImage;
private int currentPlace;
public pickedAuthor(String genre, String bookTitle, String synopsis, String bookAuthor, String bookPublisher, String bookImage, int currentPlace) {
this.genre = genre;
this.bookTitle = bookTitle;
this.synopsis = synopsis;
this.bookAuthor = bookAuthor;
this.bookPublisher = bookPublisher;
this.bookImage = bookImage;
this.currentPlace = currentPlace;
}
public int getCurrentPlace() {
return currentPlace;
}
public String getCategoryImage() {
return genre;
}
public String getBookImage() {
return bookImage;
}
public String getBookTitle() {
return bookTitle;
}
public String getBookSynopsis() {
return synopsis;
}
public String getBookAuthor() {
return bookAuthor;
}
public String getBookPublisher() {
return bookPublisher;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View currentListViewItem = convertView;
// Check if the existing view is being reused, otherwise inflate the view
if (currentListViewItem == null) {
currentListViewItem = LayoutInflater.from(getContext()).inflate(mLayoutID, parent, false);
}
//Get the Number object for the current position
final BookList currentNumber = dataset.get(position);
//Set the attributed of list_view_number_item views
ImageView iconImageView = (ImageView) currentListViewItem.findViewById(R.id.image_view_book_icon);
int i = mContext.getResources().getIdentifier(
currentNumber.getBookImage(), "drawable",
mContext.getPackageName());
//Setting the icon
iconImageView.setImageResource(i);
TextView titleNameTextView = (TextView) currentListViewItem.findViewById(R.id.text_view_book_title);
titleNameTextView.setText(currentNumber.getBookTitle());
TextView authorNameTextView = (TextView) currentListViewItem.findViewById(R.id.text_view_author_name);
authorNameTextView.setText(currentNumber.getBookAuthor());
TextView publisherNameTextView = (TextView) currentListViewItem.findViewById(R.id.text_view_publisher_name);
publisherNameTextView.setText(currentNumber.getBookPublisher());
CardView button = (CardView) currentListViewItem.findViewById(R.id.card_view_list_item);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent;
int currentDigit;
intent = new Intent(mContext.getApplicationContext(), DetailsActivity.class);
String currentGenre,currentTitle, currentBookImage, currentCategoryImage, currentSynopsis, currentAuthor, currentPublisher;
currentDigit = 1;
currentGenre = currentNumber.getCategoryImage();
currentTitle = currentNumber.getBookImage().toString();
currentBookImage = currentNumber.getBookImage();
currentSynopsis = currentNumber.getBookSynopsis();
currentAuthor = currentNumber.getBookAuthor();
currentPublisher = currentNumber.getBookPublisher();
pickedAuthor author;
author = new pickedAuthor(currentGenre, currentTitle, currentSynopsis, currentAuthor, currentPublisher, currentBookImage, currentDigit);
intent.putExtra("Author", author);
mContext.getApplicationContext().startActivity(intent);
}
});
return currentListViewItem;
}
}
package com.example.bookstore;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class DetailsActivity extends AppCompatActivity {
public BookAdaptor.pickedAuthor author;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
Intent intent = getIntent();
author = (BookAdaptor.pickedAuthor) intent.getSerializableExtra("Author Name");
TextView titleNameTextView = (TextView) findViewById(R.id.text_view_book_title);
titleNameTextView.setText(author.getBookTitle());
TextView authorNameTextView = (TextView) findViewById(R.id.details_text_view_author_name);
authorNameTextView.setText(author.getBookAuthor());
TextView publisherNameTextView = (TextView) findViewById(R.id.details_text_view_publisher_name);
publisherNameTextView.setText(author.getBookPublisher());
TextView synopsisTextView = (TextView) findViewById(R.id.text_view_synopsis);
synopsisTextView.setText(author.getBookSynopsis());
}
}
intent.putExtra("Author", new Gson().toJson(author));
Author author= new Gson().fromJson(intent.getStringExtra("Author"), Author.class);
这样试试。
像这样更改onclick
Intent intent = new Intent(v.getContext, ThirdActivity.class);
DataClass data = new DataClass(param1, param2);
intent.putExtra("param", data);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
v.getContext().startActivity(intent);
如果它不起作用,请尝试更改 Dataclass 以使用 intent 实现 parcelable 和 sent parcelable 对象。
Note : Always prefer parcelable over serializable (Parcelable is
perfomant) if you have no specific advantage using serializables.
我几乎完成了这个项目,代码很糟糕,但我正在尝试通过 arrayadapter 将数据传递给第三个 activity,但即使没有传递数据,intent 也不会将其传递给第三个activity.
我一直在查看 Whosebug,我已经更改了上下文。我试过添加标志等。
我有三个主要活动,一个适配器和两个自定义 类。
另外,如果重要的话,列表视图会根据输入的数据使用自定义适配器到不同的卡片视图。
public class BookAdaptor extends ArrayAdapter {
int mLayoutID;
List<BookList> dataset;
Context mContext;
public BookAdaptor(Context context, int resource, List<BookList> objects) {
super(context, resource, objects);
mContext = context;
dataset = objects;
mLayoutID = resource;
}
public class pickedAuthor implements Serializable {
private String genre;
private String bookTitle;
private String synopsis;
private String bookAuthor;
private String bookPublisher;
private String bookImage;
private int currentPlace;
public pickedAuthor(String genre, String bookTitle, String synopsis, String bookAuthor, String bookPublisher, String bookImage, int currentPlace) {
this.genre = genre;
this.bookTitle = bookTitle;
this.synopsis = synopsis;
this.bookAuthor = bookAuthor;
this.bookPublisher = bookPublisher;
this.bookImage = bookImage;
this.currentPlace = currentPlace;
}
public int getCurrentPlace() {
return currentPlace;
}
public String getCategoryImage() {
return genre;
}
public String getBookImage() {
return bookImage;
}
public String getBookTitle() {
return bookTitle;
}
public String getBookSynopsis() {
return synopsis;
}
public String getBookAuthor() {
return bookAuthor;
}
public String getBookPublisher() {
return bookPublisher;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View currentListViewItem = convertView;
// Check if the existing view is being reused, otherwise inflate the view
if (currentListViewItem == null) {
currentListViewItem = LayoutInflater.from(getContext()).inflate(mLayoutID, parent, false);
}
//Get the Number object for the current position
final BookList currentNumber = dataset.get(position);
//Set the attributed of list_view_number_item views
ImageView iconImageView = (ImageView) currentListViewItem.findViewById(R.id.image_view_book_icon);
int i = mContext.getResources().getIdentifier(
currentNumber.getBookImage(), "drawable",
mContext.getPackageName());
//Setting the icon
iconImageView.setImageResource(i);
TextView titleNameTextView = (TextView) currentListViewItem.findViewById(R.id.text_view_book_title);
titleNameTextView.setText(currentNumber.getBookTitle());
TextView authorNameTextView = (TextView) currentListViewItem.findViewById(R.id.text_view_author_name);
authorNameTextView.setText(currentNumber.getBookAuthor());
TextView publisherNameTextView = (TextView) currentListViewItem.findViewById(R.id.text_view_publisher_name);
publisherNameTextView.setText(currentNumber.getBookPublisher());
CardView button = (CardView) currentListViewItem.findViewById(R.id.card_view_list_item);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent;
int currentDigit;
intent = new Intent(mContext.getApplicationContext(), DetailsActivity.class);
String currentGenre,currentTitle, currentBookImage, currentCategoryImage, currentSynopsis, currentAuthor, currentPublisher;
currentDigit = 1;
currentGenre = currentNumber.getCategoryImage();
currentTitle = currentNumber.getBookImage().toString();
currentBookImage = currentNumber.getBookImage();
currentSynopsis = currentNumber.getBookSynopsis();
currentAuthor = currentNumber.getBookAuthor();
currentPublisher = currentNumber.getBookPublisher();
pickedAuthor author;
author = new pickedAuthor(currentGenre, currentTitle, currentSynopsis, currentAuthor, currentPublisher, currentBookImage, currentDigit);
intent.putExtra("Author", author);
mContext.getApplicationContext().startActivity(intent);
}
});
return currentListViewItem;
}
}
package com.example.bookstore;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class DetailsActivity extends AppCompatActivity {
public BookAdaptor.pickedAuthor author;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
Intent intent = getIntent();
author = (BookAdaptor.pickedAuthor) intent.getSerializableExtra("Author Name");
TextView titleNameTextView = (TextView) findViewById(R.id.text_view_book_title);
titleNameTextView.setText(author.getBookTitle());
TextView authorNameTextView = (TextView) findViewById(R.id.details_text_view_author_name);
authorNameTextView.setText(author.getBookAuthor());
TextView publisherNameTextView = (TextView) findViewById(R.id.details_text_view_publisher_name);
publisherNameTextView.setText(author.getBookPublisher());
TextView synopsisTextView = (TextView) findViewById(R.id.text_view_synopsis);
synopsisTextView.setText(author.getBookSynopsis());
}
}
intent.putExtra("Author", new Gson().toJson(author));
Author author= new Gson().fromJson(intent.getStringExtra("Author"), Author.class);
这样试试。
像这样更改onclick
Intent intent = new Intent(v.getContext, ThirdActivity.class);
DataClass data = new DataClass(param1, param2);
intent.putExtra("param", data);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
v.getContext().startActivity(intent);
如果它不起作用,请尝试更改 Dataclass 以使用 intent 实现 parcelable 和 sent parcelable 对象。
Note : Always prefer parcelable over serializable (Parcelable is perfomant) if you have no specific advantage using serializables.