Android 不显示RadioGroup的ListView
我想创建一个Android 不显示RadioGroup的ListView,android,listview,android-arrayadapter,Android,Listview,Android Arrayadapter,我想创建一个列表视图,其中包含带有复选框的列表项和带有2个单选按钮的放射组 我不知道为什么,但是显示了TextView和复选框,而RadioGroup没有显示 我查过这个问题,但找不到确切的问题。有人能帮我吗 这是XML代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height=
列表视图
,其中包含带有复选框的列表项
和带有2个单选按钮的放射组
我不知道为什么,但是显示了TextView
和复选框,而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"
/>
下面是自定义adatper类:
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;
}
公共类交互适配器扩展了ArrayAdapter
{
私有数组列表;
私有上下文;
/*
*为列表创建适配器
*/
公共交互适配器(上下文、int-layoutId、ArrayList列表)
{
超级(上下文、布局ID、列表);
mList=列表;
mContext=上下文;
}
/**
*为每行充气视图
*/
@SuppressLint(“充气参数”)
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
交互电流=mList.get(位置);
/*如果未更新给定的通道行视图*/
如果(v==null){
/*充气布局*/
LayoutInflater vi=LayoutInflater.from(getContext());
v=vi.充气(R.布局.列表\项目\交互,空,假);
}
/*获取组件指针*/
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(真);
复选框cbInteraction=(复选框)v.findviewbyd(R.id.cbInteraction);
Device d=current.getDevice();//获取设备对象,然后获取BlueToThDevice对象
tvInteractionDeviceName.setText(d.getDevice().getName());
tvInteractionDeviceDistance.setText(String.valueOf(d.getRSSI());
返回v;
}
另外,如果我想获得单选按钮的状态,我基本上需要添加标签作为一种标签形式,这样我就可以区分其中一种,对吗
编辑
在我刚刚发布的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" />
试试这个,不要使用@SuppressLint(“InflateParams”),使用
帕维尔,
首先,在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());
// 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();
}
}
});
像这样使用Listner:
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());
// 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);
}
}
告诉我它是否有帮助,希望它有帮助…我还没有测试运行这段代码,但我确实认为这是问题所在,更正后的XML代码没有帮助。而且不确定这段代码的第二部分有什么帮助…也尝试了它,不起作用…@PavlePavlov看看它是否有帮助,一定要让我知道。我已经编辑了我的答案,不要使用@SuppressLint(“InflateParams”)但是使用convertView=inflater.inflate(R.layout.list\u item\u交互,父项,false);而不是仍然是相同的结果。:/等等,我会自己尝试,然后发布答案。请为此添加一些代码。这不会被接受为答案。您需要为此添加一些代码。我已发布了一个新的答案。此代码正在运行。感谢您的努力,您的解决方案解决了我的问题。我非常感谢!:@PavlePavlov乐于帮助
<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>
<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>
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;
}
}
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;
}
}