gpt4 book ai didi

android - 无法在新 Activity 中显示来自先前 fragment 的数据

转载 作者:行者123 更新时间:2023-11-30 01:03:20 25 4
gpt4 key购买 nike

我在点击时在新 Activity 中显示来自先前 fragment 的字符串时遇到了一些问题。该字符串显示为“isnull”。

任何帮助将不胜感激。相关代码如下。

技能 Activity :

    public class Skill extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_skill);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Intent in = getIntent();
String skill = in.getStringExtra("skill");

TextView dispSkill = (TextView) findViewById(R.id.skill);
dispSkill.setText("this skill is" + skill);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

}

RVAdapter(前一个 fragment 的):

public static class AssessViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {

CardView cv;
TextView assessName;
TextView pracName;
TextView required;
TextView reqnum;

AssessViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
assessName = (TextView)itemView.findViewById(R.id.assessment);
pracName = (TextView) itemView.findViewById(R.id.details);
required = (TextView) itemView.findViewById(R.id.required);
reqnum = (TextView) itemView.findViewById(R.id.reqnum);

itemView.setOnClickListener(this);
}

@Override
public void onClick(View v) {
Context context = v.getContext();

Intent intent = new Intent(context, Skill.class);
context.startActivity(intent);

String assessName = ((TextView) v.findViewById(R.id.assessment))
.getText().toString();
intent.putExtra(KEY_SKILL, assessName);

System.out.println(assessName);
}
}

最佳答案

正如我在您的问题下方的评论中指出的那样,问题是您在将数据附加到 Intent 之前调用 startActivity,因此将 context.startActivity(intent); 移动到 的末尾onClick 方法应该可以工作

@Override
public void onClick(View v) {
Context context = v.getContext();

Intent intent = new Intent(context, Skill.class);

String assessName = ((TextView) v.findViewById(R.id.assessment))
.getText().toString();
intent.putExtra(KEY_SKILL, assessName);

System.out.println(assessName);
context.startActivity(intent);
}

关于android - 无法在新 Activity 中显示来自先前 fragment 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39174416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com