我有一个NavigationTab活动,它包含三个片段。第一个片段包括feed的列表视图。单击单个Feed上的按钮时,它会打开包含详细信息Feed的新活动。我想将数据从活动传递回片段,以便它可以更新适配器。我在传递数据方面遇到了麻烦。请帮忙。 下面是调用第一个片段的代码。
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_feed:
android.support.v4.app.FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
ft.replace(R.id.myframe, new FeedFragment())
.addToBackStack(null)
.commit();
return true;
case R.id.navigation_attendance:
//fragment = new AttendanceFragment();
//loadFragment(fragment);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
从适配器启动新活动的代码如下:
commentBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout vParent = (LinearLayout)v.getParent();
LinearLayout vvParent = (LinearLayout)vParent.getParent();
LinearLayout vvchild = (LinearLayout)vvParent.getChildAt(5);
TextView likesView = (TextView)vvchild.getChildAt(0);
int spaceIndex = likesView.getText().toString().indexOf(" ");
int like = Integer.parseInt(likesView.getText().toString().substring(0, spaceIndex));
String tags[] = vvParent.getTag().toString().split(" ");
final int position = Integer.parseInt(tags[2].split(":")[1]);
FeedItem f = feedItems.get(position);
Log.e(TAG, f.getLiked_by().toString());
Intent intent = new Intent(activity.getApplicationContext(), CommentActivity.class);
intent.putExtra("feedObj", f);
activity.startActivity(intent);
}
});
如何从CommentActivity传回数据?