我如何更改 Adapter class 中的 itemLayout
How can i change the itemLayout in Adapter class
我尝试从主 class 获取密钥,但它总是为空
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);
}
从
获取kType
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;
}
我尝试在其他 class 中设置它并从中再次获取它但不起作用 = null。有什么好的方法可以解决这个
您的问题已得到解答 here。
在这种情况下,您可以仅使用 Intent 在 类.
之间发送数据
这里是一个使用 intent 的例子:
FirstActivity:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("theType", YOUR_TYPE_HERE);
startActivity(intent);
SecondAcivity
String type = getIntent.getStringExtra("theType");
但如果您想使用 Bundle,这里是示例:
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");
为此,您需要在 activity 中初始化两个布尔类型的 class 变量,如下所示。
public static boolean defaultType = false;
public static boolean noteCheck = false;
然后根据您的要求进行更改(例如:'noteCheck = true' 表示 kType="Check" 并且 'noteCheck = false' 表示 kType="Photo" 并且 'defaultType = true' 表示 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;
}
然后在您的 Adapter 中 class 以 class 名称调用它们并设置如下布局:
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);
}
不要忘记用 class 名称来调用变量,因为它们是 class 变量。
方法对我有用,希望对你有帮助。
我尝试从主 class 获取密钥,但它总是为空
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);
}
从
获取kTypepublic 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;
}
我尝试在其他 class 中设置它并从中再次获取它但不起作用 = null。有什么好的方法可以解决这个
您的问题已得到解答 here。
在这种情况下,您可以仅使用 Intent 在 类.
之间发送数据这里是一个使用 intent 的例子:
FirstActivity:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("theType", YOUR_TYPE_HERE);
startActivity(intent);
SecondAcivity
String type = getIntent.getStringExtra("theType");
但如果您想使用 Bundle,这里是示例:
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");
为此,您需要在 activity 中初始化两个布尔类型的 class 变量,如下所示。
public static boolean defaultType = false;
public static boolean noteCheck = false;
然后根据您的要求进行更改(例如:'noteCheck = true' 表示 kType="Check" 并且 'noteCheck = false' 表示 kType="Photo" 并且 'defaultType = true' 表示 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;
}
然后在您的 Adapter 中 class 以 class 名称调用它们并设置如下布局:
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);
}
不要忘记用 class 名称来调用变量,因为它们是 class 变量。 方法对我有用,希望对你有帮助。