如何在自定义列表视图中对单选按钮进行分组

3

我正在使用自定义带有图像和单选按钮的列表视图。现在我想将这些单选按钮分组。下面是我的xml和java类程序。

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<ImageView
    android:id="@+id/image"
    android:layout_width="55dip"
    android:layout_height="55dip"
    android:scaleType="centerCrop"
    android:src="@drawable/stub" />

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="208dp"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:layout_weight="0.56"
    android:checkMark="?android:attr/listChoiceIndicatorSingle"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#000000" />

<RadioButton
    android:id="@+id/radioButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

     public class LazyAdapter extends BaseAdapter {

private Activity activity;
private String[] data;
private String[] conditions;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, String[] d, String[] c) {
    activity = a;
    data=d;
    conditions=c;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.length;
}



public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.item, null);

    TextView text=(TextView)vi.findViewById(R.id.text1);      
    ImageView image=(ImageView)vi.findViewById(R.id.image);
    text.setText(conditions[position]);
    imageLoader.DisplayImage(data[position], image);


    return vi;
}

Java的第二类...

     public class WeatherCondition extends Activity {

ListView list1;
LazyAdapter adapter;
ImageButton done,cancel;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.weather_conditions);
    done=(ImageButton)findViewById(R.id.imageButton1);
    done.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(WeatherCondition.this, AddWeatherAlarm.class);
                startActivity(intent);

        }

    });

    cancel=(ImageButton)findViewById(R.id.imageButton2);
    cancel.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(WeatherCondition.this, AddWeatherAlarm.class);
            startActivity(intent);
        }

    });       



    list1=(ListView)findViewById(R.id.list);        

    adapter=new LazyAdapter(this, mStrings,conditions);


    list1.setAdapter(adapter);
    list1.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    list1.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
        Toast.makeText(WeatherCondition.this, ""+conditions[arg2],Toast.LENGTH_SHORT).show();

        }

    });


}

String conditions[]={ "Clear/Sunny","Partly Cloudy","Cloudy","Overcast","Mist","Patchy rain nearby","Patchy snow nearby","Patchy sleet nearby","Patchy freezing drizzle nearby","Thundery outbreaks in nearby","Blowing snow","Blizzard","Fog","Freezing fog","Patchy light drizzle","Light drizzle","Freezing drizzle","Heavy freezing drizzle","Patchy light rain","Light rain","Moderate rain at times","Moderate rain","Heavy rain at times","Heavy rain","Light freezing rain","Moderate or Heavy freezing rain","Light sleet","Moderate or heavy sleet","Patchy light snow","Light snow","Patchy moderate snow","Moderate snow","Patchy heavy snow","Heavy snow","Ice pellets","Light rain shower","Moderate or heavy rain shower","Torrential rain shower","Light sleet showers","Moderate or heavy sleet showers","Light snow showers","Moderate or heavy snow showers","Light showers of ice pellets","Moderate or heavy showers of ice pellets","Patchy light rain in area with thunder","Moderate or heavy rain in area with thunder","Patchy light snow in area with thunder","Moderate or heavy snow in area with thunder"};

   }

}


你有什么问题...? - Pragnani
1个回答

0
Add the radiogroup to your row layout like this I have added 

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">


  <RadioGroup
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <RadioButton
  android:layout_width="wrap_content"
  android:id="@+id/radio1"
  android:text="alpha"
  android:layout_height="wrap_content"/>

    <RadioButton
  android:layout_width="wrap_content"
  android:id="@+id/radio2"
  android:text="beta"
  android:layout_height="wrap_content"/>



  </RadioGroup>
</LinearLayout>

而你的适配器应该有类似这样的内容

public class adapter extends ArrayAdapter<String>
    {

        public adapter() {
            super(rtt.this,R.layout.test,arr);
            // TODO Auto-generated constructor stub
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return arr.length;
        }

        @Override
        public int getPosition(String item) {
            // TODO Auto-generated method stub
            return super.getPosition(item);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            LayoutInflater li=getLayoutInflater();
            View row=li.inflate(R.layout.test,parent, false);

            RadioButton a=(RadioButton)row.findViewById(R.id.radio1);
            RadioButton b=(RadioButton)row.findViewById(R.id.radio2);

            return row;
        }



    }

这只是一个例子,请根据您的需要进行修改 CheckedTextView 您可以继续删除它,它不是必需的。


所以您想说如果列表中有48个按钮,那么我就必须设置48个单选按钮吗? - saman
1
48个按钮是什么东西?这就是我们使用布局填充器的原因。这段代码很好用,只需将其添加到您正在创建的单行布局中即可。 - Aashish Bhatnagar
@Pragnani 我的代码工作正常,但是它给我多个选择而不是单个选择。 - saman
让我们在聊天室里讨论这个问题,我正在给这个问题点赞,这样你就可以获得聊天特权了。 - Aashish Bhatnagar
这是我想要的。它是一个包含图像和单选按钮的ListView。现在在XML文件中,当我尝试将CheckedTextView添加为CustomListView时,它不会检查任何一个,但如果将单独的RadioButton与TextView放在自定义列表中,则可以同时检查多个项目... - saman
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接