不显示 RadioGroup 的 ListView

ListView with RadioGroup not displaying

我想创建一个 ListView,其中包含具有 CheckBoxRadioGroup 以及 2 RadioButtons 的列表项。

我不确定为什么,但是 TextViewCheckBox 正在显示,而 RadioGroup 没有。

我已查找问题,但找不到确切的问题。谁能帮我?

这是 XML 代码:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<View
    android:layout_width="match_parent"
    android:layout_height="1dip"
    android:id="@+id/hlInteraction"
    android:layout_alignParentBottom="true"
    android:background="@android:color/darker_gray" />

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/hlInteraction"
    android:orientation="horizontal"
    android:gravity="center"
    android:id="@+id/rgInteraction"
    >
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:text="@string/rb_on"
        android:id="@+id/rbTurnOff"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="16dp"
        android:paddingLeft="16dp"
        android:text="@string/rb_off"
        android:id="@+id/rbTurnOn"
        />
    </RadioGroup>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tvDeviceNameInteraction"
    android:layout_alignParentLeft="true"
    android:paddingLeft="16dp"
    android:textSize="24sp"
    android:layout_above="@id/rgInteraction"
    />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tvDeviceDistanceInteraction"
    android:layout_toRightOf="@id/tvDeviceNameInteraction"
    android:paddingLeft="16dp"
    android:textSize="24sp"
    android:layout_above="@id/rgInteraction"
    />
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:paddingRight="4dp"
    android:id="@+id/cbInteraction"
    android:layout_above="@id/rgInteraction"
    />

这里是自定义适配器 class:

public class InteractionAdapter extends ArrayAdapter<Interaction>
{
    private ArrayList<Interaction> mList;
    private Context mContext;

    /*
     * Creates the Adapter for the list
     */
    public InteractionAdapter(Context context, int layoutId, ArrayList<Interaction> list)
    {
        super(context,layoutId,list);
        mList = list;
        mContext = context;

    }
    /**
     * inflate view for each row
     */
    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        Interaction current = mList.get(position);
        /* if the given channel row view is not being updated*/
        if (v == null) {
            /* inflate layout */
            LayoutInflater vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.list_item_interaction, null, false);
        }

        /*get the component pointers */
        TextView tvInteractionDeviceName = (TextView) v.findViewById(R.id.tvDeviceNameInteraction);
        TextView tvInteractionDeviceDistance = (TextView) v.findViewById(R.id.tvDeviceDistanceInteraction);


        RadioButton rbOn = (RadioButton) v.findViewById(R.id.rbTurnOn);
        RadioButton rbOff =  (RadioButton) v.findViewById(R.id.rbTurnOff);
        rbOn.setChecked(true);

        CheckBox cbInteraction =  (CheckBox) v.findViewById(R.id.cbInteraction);

        Device d = current.getDevice();//Gets Device object and then BluettothDevice object

        tvInteractionDeviceName.setText(d.getDevice().getName());
        tvInteractionDeviceDistance.setText(String.valueOf(d.getRSSI()));

        return v;
    }

此外,如果我想获得 RadioButton 的状态,我基本上需要向它们添加标签作为一种标记形式,以便我可以将它们彼此区分开来。对吧?

编辑
在我刚刚发布 xml 代码供您查看的预览中,布局似乎工作正常。我怀疑那部分代码有什么错误。

这是一张图片:

首先我对你的布局做了一些改进

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<View
    android:id="@+id/hlInteraction"
    android:layout_width="match_parent"
    android:layout_height="1dip"
    android:layout_alignParentBottom="true"
    android:background="@android:color/darker_gray" />

<RadioGroup
    android:id="@+id/rgInteraction"
    android:layout_width="match_parent"
    android:id="@+id/radioGroup"
    android:layout_height="wrap_content"
    android:layout_above="@id/hlInteraction"
    android:gravity="center"
    android:orientation="horizontal"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >

    <RadioButton
        android:id="@+id/rbTurnOff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="asdasd" />

    <RadioButton
        android:id="@+id/rbTurnOn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="asdas" />
</RadioGroup>

<TextView
    android:id="@+id/tvDeviceNameInteraction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/rgInteraction"
    android:layout_alignParentLeft="true"
    android:paddingLeft="16dp"
    android:textSize="24sp" />

<TextView
    android:id="@+id/tvDeviceDistanceInteraction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/rgInteraction"
    android:layout_toRightOf="@id/tvDeviceNameInteraction"
    android:paddingLeft="16dp"
    android:textSize="24sp" />

<CheckBox
    android:id="@+id/cbInteraction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/rgInteraction"
    android:layout_alignParentRight="true"
    android:paddingRight="4dp" />

