gpt4 book ai didi

android - 不显示 RadioGroup 的 ListView

转载 作者:行者123 更新时间:2023-11-30 02:08:18 26 4
gpt4 key购买 nike

我想创建一个 ListView,它包含具有 CheckBox 的列表项和一个带有 2 个 RadioButtonsRadioGroup >。

我不确定为什么,但是 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"
/>

这是自定义适配器类:

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 代码供您查看。我怀疑那部分代码有什么错误。

这是一张图片:

enter image description here

最佳答案

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

主 Activity .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;
}
}

交互.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;
}

}

关于android - 不显示 RadioGroup 的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30438132/

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