我不知道我在这里做错了什么。我已按照教程进行操作,但我的值最终为空。
将值传递给意图
ListView lvItems = (ListView) view.findViewById(R.id.lvDashboardCompleted);
lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Toast shows the id over here
Intent intent = new Intent(getActivity(), categoryActivity.class);
intent.putExtra("CATEGORY_ID", id);
startActivity(intent);
}
});
从意图中恢复价值
public class categoryActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_category);
String recevied_cat_id = getIntent().getStringExtra("CATEGORY_ID");
Context context = getApplicationContext();
CharSequence text = recevied_cat_id;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
答案 0 :(得分:1)
改为使用String recevied_cat_id = getIntent().getLongExtra("CATEGORY_ID", defaultLongValue);
。
答案 1 :(得分:1)
尝试替换onCreate
方法。问题是什么?由于@ARiF已正确突出显示,您从Intent传递的时间很长,但在您的方法中,您正在检索字符串。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_category);
Long recevied_cat_id = getIntent().getLongExtra("CATEGORY_ID", 1L);
Context context = getApplicationContext();
String strLong = Long.toString(recevied_cat_id);
CharSequence text = strLong;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}