Android - 根据在RecyclerView中单击的位置更新textview

时间:2017-02-03 09:16:59

标签: android textview android-recyclerview android-adapter

目前我有一个RecyclerView,可以打开Activity2 / activity2.xmlactivity2.xml有一个textview。

我的问题是:如何根据我在Recyclerview中点击的项目,在activity2.xml中设置文字?我也想设置一个图像,但现在可以等了。

我使用实现RecyclerView.ViewHolder的{​​{1}}。 这是我的View.OnClickListener

RecyclerPracticeAdapter.java

RecyclerView.Adapter

activity2.xml

public class RecyclerPracticeAdapter extends RecyclerView.Adapter<MyViewHolder> {

Context c;
ArrayList<Category> category;

public RecyclerPracticeAdapter(Context c, ArrayList<Category> category) {
    this.c = c;
    this.category = category;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.model, null);
    MyViewHolder holder = new MyViewHolder(v);
    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    holder.nameTxt.setText(category.get(position).getName());
    holder.img.setImageResource(category.get(position).getImage());

    //Listners
    //THIS IS WHERE YOU CAN CHOOSE TO OPEN A ACTIVITY WHEN USER CLICK ON A CARDVIEW!!
    holder.setItemClickListener(new ItemClickListener() {


        @Override
        public void onItemClick(View v, int pos) {
           switch (pos) {
                case 0:
                  // Open New Activity......
                  // THIS IS WHERE I WANT TO CHANGE TEXT activity2.xml
                  Intent intent=new Intent(v.getContext(),Activity2.class);
                    v.getContext().startActivity(intent);

                    break;
                case 1:
                  Intent intent=new Intent(v.getContext(),Activity2.class);
                    v.getContext().startActivity(intent);

                    break;


            }

        }
    });
}

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

有人可以指出我正确的方向吗?

EDIT :::

MyViewHolder:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Update Text"
        android:id="@+id/txt_update"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center_horizontal" />

</RelativeLayout>

类别:

public class MyViewHolder extends RecyclerView.ViewHolder implements  View.OnClickListener {

public ImageView img;
public TextView nameTxt;
ItemClickListener itemClickListener;

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

    nameTxt= (TextView) itemView.findViewById(R.id.nameTxt);
    img= (ImageView) itemView.findViewById(R.id.ppImage);

    itemView.setOnClickListener(this);

}

public void  setItemClickListener(ItemClickListener ic){

    this.itemClickListener=ic;
}

@Override
public void onClick(View v) {
    this.itemClickListener.onItemClick(v,getLayoutPosition());
}
}

2 个答案:

答案 0 :(得分:2)

1.意图在onClick of item中传递数据。

Intent intent=new Intent(v.getContext(),Activity2.class);
intent.putExtra("YOUR_KEY", category.get(pos).getName());  
v.getContext().startActivity(intent);

2.获取Activity2

中的数据
Bundle bundle = getIntent().getExtras();
if(bundle!=null){
  String text = bundle.getString("YOUR_KEY");
  if(text!=null)
    YOUR_TEXT_VIEW.setText(text);
}

答案 1 :(得分:0)

您只需要传递数据

    Intent intent=new Intent(v.getContext(),Activity2.class);
  intent.putExtra("value",category.get(pos).getName());
  v.getContext().startActivity(intent);

在Activity2中你需要取值

String updatevalue = getIntent()。getExtras()。getString(&#34; value&#34;);             TextView.setText(updatevalue);