简体   繁体   中英

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. 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. How am I supposed to pass that data to another activity?

Here is what I have so far:(CustomAdapter class)

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?

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

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

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

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.

For large objects like Bitmaps , you can use any of the libraries out there to do the fetching and automatic caching for you. And you only need to persist the URL or path of the image in your DB not the whole byte array.

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.

 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-

 imageView.setImageBitmap(YourRecyclerViewAdapter.yourBitmap);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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