简体   繁体   中英

Go back to previous Activity with some `put extra` onClick of a recyclerView Item

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);
            }
        }
    }

}

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; } 

    }

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

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!

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM