gpt4 book ai didi

java - 如何更改 Adapter 类中的 itemLayout

转载 作者:太空宇宙 更新时间:2023-11-04 09:05:53 25 4
gpt4 key购买 nike

我尝试从主类获取 key ,但它总是为空

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

我尝试在其他类中设置它并从中再次获取它,但不起作用= null。有什么好的办法解决这个问题

最佳答案

为此,您需要在 Activity 中初始化两个 boolean 类型的类变量,如下所示。

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 类中通过类名调用它们并按如下方式设置布局:

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

不要忘记通过类名来调用变量,因为它们是类变量。这个方法对我有用,希望对你有帮助。

关于java - 如何更改 Adapter 类中的 itemLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60236931/

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