Red Virus Red Virus - 26 days ago 5
Android Question

Values from the Intent is not showing

I don't know what I did wrong over here. I have followed the tutorial but my values end up being empty.

Passsing values to the Intent

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


Reviving value from the 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();
}
}

Answer

Try replace onCreate method. What was the issue? As @ARiF has highlighted correctly, you are passing long from your Intent but in your method you are retrieving the String.

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