为每个RecyclerView项创建DetailsActivity

时间:2017-12-23 21:07:36

标签: java android

我一直在寻找一种方法来点击我的recyclerview并让它在recyclerview中显示信息。我已经看到很多使用Lists和Intents来传递数据的解决方案,但我的问题是我使用带有Bitmap图像的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();

    }
}

DetailsActivity:

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);
    }
}

Closet类(定义壁橱内的内容)

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的内容存储在列表中?

3 个答案:

答案 0 :(得分:0)

一个简单的解决方案但不推荐 在recycleler视图适配器类上创建静态变量,然后在项目上单击将值赋给这些变量,然后从下一个按类名调用的活动中访问这些变量

答案 1 :(得分:0)

您需要考虑几件事情。

首先,将您的数据传递给新活动

您可以创建一个意图并将其作为额外内容添加到其中,然后在您的活动中,您将获得这些额外内容并使用它们。

在您的点击事件中

Intent intent = new Intent(context, YourActivity.class);
intent.putExtra("closet_key", closetObject);

在您的活动中onCreate()

Closet closetObject = getIntent().getParcelableExtra("closet_key");
// Use your object

但是,如果您的物体较小,重量较轻,这是合适的,因为如果物体尺寸较大,您将获得

  • 由于正在发生的序列化过程,您的活动开始慢慢开始
  • 或因为您的意图尺寸超过Android的允许尺寸而导致崩溃

另一个选项,只是将某种标识符传递给活动,并使用该标识符加载Activity中的对象。因此,您可以传递数据库ID并在活动中查询该对象。

对于像Bitmaps这样的大型对象,您可以使用其中的任何库来为您进行提取和自动缓存。而且您只需要在数据库中保留图像的URL或路径,而不是整个字节数组。

可以帮助实现这一目标的图书馆是PicassoGlide,还有更多,我发现这些图书很简单并能很好地完成工作。

答案 2 :(得分:0)

 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);