1

I want to pass an data previous activity on click of Item in Recycler view and show it on a Edit Text.

This is the code i have used to pass data from listview to the previous activity

I want to do the same thing with Recyclerview

//Calling Second Activity

public static final int REQUEST_CODE = 100;
Intent dateintent = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(dateintent, REQUEST_CODE);

//onClick of listview pass the data back to previous activity

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView txt = (TextView) view.findViewById(R.id.textView);
                String str = txt.getText().toString();

                Intent intent = new Intent();
                intent.putExtra("data",str);
                setResult(RESULT_OK,intent);
                finish();

            }

});

//After getting data show the data in the first activity edit box

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            String data= data.getStringExtra("data");
            if (data!= null) {
                edittext.setText(data);
            }
        }
    }

}

Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
Sachin rv
  • 11
  • 5
  • Passing Data from one activity to previous activity i have tried, when i click on list-view item the data will be passed back to previous activity text-box. I want to do the same thing using recycler view – Sachin rv Apr 24 '15 at 04:38

4 Answers4

1

First create this Interface

public interface RunnableValue {

public void run(Object obj);
}

2.This MainActivity add

 RunnableValue run=new RunnableValue() {
        @Override
        public Bundle run(Object obj) {

             String str = obj.toString();

            Intent intent = new Intent();
            intent.putExtra("data",str);
            setResult(RESULT_OK,intent);
            finish();
          }
    };
    mAdapter = new SearchAdapter(dataSet,run);
  1. This RecyclerView Adapter

    public SearchAdapter(List<String>  dataSet,RunnableValue runnableValue) {
        mDataSet = dataSet;
        this.runnableValue=runnableValue;
    }
    public static class SearchHolder extends RecyclerView.ViewHolder {
    private final TextView textView;
    
    public SearchHolder(View v) {
        super(v);
    
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                runnableValue.run(getTextView().toString());
            }
        });
        textView = (TextView) v.findViewById(R.id.txtSearchItem);
    }
    
    public TextView getTextView() {
        return textView;
    }
    

    }

Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
0

Follow the Jacob's solution here. This adds listener for RecyclerView. Then, do the same as you have done in ListView.

Community
  • 1
  • 1
Hawk-Eye
  • 400
  • 1
  • 7
  • 23
0

There is no setOnItemClickListener available in RecyclerView, so you need make your own click listener in your RecyclerView adaper, just check out the post, then you should be able to make it.

Hope this help!

Community
  • 1
  • 1
Xcihnegn
  • 11,579
  • 10
  • 33
  • 33
0

RecyclerView doesn't have a setOnItemClickListener like its predecessor ListView did. However, that shouldn't prevent us from doing what we want to do. So, we reinvent the wheel and make our very own OnItemClickListener for your RecyclerView. Here's a step by step guide.

  1. Create an interface called OnItemClickListener by creating a new file called OnItemClickListener.java with an empty method called onItemClick.

    public interface OnItemClickListener {
         public void onItemClick(View view , int position);
     }
    
  2. Create a static variable in your adapter called

    static OnItemClickListener mItemClickListener;
    
  3. Setup onClickListener in your custom ViewHolder with a call to our onItemClick method like so

    @Override
    public void onClick(View view) {
       mItemClickListener.onItemClick(view, getPosition());
    }
    
  4. Create a public method called SetOnItemClickListener in your adapter class

    public void SetOnItemClickListener(final OnItemClickListener mItemClickListener)
    {
    this.mItemClickListener = mItemClickListener;
    }
    
  5. SetOnItemClickListener on your custom RecyclerView Adapter

    ((NameOfYourAdapter) mAdapter).SetOnItemClickListener(new OnItemClickListener() {
    
                @Override
                public void onItemClick(View view, int position) {
                    if(view != null)
                    {
            TextView txt = (TextView) view.findViewById(R.id.textView);
            String str = txt.getText().toString();
                        Intent intent = new Intent();
                        intent.putExtra("data",str);
                        setResult(RESULT_OK, intent);
                        //close this Activity...
                        finish();
                    }
                }
            });
    

That should do it. If you have any questions, feel free to ask!

Advait Saravade
  • 3,029
  • 29
  • 34