简体   繁体   English

为每个RecyclerView项目创建DetailsActivity

[英]Create DetailsActivity for Each RecyclerView Item

I have been searching everywhere to find a way to get click on my recyclerview and get it to display the information in the recyclerview. 我一直在到处搜索以找到一种方法来单击我的recyclerview并使其在recyclerview中显示信息。 I have seen many solutions using Lists and Intents to pass the data around, but my problem is that I am using an SQLiteDatabase With a Bitmap image, and two string values. 我已经看到许多使用列表和意图来传递数据的解决方案,但是我的问题是我正在使用带有位图图像和两个字符串值的SQLiteDatabase。 How am I supposed to pass that data to another activity? 我应该如何将这些数据传递给另一个活动?

Here is what I have so far:(CustomAdapter class) 这是我到目前为止的内容:(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 class(defines what is inside of the 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];
        }
    };

}

Is there a way to get the information from each recyclerview without having to store the contents of that recyclerview in a list? 有没有一种方法可以从每个recyclerview获取信息,而不必在列表中存储该recyclerview的内容?

一个简单的解决方案,但不建议在回收者视图适配器类上创建静态变量,然后在项目上单击,将值分配给这些变量,然后从下一个活动中通过类名调用这些变量

There are a couple of things you need to consider. 您需要考虑几件事。

First of all, passing your data to the new activity . 首先, 将您的数据传递给新活动

You can create an intent and add your data to it as extras, and then in your activity you would get those extras and use them. 您可以创建一个意图并将数据作为附加项添加到其中,然后在您的活动中,您将获得这些附加项并使用它们。

In your click event 在您的点击事件中

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

And in your activity's onCreate() 在您的活动的onCreate()

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

However, this is appropriate in case you have small objects, light-weight, because if the object is large in size you will get either 但是,如果您的物体较小,重量较轻,这是适当的,因为如果物体较大,您将得到

  • Slow laggy start of your activity, because of the serialization process that is happening 由于正在进行序列化过程,因此您的活动开始时会缓慢
  • Or a crash because your intent size exceeded the allowed size by Android 或因您的意图大小超出Android允许的大小而崩溃

Another option , is to only pass some sort of identifier to the activity, and use that identifier to load the object in your Activity. 另一种选择是,仅将某种标识符传递给活动,并使用该标识符将对象加载到活动中。 So you can pass the DB ID and query that object in the activity. 因此,您可以传递数据库ID并在活动中查询该对象。

For large objects like Bitmaps , you can use any of the libraries out there to do the fetching and automatic caching for you. 对于像Bitmaps这样的大对象,您可以使用那里的任何库来为您执行获取和自动缓存。 And you only need to persist the URL or path of the image in your DB not the whole byte array. 您只需要在数据库中保留图像的URL或路径,而不是整个字节数组。

Libraries that could help doing that is Picasso or Glide , there are many more, those are the ones that I found to be simple and does the job well. 毕加索(Picasso)格莱德(Glide)可以帮助实现这一点的图书馆,还有很多,我发现它们很简单,而且做得很好。

 public static Bitmap yourBitmap;
   public class MovementListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
...
//assign value to **yourBitmap** on item click
  yourBitamp = imageBitmap;
}

Access this yourBitmap from any place like- 从任何地方访问此yourBitmap-

 imageView.setImageBitmap(YourRecyclerViewAdapter.yourBitmap);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM