我在radiogroup中創建了兩個單選按鈕的自定義列表視圖。單選按鈕沒有被選中,而在列表視圖中選擇單選按鈕
我的代碼getter和setter文件
Item_radio.java
package com.example.customlistview_radio;
public class Item_radio
{
public boolean isSelected_radio1;
public boolean isSelected_radio2;
public Item_radio(boolean radio1_status,boolean radio2_status)
{
this.isSelected_radio1=radio1_status;
this.isSelected_radio2=radio2_status;
}
public boolean isSelected_radio1() {
return isSelected_radio1;
}
public void setSelected_radio1(boolean isSelected_radio1) {
this.isSelected_radio1 = isSelected_radio1;
}
public boolean isSelected_radio2() {
return isSelected_radio2;
}
public void setSelected_radio2(boolean isSelected_radio2) {
this.isSelected_radio2 = isSelected_radio2;
}
}
MyRadioAdapter.java
package com.example.customlistview_radio;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
public class MyRadioAdapter extends BaseAdapter
{
Context context;
List<Item_radio> radioList;
public MyRadioAdapter(Context context,List<Item_radio> radiolist)
{
this.context=context;
this.radioList=new ArrayList<Item_radio>();
this.radioList.addAll(radiolist);
}
@Override
public int getCount() {
return radioList.size();
}
@Override
public Object getItem(int position) {
return radioList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder
{
RadioButton radio_button1;
RadioButton radio_button2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
View view = convertView;
if(view==null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.radio_button1=(RadioButton)view.findViewById(R.id.my_radio_but1);
holder.radio_button2=(RadioButton)view.findViewById(R.id.my_radio_but2);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
}
if (position % 2 == 1)
{
view.setBackgroundColor(Color.CYAN);
}
else
{
view.setBackgroundColor(Color.WHITE);
}
final Item_radio radio = (Item_radio) getItem(position);
holder.radio_button1.setChecked(radio.isSelected_radio1());
holder.radio_button2.setChecked(radio.isSelected_radio2());
holder.radio_button1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Log.e("button1","Clicked:button1");
RadioButton rb=(RadioButton)v;
radio.setSelected_radio1(rb.isChecked());
Log.e("button1","Clicked:button1:"+rb.isChecked());
}
});
holder.radio_button2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
RadioButton rb=(RadioButton)v;
Log.e("button2","Clicked:button2"+rb.isChecked());
radio.setSelected_radio2(rb.isChecked());
}
});
return view;
}
}
MainActivity.java
package com.example.customlistview_radio;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity
{
ListView mylist;
Button submit_but;
ArrayList<Item_radio> radio_list;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mylist=(ListView)findViewById(R.id.mylist_demo);
submit_but=(Button)findViewById(R.id.submit_but);
radio_list=new ArrayList<Item_radio>();
for(int i=0;i<20;i++)
{
radio_list.add(new Item_radio(false,false));
}
mylist.setAdapter(new MyRadioAdapter(MainActivity.this, radio_list));
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/my_radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/my_radio_but1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="radio1" />
<RadioButton
android:id="@+id/my_radio_but2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="radio2" />
</RadioGroup>
</RelativeLayout>
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.customlistview_radio.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
/>
<ListView
android:id="@+id/mylist_demo"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
tools:listitem="@layout/list_item"
android:layout_weight="0.3" >
</ListView>
<Button
android:id="@+id/submit_but"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="submit"
android:layout_weight="1.5"/>
</LinearLayout>
在選擇一些單選按鈕被選擇在列表中排單選按鈕。
如果我滾動列表視圖並選擇最後5或6項之間的單選按鈕,它將不會被選中。
問題
這是假設無線電按鈕1沒有被選擇的話,我選擇radiobutton2它被選中之後radiobutton1選擇。
我logcat的
03-04 10:22:17.221: E/button2(4665): Clicked:button2true
03-04 10:22:20.201: E/button2(4665): Clicked:button2true
03-04 10:22:21.221: E/button2(4665): Clicked:button2true
03-04 10:22:22.101: E/button2(4665): Clicked:button2true
03-04 10:22:23.711: E/button2(4665): Clicked:button2true
03-04 10:22:24.671: E/button2(4665): Clicked:button2true
03-04 10:22:25.711: E/button2(4665): Clicked:button2true
03-04 10:22:40.770: E/button2(4665): Clicked:button2true
03-04 10:22:42.371: E/button2(4665): Clicked:button2false
03-04 10:22:52.271: E/button2(4665): Clicked:button2false
請參閱我的要價較高的前8行正常工作。 當我點擊第9行的單選按鈕2沒有被選中時,打印檢查狀態爲false,但是我選擇了按鈕,並且選中的外觀在未選定模式下仍未更改。
我做錯了什麼? 請幫我解決問題。 謝謝...
感謝您的回答@Shree克里希納。我嘗試你的建議。如果我這樣做,當我滾動列表視圖列表視圖中的單選按鈕自動被選中。 – Asha
@Asha我更新了我的答案,看看。如果發生任何錯誤,請留下評論。我目前正在明天休假。一天後我會抓住這個。對不起,但..我認爲最後的選擇將解決你的問題,你的代碼是好的。 –
謝謝@Shree克里希納在最後,當我刪除if和else條件**時,所有行中的單選按鈕被選中。但現在我面臨滾動問題。我會嘗試修復滾動問題。謝謝... – Asha