但是单选按钮的显示与此无关,我认为你必须使用radiogroup

 RadioGroup radiogroup = (RadioGroup) findViewById(R.id.radioGroup);

        radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // Radio button
                RadioButton rb1 = (RadioButton) findViewById(R.id.radioButtonID);
                if (rb1.isChecked()) {
                    Toast.makeText(MainActivity.this, "1 option selected", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this, "2 option selected", Toast.LENGTH_LONG).show();

                }
            }
        });

试试这个,不要使用@SuppressLint("InflateParams"),使用

convertView = inflater.inflate(R.layout.list_item_interaction, parent, false);

帕维尔, 首先,检查你的 xml 预览模式,看看你的单选按钮是否出现在特定的行中,如果没有,那么你的布局设计肯定是不正确的。 如果是这样,那么我们可以在布局上进行设计。

请这样尝试:

RadioGroup radioGroup = (RadioGroup)findViewById(r.id.rgInteraction);
// This will get the radiobutton in the radiogroup that is checked 
RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(radioGroup.getCheckedRadioButtonId());

像这样使用 Listner:

// This overrides the radiogroup onCheckListener 

 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
    { 
        public void onCheckedChanged(RadioGroup radioGroup, int checkedId)
        { 
            // This will get the radiobutton that has changed in its check state 
            RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(checkedId);
            // This puts the value (true/false) into the variable 
            boolean isChecked = checkedRadioButton.isChecked();
            // If the radiobutton that has changed in check state is now checked... 

            if (isChecked){ 
                Toast.makeText(InteractionAdapter.this, "Checked", Toast.LENGTH_LONG).show();
            } else{
                Toast.makeText(InteractionAdapter.this, "Unchecked", Toast.LENGTH_LONG).show();
            }
        } 
    }); 

我自己都试过了。这段代码工作正常,问题出在您的布局中。使用您使用的视图布局。这是完整的代码。

MainActivity.java

package com.example.helppeople;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {

ListView mLists;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mLists = (ListView) findViewById(R.id.list);
    ArrayList<Interaction> mList = new ArrayList<Interaction>();

    mList.add(new Interaction("123"));
    mList.add(new Interaction("456"));
    mList.add(new Interaction("789"));
    mList.add(new Interaction("012"));

    InteractionAdapter mAdapter = new InteractionAdapter(
            getApplicationContext(), mList);

    mLists.setAdapter(mAdapter);

    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

list_item_interaction.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

<RadioGroup
    android:id="@+id/rgInteraction"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >

    <RadioButton
        android:id="@+id/rbTurnOff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="asdasd" />

    <RadioButton
        android:id="@+id/rbTurnOn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="asdas" />
</RadioGroup>

<TextView
    android:id="@+id/tvDeviceNameInteraction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/rgInteraction"
    android:layout_alignParentLeft="true"
    android:paddingLeft="16dp"
    android:textSize="24sp" />

<TextView
    android:id="@+id/tvDeviceDistanceInteraction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/rgInteraction"
    android:layout_toRightOf="@id/tvDeviceNameInteraction"
    android:paddingLeft="16dp"
    android:textSize="24sp" />

<CheckBox
    android:id="@+id/cbInteraction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/rgInteraction"
    android:layout_alignParentRight="true"
    android:paddingRight="4dp" />

</RelativeLayout>

InteractionAdapter.java

package com.example.helppeople;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;

public class InteractionAdapter extends BaseAdapter {
private ArrayList<Interaction> mList;
private Context mContext;

/*
 * Creates the Adapter for the list
 */
public InteractionAdapter(Context context, ArrayList<Interaction> list) {
    mList = list;
    mContext = context;

}

/**
 * inflate view for each row
 */
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    Interaction current = mList.get(position);
    /* if the given channel row view is not being updated */
    if (v == null) {
        /* inflate layout */
        LayoutInflater vi = LayoutInflater.from(mContext);
        v = vi.inflate(R.layout.list_item_interaction, null, false);
    }

    /* get the component pointers */
    TextView tvInteractionDeviceName = (TextView) v
            .findViewById(R.id.tvDeviceNameInteraction);
    TextView tvInteractionDeviceDistance = (TextView) v
            .findViewById(R.id.tvDeviceDistanceInteraction);

    RadioButton rbOn = (RadioButton) v.findViewById(R.id.rbTurnOn);
    RadioButton rbOff = (RadioButton) v.findViewById(R.id.rbTurnOff);
    rbOn.setChecked(true);

    CheckBox cbInteraction = (CheckBox) v.findViewById(R.id.cbInteraction);

    // Gets Device object and then BluettothDevice object

    tvInteractionDeviceName.setText("abc");
    tvInteractionDeviceDistance.setText("xyxz");

    return v;
}

@Override
public int getCount() {
    return mList.size();
}

@Override
public Object getItem(int arg0) {
    return mList.get(arg0);
}

@Override
public long getItemId(int arg0) {
    return arg0;
    }
}

Interaction.java

package com.example.helppeople;

public class Interaction {

String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Interaction(String name) {
    super();
    this.name = name;
    }

}