1

I try to get a key from the main class but it is Always getting null

 Bundle bundle =new Bundle();
 String type = bundle.getString("theType");



if (type =="Check"){

    view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_note_check, parent, false);

}else if (type =="photo"){
    view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_note_photo, parent, false);
}else {view = LayoutInflater.from(parent.getContext())
         .inflate(R.layout.item_note, parent, false);
}

to get kType from

public void onRadioButtonTypeClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();
Bundle bundle = new Bundle();

   // Intent intent = new Intent();
    kType="photo";
    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radioButtonSheet:
            if (checked)
                mImageNoteV.setImageResource(R.drawable.radio_button_note);
            kType= "note";
            bundle.putString("theType",kType);
         //   intent.putExtra("theType",kType);
            break;
        case R.id.radioButtonDone:
            if (checked)
                mImageNoteV.setImageResource(R.drawable.radio_button_check_box);
            kType="Check";
            bundle.putString("theType",kType);
        //   intent.putExtra("theType",kType);
            break;
        case R.id.radioButtonPicture:
            if (checked)
                mImageNoteV.setImageResource(R.drawable.radio_button_photo);
            kType = "photo";
            bundle.putString("theType",kType);
         //   intent.putExtra("theType",kType);
            break;
    }

i try to set it in other class and get it again from it but does not work = null. is there good way to solve this

Hassan
  • 13
  • 2

2 Answers2

0

Your question is already answered here.

In this case you can use just Intent for sending data between classes.

Here is a sample of using intent:

FirstActivity:

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("theType", YOUR_TYPE_HERE);
startActivity(intent);

SecondAcivity

String type = getIntent.getStringExtra("theType");

But if you want to use Bundle instead, here is sample:

FirstActivity:

Bundle bundle = new Bundle();
bundle.putString("theType", YOUR_TYPE_HERE);
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtras(bundle);
startActivity(intent);

SecondActivity:

Bundle bundle = getIntent().getExtras();
String type = bundle.getString("theType");
0

For this, you need to initialize two class variables of type boolean in your activity like the following.

public static boolean defaultType = false;
public static boolean noteCheck = false;

then do changes as per your requirement (eg: 'noteCheck = true' represent kType="Check" and 'noteCheck = false' represent kType="Photo" and 'defaultType = true' represents kType="note")

// Check which radio button was clicked
switch(view.getId()) {
    case R.id.radioButtonSheet:
        if (checked)
            mImageNoteV.setImageResource(R.drawable.radio_button_note);
        defaultType= true;
        break;
    case R.id.radioButtonDone:
        if (checked)
            mImageNoteV.setImageResource(R.drawable.radio_button_check_box);
        noteCheck= true;
        break;
    case R.id.radioButtonPicture:
        if (checked)
            mImageNoteV.setImageResource(R.drawable.radio_button_photo);
        noteCheck = false;
        break;
}

Then in your Adapter class call them by class name and set the layout as follows:

View view;
    if (defaultType){
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note, parent, false);
    }else {
        view = LayoutInflater.from(context).inflate(noteCheck ? R.layout.item_note_check : R.layout.item_note_photo, parent, false);
    }

Don't forget to call the variables by their class name as they are class variables. The way worked for me, hope it helps you.

Sujeet
  • 558
  • 8
  • 13
  • i have three Different ItemLayout not only tow . and i don't understand the Fifth line in what you write about adabter class ` view = LayoutInflater.from(context).inflate(noteCheck ? R.layout.item_note_check : R.layout.item_note_photo, parent, false);` – Hassan Feb 15 '20 at 12:26
  • Yes, it's for showing three layouts if in Third line there's one layout that would be showing if kType = note (in your logic). And in Fifth line there I'm using ternary operator as: if noteCheck is true (in your logic if kType="Check" ) then it'll be showing R.layout.item_note_check and if noteCheck = false (in your logic kType="Photo" ) then it'll be showing R.layout.item_note_photo. You just need to set these boolean variables (defaultType,noteCheck ) from your activity as you are setting variable kType. – Sujeet Feb 17 '20 at 06:03