如何通过RecyclerView从string.xml中传递字符串

时间:2019-03-02 13:06:34

标签: android android-recyclerview

我有一个RecyclerView,它扩展了网格布局以显示图像和文本,它具有一个onclick侦听器,可导致第二个活动,在该活动中,我有两个由适配器发送的数据填充的视图,得到了要通过的图像,以及我通过

设置的静态文本
lstItem.add(new Item("Item1","Category",R.strings.item1Des,R.drawable.image1));

图像通过,但字符串不通过。

我的适配器类

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.*;
import android.widget.*;

import java.util.List;


public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

    private Context mContext ;
    private List<Item> mData ;


    public RecyclerViewAdapter(Context mContext, List<Item> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view ;
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        view = mInflater.inflate(R.layout.cardveiw_item_item,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        holder.tv_tem_title.setText(mData.get(position).getTitle());
        holder.img_item_thumbnail.setImageResource(mData.get(position).getThumbnail());
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(mContext,Item_Activity.class);

                // passing data to the book activity
                intent.putExtra("Title",mData.get(position).getTitle());
                intent.putExtra("Description",mData.get(position).getDescription());
                intent.putExtra("Thumbnail",mData.get(position).getThumbnail());
                // start the activity
                mContext.startActivity(intent);

            }
        });

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv_item_title;
        ImageView img_item_thumbnail;
        CardView cardView ;

        public MyViewHolder(View itemView) {
            super(itemView);

            tv_item_title = (TextView) itemView.findViewById(R.id.book_title_id) ;
            img_item_thumbnail = (ImageView) itemView.findViewById(R.id.item_img_id);
            cardView = (CardView) itemView.findViewById(R.id.cardview_id);


        }
    }

}

我的项目活动(在onclick之后打开):

private TextView tvtitle,tvdescription,tvcategory;
private ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_book_);

    tvtitle = (TextView) findViewById(R.id.txttitle);
    tvdescription = (EditText) findViewById(R.id.txtDesc);
    tvcategory = (TextView) findViewById(R.id.txtCat);
    tvdescription.setKeyListener(null);
    img = (ImageView) findViewById(R.id.itemthumbnail);

    Object clipboardService = getSystemService(CLIPBOARD_SERVICE);
    final ClipboardManager clipboardManager = (ClipboardManager)clipboardService;
    Button copyButton = (Button)findViewById(R.id.clipboard_copy_button);


    // Recieve data
    Intent intent = getIntent();
    String Title = intent.getExtras().getString("Title");
    String Description = intent.getExtras().getString("Description");
    int image = intent.getExtras().getInt("Thumbnail") ;

    // Setting values

    tvtitle.setText(Title);
    tvdescription.setText(Description);
    img.setImageResource(image);

    copyButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {  }
    });
}

我的物品分类

public class Item{

    private String Title;
    private String Category ;
    private int Description ;
    private int Thumbnail ;


    public Item(String title, String category, int description, int thumbnail) {
        Title = title;
        Category = category;
        Description = description;
        Thumbnail = thumbnail;
    }


    public String getTitle() {
        return Title;
    }

    public String getCategory() {
        return Category;
    }

    public int getDescription() {
        return Description;
    }

    public int getThumbnail() {
        return Thumbnail;
    }


    public void setTitle(String title) {
        Title = title;
    }

    public void setCategory(String category) {
        Category = category;
    }

    public void setDescription(int description) {
        Description = description;
    }

    public void setThumbnail(int thumbnail) {
        Thumbnail = thumbnail;
    }
}

4 个答案:

答案 0 :(得分:1)

在适配器类的onBindViewHolder()的这一行:

intent.putExtra("Description", mData.get(position).getDescription());

mData.get(position).getDescription()是一个整数,代表字符串资源中的id。因此它不是实际字符串。
更改为:

String desc = mContext.getResources().getString(mData.get(position).getDescription());
intent.putExtra("Description", desc);

答案 1 :(得分:0)

当您传递字符串的ID(即int),即R.strings.item1Des时,您必须首先从Intent Extras中获取ID,然后通过调用getString(R.strings.item1Des)方法来获取其值,像这样使用该字符串

String Description = getString(intent.getIntExtra("Description",0));    

答案 2 :(得分:0)

这应该有效。您的描述是Int(仅引用字符串资源)

// Recieve data
    Intent intent = getIntent();
    String Title = intent.getExtras().getString("Title");
    int Description = intent.getExtras().getInt("Description");
    int image = intent.getExtras().getInt("Thumbnail") ;

    // Setting values

    tvtitle.setText(Title);
    tvdescription.setText(getString(Description));
    img.setImageResource(image);

答案 3 :(得分:0)

(代表问题作者发布)

我使用的是getString(R.strings.id),而不仅仅是R.strings.Id。