簡體   English   中英

為每個RecyclerView項目創建DetailsActivity

[英]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

但是,如果您的物體較小,重量較輕,這是適當的,因為如果物體較大,您將得到

  • 由於正在進行序列化過程,因此您的活動開始時會緩慢
  • 或因您的意圖大小超出Android允許的大小而崩潰

另一種選擇是,僅將某種標識符傳遞給活動,並使用該標識符將對象加載到活動中。 因此,您可以傳遞數據庫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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM