我有一个ListView,它处于单选模式。我想要的只是在旁边显示一个RadioButton,当点击时高亮以表示已选择,当点击不同的选项时,该选项返回未选择状态,新的选项则变为已选中状态。这为什么这么难?这不应该这么复杂。我花了几天的时间寻找适当的答案,但找不到,所以我希望能够清晰明了地问出来。
我的listview布局:(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的书。然而,评论现在已经过时,他的存储库已经发生了很大的变化,那些不再是正确的答案了。
所以,有人知道如何获得预期的功能吗?或者失败了,只需将Gmail应用程序的源代码传递给我。 :)