返回上一个Activity,其中有一些`put extra` onClick of recyclerView Item [英] Go back to previous Activity with some `put extra` onClick of a recyclerView Item

查看:160
本文介绍了返回上一个Activity,其中有一些`put extra` onClick of recyclerView Item的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过点击Recycler视图中的Item传递先前活动的数据,并在编辑文本上显示。

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

这是我用过的代码从listview到上一个活动的数据

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

我想用Recyclerview做同样的事情

I want to do the same thing with Recyclerview

//呼叫秒活动

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

// onClick listview将数据传回上一个活动

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

}

推荐答案

首先创建此接口

public interface RunnableValue {

public void run(Object obj);
}

2.This MainActivity add

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. 此RecyclerView适配器

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

}

这篇关于返回上一个Activity,其中有一些`put extra` onClick of recyclerView Item的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