我有一个列表查看,这是在singleChoice模式。我只需要在旁边显示一个RadioButton,当点击突出显示它被选中时,以及当点击另一个时,一个回到未选择状态,并且新的状态被选中。为什么这么难?这不应该是这么复杂。我花了DAYS寻找一个适当的答案,我什么也没找到,所以我希望以清晰和简洁的方式问。单选按钮模式和自定义行布局的Android 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应用程序的源代码传递给我即可。 :)
所以这正是发生了什么。我不知道为什么其他答案不只是陈述这个简单的事实。在意识到我正在使用convertView并将其回收后,它非常明显。一两分钟内使用Google,我知道如何解决这个问题。我看了马克的书,第9章,“喜欢用列表”,他的RateList例子在那里详细说明,这很清楚地解释了它。任何有此问题的人都可以通过Mark Murphy查看Beginning Android的章节。 – 2010-11-22 23:37:05
Android中的这些怪癖使其成为一个陡峭的学习曲线,但一旦明白我们确实获得了回收的性能优势。 – 2010-11-23 15:12:43
因此,这并没有结束工作,我经历了它,而回收利用肯定是我看到的背后的原因,马克的书没有详细说明如何解决我遇到的具体问题。 – 2010-11-23 18:57:18