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

时间:2013-02-21 09:36:09

标签: android image listview radio-button custom-lists

我正在使用带图片和单选按钮的自定义列表视图。现在我想分组这些单选按钮。这是我的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"};

   }

}

1 个答案:

答案 0 :(得分: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你可以继续并删除它不会是必要的。