我有一个recyclerview适配器,它在列表视图中显示数据,这些数据是通过游标填充的。我想进一步扩展,为每个列表项设置一个点击,并将值的意图传递给另一个活动,如详细活动。
Adapter.class
public class RecyclerAdapter extends
RecyclerView.Adapter<RecyclerAdapter.Holder> {
/* ViewHolder for each insect item */
public class Holder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView friendlyName, scientificName, dangerLevel;
ImageView image;
public Holder(View itemView) {
super(itemView);
friendlyName = (TextView) itemView.findViewById(R.id.friendlyName);
scientificName = (TextView) itemView.findViewById(R.id.scientificName);
dangerLevel = (TextView) itemView.findViewById(R.id.text1);
}
@Override
public void onClick(View v) {
}
}
private Cursor mCursor;
private Context mContext;
public RecyclerAdapter(Context context, Cursor cursor) {
this.mContext = context;
this.mCursor = cursor;
}
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.bugs_list_item, parent, false);
return new Holder(view);
}
@Override
public void onBindViewHolder(Holder holder, int position) {
int insectname = mCursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_FRIENDLYNAME);
int scienceName = mCursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_SCIENTIFICNAME);
int id = mCursor.getColumnIndex(BugsContract.BugsEntry._ID);
int dangerlevel = mCursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_DANGERLEVEL);
int insectImage = mCursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_IMAGEASSET);
mCursor.moveToPosition(position);
String insectRName = mCursor.getString(insectname);
String scienceRName = mCursor.getString(scienceName);
String insectRImage = mCursor.getString(insectImage);
int dangerlevelInt = mCursor.getInt(dangerlevel);
String dangerString = "" + dangerlevelInt;
holder.dangerLevel.setText(dangerString);
holder.friendlyName.setText(insectRName);
holder.scientificName.setText(scienceRName);
}
private int getDangerColor(int danger) {
int priorityColor = 0;
int[] colorDangerarray = mContext.getResources().getIntArray(R.array.dangerColors);
switch(danger) {
case 1: priorityColor = ContextCompat.getColor(mContext, colorDangerarray[0]);
break;
case 2: priorityColor = ContextCompat.getColor(mContext, colorDangerarray[1]);
break;
case 3: priorityColor = ContextCompat.getColor(mContext, colorDangerarray[2]);
break;
default: break;
}
return priorityColor;
}
@Override
public int getItemCount() {
return mCursor.getCount();
}
/**
* Return the {@link Insect} represented by this item in the adapter.
*
* @param position Adapter item position.
*
* @return A new {@link Insect} filled with this position's attributes
*
* @throws IllegalArgumentException if position is out of the adapter's bounds.
*/
public Insect getItem(int position) {
if (position < 0 || position >= getItemCount()) {
throw new IllegalArgumentException("Item position is out of adapter's range");
} else if (mCursor.moveToPosition(position)) {
return new Insect(mCursor);
}
return null;
}
}
实现可加速
的模型类public class Insect implements Parcelable {
private static final String TAG = Insect.class.getSimpleName();
//Common name
public int id;
public String name;
//Latin scientific name
public String scientificName;
//Classification order
public String classification;
//Path to image resource
public String imageAsset;
//1-10 scale danger to humans
public int dangerLevel;
/**
* Create a new Insect from discrete values
*/
public Insect(String name, String scientificName, String classification, String imageAsset, int dangerLevel) {
this.name = name;
this.scientificName = scientificName;
this.classification = classification;
this.imageAsset = imageAsset;
this.dangerLevel = dangerLevel;
}
/**
* Create a new Insect from a database Cursor
*/
public Insect(Cursor cursor) {
//TODO: Create a new insect from cursor
this.name = cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_FRIENDLYNAME));
this.scientificName = cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_SCIENTIFICNAME));
this.classification = cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_CLASSIFICATION));
this.imageAsset = cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_IMAGEASSET));
this.dangerLevel = Integer.parseInt(cursor.getString(cursor.getColumnIndex(BugsContract.BugsEntry.COLUMN_DANGERLEVEL)));
}
public Insect() {
}
/**
* Create a new Insect from a data Parcel
*/
protected Insect(Parcel in) {
this.name = in.readString();
this.scientificName = in.readString();
this.classification = in.readString();
this.imageAsset = in.readString();
this.dangerLevel = in.readInt();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(scientificName);
dest.writeString(classification);
dest.writeString(imageAsset);
dest.writeInt(dangerLevel);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<Insect> CREATOR = new Creator<Insect>() {
@Override
public Insect createFromParcel(Parcel in) {
return new Insect(in);
}
@Override
public Insect[] newArray(int size) {
return new Insect[size];
}
};
}
我想知道如何声明将数据存储到另一个活动的意图以及如何在下一个活动中从意图中获取数据
答案 0 :(得分:0)
只要您在模型类上实现了parcelable,就可以像通常通过putExtra()
方法传递原始数据一样轻松传递对象。
对于发件人活动:
Intent receiverIntent = new Intent(this, Receiver.class);
receiverIntent.putExtra("key", ParcelableObject);
startActivity(receiverIntent);
对于接收器类,您只需创建一个intent对象即可获得parcelableExtra
方法中的onCreate()
。
Intent i = getIntent();
ParcelableClass parcelableObject;
parcelableObject = i.getParcelableExtra("key");
瞧,应该这样做。