[英]Create DetailsActivity for Each RecyclerView Item
我一直在到处搜索以找到一种方法来单击我的recyclerview并使其在recyclerview中显示信息。 我已经看到许多使用列表和意图来传递数据的解决方案,但是我的问题是我正在使用带有位图图像和两个字符串值的SQLiteDatabase。 我应该如何将这些数据传递给另一个活动?
这是我到目前为止的内容:(CustomAdapter类)
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.TaskHolder> {
private Cursor mCursor;
private OnItemClickListener mOnItemClickListener;
private Context mContext;
/* Callback for list item click events */
public interface OnItemClickListener {
void onListItemClick(int clickedItemIndex);
}
public CustomAdapter(Context mContext) {
this.mContext = mContext;
}
public CustomAdapter()
{
mContext = null;
}
/* ViewHolder for each task item */
public class TaskHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView shoeBrandName;
public TextView shoeName;
public ImageView shoeImage;
public TaskHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
shoeBrandName = (TextView) itemView.findViewById(R.id.textBrandName);
shoeImage = (ImageView) itemView.findViewById(R.id.shoeImage);
shoeName = (TextView) itemView.findViewById(R.id.textShoeName);
}
@Override
public void onClick(View v) {
}
}
@Override
public TaskHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View itemView = inflater
.inflate(R.layout.text_row_item, parent, false);
return new TaskHolder(itemView);
}
@Override
public void onBindViewHolder(TaskHolder holder, int position) {
int idIndex = mCursor.getColumnIndex(DatabaseContract.ShoeColumns._ID);
int imgValue = mCursor.getColumnIndex(DatabaseContract.ShoeColumns.SHOE_IMAGE);
int shoeBrandName = mCursor.getColumnIndex(DatabaseContract.ShoeColumns.SHOE_BRAND);
int shoeName = mCursor.getColumnIndex(DatabaseContract.ShoeColumns.SHOE_NAME);
mCursor.moveToPosition(position);
final int id = mCursor.getInt(idIndex);
byte[] shoeImg = mCursor.getBlob(imgValue);
String brandNameStr = mCursor.getString(shoeBrandName);
String shoeNameStr = mCursor.getString(shoeName);
Bitmap bmp = BitmapFactory.decodeByteArray(shoeImg, 0, shoeImg.length);
holder.itemView.setTag(id);
holder.shoeImage.setImageBitmap(Bitmap.createScaledBitmap(bmp, 100, 100, false));
holder.shoeBrandName.setText(brandNameStr);
holder.shoeName.setText(shoeNameStr);
}
@Override
public int getItemCount() {
return (mCursor != null) ? mCursor.getCount() : 0;
}
public void swapCursor(Cursor cursor) {
if (mCursor != null) {
mCursor.close();
}
mCursor = cursor;
notifyDataSetChanged();
}
}
详细信息活动:
package com.example.android.myshoecloset;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class ShoeDetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shoe_detail);
ImageView imgShoe = (ImageView) findViewById(R.id.shoeImgDetails);
TextView brandShoe = (TextView) findViewById(R.id.shoeBrandDetails);
TextView nameShoe = (TextView) findViewById(R.id.shoeNameDetails);
}
}
壁橱类(定义壁橱内部的内容)
import android.database.Cursor;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by man on 12/21/2017.
*/
public class Closet implements Parcelable
{
private static final String TAG = Closet.class.getSimpleName();
//Brand name
private final String brandName;
//Shoe Name
private final String shoeName;
//Image of the shoe
private final String shoeImage;
public Closet(String brandName, String shoeName, String shoeImage)
{
this.brandName = brandName;
this.shoeName = shoeName;
this.shoeImage = shoeImage;
}
public Closet(Cursor cursor)
{
this.brandName = null;
this.shoeName = null;
this.shoeImage = null;
}
protected Closet(Parcel in)
{
this.brandName = in.readString();
this.shoeName = in.readString();
this.shoeImage = in.readString();
}
public String getShoeImageName()
{
return shoeImage;
}
public String getBrandName()
{
return brandName;
}
public String getShoeName()
{
return shoeName;
}
@Override
public void writeToParcel(Parcel dest, int flags)
{
dest.writeString(brandName);
dest.writeString(shoeName);
dest.writeString(shoeImage);
}
@Override
public int describeContents()
{
return 0;
}
public static final Creator<Closet> CREATOR = new Creator<Closet>(){
@Override
public Closet createFromParcel(Parcel in)
{
return new Closet(in);
}
@Override
public Closet[] newArray(int size)
{
return new Closet[size];
}
};
}
有没有一种方法可以从每个recyclerview获取信息,而不必在列表中存储该recyclerview的内容?
一个简单的解决方案,但不建议在回收者视图适配器类上创建静态变量,然后在项目上单击,将值分配给这些变量,然后从下一个活动中通过类名调用这些变量
您需要考虑几件事。
首先, 将您的数据传递给新活动 。
您可以创建一个意图并将数据作为附加项添加到其中,然后在您的活动中,您将获得这些附加项并使用它们。
在您的点击事件中
Intent intent = new Intent(context, YourActivity.class);
intent.putExtra("closet_key", closetObject);
在您的活动的onCreate()
Closet closetObject = getIntent().getParcelableExtra("closet_key");
// Use your object
但是,如果您的物体较小,重量较轻,这是适当的,因为如果物体较大,您将得到
另一种选择是,仅将某种标识符传递给活动,并使用该标识符将对象加载到活动中。 因此,您可以传递数据库ID并在活动中查询该对象。
对于像Bitmaps
这样的大对象,您可以使用那里的任何库来为您执行获取和自动缓存。 您只需要在数据库中保留图像的URL或路径,而不是整个字节数组。
毕加索(Picasso)或格莱德(Glide)可以帮助实现这一点的图书馆,还有很多,我发现它们很简单,而且做得很好。
public static Bitmap yourBitmap;
public class MovementListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
...
//assign value to **yourBitmap** on item click
yourBitamp = imageBitmap;
}
从任何地方访问此yourBitmap-
imageView.setImageBitmap(YourRecyclerViewAdapter.yourBitmap);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.