繁体   English   中英

如何更改 Adapter 类中的 itemLayout

[英]How can i change the itemLayout in Adapter 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;
    }

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

您的问题已在此处得到解答。

在这种情况下,您可以仅使用Intent在类之间发送数据。

以下是使用意图的示例:

第一活动

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

第二个活动

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

但是如果你想改用Bundle ,这里是示例:

第一活动

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

第二个活动

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

为此,您需要在您的活动中初始化两个 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);
    }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM