单击RecyclerView项时发送行ID

时间:2016-02-09 09:30:28

标签: android database android-recyclerview

我在资产文件夹中准备了一个数据库。我有一个recyclerView用于在数据库中显示标题。现在,我希望展示DetailActivity具有特定的id。我的意思是显示帖子的描述。我认为这可以用意图完成,但我不知道如何。

    public class FehrestRecyclerAdapter extends RecyclerView.Adapter<FehrestRecyclerAdapter.ViewHolder> {

    private Context ctx;
    private List<Post> posts;

    //constructor
    public FehrestRecyclerAdapter(Context ctx, List<Post> posts) {
        this.ctx = ctx;
        this.posts= posts;
    }

    //ViewHodler Pattern
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private TextView txtTitle;

        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);

            txtTitle= (TextView)itemView.findViewById(R.id.txtTitle);
        }

        @Override
        public void onClick(View v) {
            //Toast.makeText(v.getContext() , txtTitle.getText() , Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(itemView.getContext() , DetailActivity.class);
            intent.putExtra("postId" , "id");
            itemView.getContext().startActivity(intent);
        }
    }

    //OncreateViewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v  = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_row , parent , false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    //OnBindViewHolder
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        Post current = Post.get(position);

        holder.txtTitle.setText(current.getTitle());
    }

    //getItemCount method
    @Override
    public int getItemCount() {
        return posts.size();
    }
}

我的数据库中有3列

  

1.id 2.title 3.desc

和我的Post模型类中的3个方法:

  
      
  1. getId()2.getTitle()3.getDesc()
  2.   

你可以看到我可以向DetailActivity发送意图。但我不知道如何将特定ID发送到DetailActivity。感谢

2 个答案:

答案 0 :(得分:4)

在ViewHolder中创建一个变量id并在onBindViewHolder()中分配它,我根据它更新了代码。

public class FehrestRecyclerAdapter extends RecyclerView.Adapter<FehrestRecyclerAdapter.ViewHolder> {

private Context ctx;
private List<Post> posts;

//constructor
public FehrestRecyclerAdapter(Context ctx, List<Post> posts) {
    this.ctx = ctx;
    this.posts= posts;
}

//ViewHodler Pattern
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    private TextView txtTitle;
    private int id;

    public ViewHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);

        txtTitle= (TextView)itemView.findViewById(R.id.txtTitle);
    }

    @Override
    public void onClick(View v) {
        //Toast.makeText(v.getContext() , txtTitle.getText() , Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(itemView.getContext() , DetailActivity.class);
        // here pass id through intent
        intent.putExtra("postId" , id);
        itemView.getContext().startActivity(intent);
    }
}

//OncreateViewHolder
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v  = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_row , parent , false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

//OnBindViewHolder
@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    Post current = Post.get(position);

    holder.txtTitle.setText(current.getTitle());

    // add this line
    holder.id = Post.get(postion).getId();

}

//getItemCount method
@Override
public int getItemCount() {
    return posts.size();
}
}

答案 1 :(得分:2)

  

如何实现向DetailActivity发送特定ID

使用setTag TextView的holder.txtTitle方法用于在TextView中存储id并在View点击内部调用getTag点击:

1。使用setTag onBindViewHolder保存current

  holder.txtTitle.setTag(current);
  holder.txtTitle.setText(current.getTitle());

2。在onClick上使用getTag作为:

   @Override
    public void onClick(View v) {
     txtTitle= (TextView)v.findViewById(R.id.txtTitle);
     Post current=(Post)txtTitle.getTag();
     intent.putExtra("postId" , current.getId());
     intent.putExtra("postTitle" , current.getTitle());
     ....
   }