如何在 RecyclerView 中实现 StartActivityForResult
Posted
技术标签:
【中文标题】如何在 RecyclerView 中实现 StartActivityForResult【英文标题】:How to implement StartActivityForResult in RecyclerView 【发布时间】:2018-07-19 16:07:21 【问题描述】:我正在尝试在我的 RecyclerView 中使用 startActivityForResult 并且不知道如何...尝试了这种方式,我从 Activity 获得了 NullPointExeption,有什么办法可以解决它。
这是活动:
RecyclerView recyclerView=findViewById(R.id.todo_Recyclerview);
Todo_Adapter adapter=new Todo_Adapter(this,Todo_DataGenerator.getData(this));
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
recyclerView.setAdapter(adapter);
TextView title=findViewById(R.id.todo_txt_Titleitem);
TextView content=findViewById(R.id.todo_txt_Contentitem);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (requestCode== Request &&
resultCode==RESULT_OK &&
data !=null)
String Title=data.getStringExtra(Todo_saveActivity.Todo_Title);
String Content=data.getStringExtra(Todo_saveActivity.Todo_Content);
title.setText(Title);
content.setText(Content);
这是适配器:
public class Todo_Adapter extends RecyclerView.Adapter<Todo_Adapter.Todo_itemViewHolder>
public static final int Request = 1002;
private Context context;
private List<Todo_SampleClass> Notes;
public Todo_Adapter(Context context, List<Todo_SampleClass> Notes)
this.context = context;
this.Notes = Notes;
@Override
public Todo_itemViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.todo_rv_item, parent, false);
return new Todo_itemViewHolder(view);
@Override
public void onBindViewHolder(Todo_itemViewHolder holder, int position)
holder.bindNotes(Notes.get(position));
holder.Add.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent=new Intent(context, Todo_saveActivity.class);
((Activity) context).startActivityForResult(intent,Request);
);
@Override
public int getItemCount()
return Notes.size();
public class Todo_itemViewHolder extends RecyclerView.ViewHolder
private TextView Title;
private TextView Content;
private ImageButton edit;
private ImageButton remove;
private Button Add;
public Todo_itemViewHolder(View itemView)
super(itemView);
Title = itemView.findViewById(R.id.todo_txt_Titleitem);
Content = itemView.findViewById(R.id.todo_txt_Contentitem);
edit = itemView.findViewById(R.id.todo_btn_edit);
remove = itemView.findViewById(R.id.todo_btn_remove);
Add = itemView.findViewById(R.id.todo_btn_add);
public void bindNotes(Todo_SampleClass Notes)
Title.setText(Notes.getTitle());
Content.setText(Notes.getContent());
edit.setImageDrawable(Notes.getImgbtn_edit());
remove.setImageDrawable(Notes.getImgbtn_remove());
这是我要参加的第二个活动:
public class Todo_saveActivity extends AppCompatActivity
public static final String Todo_Title ="result";
public static final String Todo_Content="content";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todo_save);
final EditText Title=findViewById(R.id.todo_name);
final EditText Content=findViewById(R.id.todo_family);
这是 logcat:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfocom.example.expert2.expert2/com.example.expert2.expert2.rv_Todo.Todo_rv: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:117)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:54)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:202)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.expert2.expert2.rv_Todo.Todo_rv.<init>(Todo_rv.java:29)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1130)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
谢谢。---------------------------------------------- ---------------------
【问题讨论】:
在你的适配器中,你从哪里得到变量context
?
【参考方案1】:
查看这段代码时:
public static final int Request = 1002;
@Override
public void onBindViewHolder(Todo_itemViewHolder holder, int position)
holder.bindNotes(Notes.get(position));
holder.Add.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent=new Intent(context, Todo_saveActivity.class);
((Activity) context).startActivityForResult(intent,Request);
);
在我看来,((活动)上下文)似乎是问题所在。您正在尝试使用为空的 Activity 对象调用 startActivityForResult(...) 。根据您的 RecyclerView 是在 Activity 中还是在 Fragment 中,请确保您将 context
设置为等于父 Activity 上下文的有效实例,例如:
Context context = getContext(); //if in fragment
Context context = (Context)this; //if in parent activity
然后你应该可以打电话了
((Activity) context).startActivityForResult(intent,Request);
没有任何问题。
【讨论】:
谢谢...家长活动你的意思是第一个活动?我的 RecyclerView 是活动,但不是第一个......我应该在哪里添加? 如果您使用的是片段,则父活动是片段所在的活动。【参考方案2】:如果您愿意使用回调来执行此操作,而不是直接在您的活动中执行此操作...
创建一个这样的界面:
public interface OnBindCallback
void onViewBound(Todo_itemViewHolder viewHolder, int position);
然后,在您的适配器中:
public OnBindCallback onBind;
@Override public void onBindViewHolder(Todo_itemViewHolder holder, int position)
if (onBind != null)
onBind.onViewBound(holder, position);
最后,在你的活动中:
adapter.onBind = (viewHolder, position) ->
viewHolder.itemView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Intent intent=new Intent(YourActivityName.this, Todo_saveActivity.class);
YourActivityName.this.startActivityForResult(intent, 1002);
);
;
【讨论】:
以上是关于如何在 RecyclerView 中实现 StartActivityForResult的主要内容,如果未能解决你的问题,请参考以下文章
如何在基于 SQLite 的 recyclerview 中实现 searchview? - 安卓
从片段中获取意图值后,我如何在 recyclerview 项目中实现单击
如何使用 Kotlin 在 RecyclerView Adapter 中实现 onClick 并进行数据绑定
在滚动列表中实现视频的播放(ListView & RecyclerView)