【问题标题】:how to group radio buttons in custom listview如何在自定义列表视图中对单选按钮进行分组
【发布时间】:2013-02-06 13:45:49
【问题描述】:

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

   }

}

【问题讨论】:

  • 你有什么问题...?

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


【解决方案1】:
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 个单选按钮???
  • 什么是48个按钮????这就是我们为此代码使用的布局充气器工作正常只需将其添加到您必须在那里为单行创建布局的行布局中。
  • @Pragnani 我的代码工作正常,但不是单选,而是它给了我多项选择。
  • 48 表示 48 个单选按钮。我不知道该怎么做那是我问的。我正在使用带有列表视图的按钮
  • 抱歉,如果我之前的评论冒犯了您,不,您不需要 48 个单选按钮,只需添加所需的数量,例如 4 个单选按钮,即可从四个答案中进行选择。并且您只需要在适配器中检查它们并仅在适配器内处理 oncheckedchangelistener。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-16
  • 2013-06-14
  • 2019-02-16
  • 1970-01-01
  • 1970-01-01
  • 2013-03-10
  • 2013-01-14
相关资源
最近更新 更多