【发布时间】:2011-05-14 03:07:43
【问题描述】:
我有一个ListView,它处于单选模式。我想要的只是在侧面显示一个 RadioButton,当单击它时突出显示它已被选中,当单击另一个按钮时,它会返回未选中状态并选择新的按钮。为什么这么难?这不应该这么复杂。我花了 DAYS 天来寻找合适的答案,但我一无所获,所以我希望清楚而简洁地问。
我的列表视图布局(R.layout.view_orders):
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:choiceMode="singleChoice"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="@drawable/list_divider"
android:dividerHeight="1px"
android:focusable="false"
android:focusableInTouchMode="false"
android:cacheColorHint="#00000000">
</ListView>
我的自定义行(R.layout.orders_row):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res/com.xxx.xxxxxx"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="6dip">
<com.xxx.xxxxxx.VerticalLabelView
app:text="SHORT"
app:textColor="#666"
app:textSize="14sp"
android:id="@+id/state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/state"
android:layout_centerVertical="true"
android:gravity="center"
android:textSize="40sp"
android:layout_margin="2dip"
android:minWidth="30dip"
android:textColor="#555" />
<RelativeLayout
android:layout_toRightOf="@id/quantity"
android:layout_centerVertical="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/instrument"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#333"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
/>
<TextView
android:id="@+id/deets"
android:layout_below="@id/instrument"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#888"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
/>
</RelativeLayout>
<RadioButton
android:id="@+id/selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
我的 onCreate() 方法:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.view_orders);
client = new Client(handler);
ola = new OrdersAdapter(this, R.layout.orders_row, Orders);
setListAdapter(ola);
final RelativeLayout loading = (RelativeLayout) findViewById(R.id.loading);
panel = (PositionsPanel) findViewById(R.id.panel);
Utility.showProgressBar(loading);
client.Connect("orders");
}
现在底层的一切都按预期工作了,你点击一个单选按钮,通过它的标签,我可以从列表中适当地选择那个项目,并按照我想要的方式操作它。但是,当单击第一个单选按钮时,将选择最后一个单选按钮。再次单击相同的单选按钮,它现在也被选中。再次单击它,没有任何反应,最后一个和第一个都被选中。现在我单击列表中的任何其他人,它会像预期的那样被选中。单击任何一个选定的单选按钮,没有任何反应,单选按钮保持选中状态。
我尝试在 onCreate() 中使用以下内容:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.view_orders);
client = new Client(handler);
ola = new OrdersAdapter(this, android.R.layout.simple_list_item_single_choice, Orders);
setListAdapter(ola);
final RelativeLayout loading = (RelativeLayout) findViewById(R.id.loading);
panel = (PositionsPanel) findViewById(R.id.panel);
Utility.showProgressBar(loading);
client.Connect("orders");
}
这根本不显示单选按钮。 太棒了。
现在也许(阅读:最有可能),我只是很密集,无法弄清楚这一点,但我看到这个问题被问了很多,但没有真正的答案。大量参考其他教程或 Commonsware 人的书。但是,现在 cmets 已经过时了,而且他的存储库已经发生了很大变化,以至于这些不再是正确的答案。
那么,有没有人知道如何从中获得预期的功能?或者如果失败了,只需将 Gmail 应用程序的源代码传给我即可。 :)
【问题讨论】:
标签: android listview radio-button listviewitem