2

我想使用自定义单选按钮(单选项目选项)进行自定义列表视图(每个项目一个图像和一个文本)。

我的代码:

listview.setAdapter(new myAdapter(getApplicationContext(),
                    R.layout.mycontent, text));

我的适配器类:

   public class myMessageAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;

private String[] mText = null;

long id;
private int mViewResourceId;

public myMessageAdapter(Context ctx, int viewResourceId,
        ArrayList<String> textList) {

    super(ctx, viewResourceId);

    mInflater = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    String[] message = textList.toArray(new String[textList.size()]);

    mText = message;

    mViewResourceId = viewResourceId;
}

@Override
public int getCount() {
    return mText .length;
}

@Override
public String getItem(int position) {
    return mText [position];
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = mInflater.inflate(mViewResourceId, null);

    TextView MESSAGE = (TextView) convertView.findViewById(R.id.message);

    id = getItemId(position);

    MESSAGE.setText(mText [position]);
    return convertView;
}
 }

我的输出列表项应该是:

     <radioButton> <Text> <Image>.Single choice selection.How could i do that?
4

2 回答 2

1

试试这个解决方案

package com.example.demo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

public class ListViewDemo extends Activity {
    ListView LSOne;
    int[] _intRadio = new int[20];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_radio);

        LSOne = (ListView) findViewById(R.id.LSOne);

        LSOne.setAdapter(new LsAdapter());

    }

    public class LsAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return 20;
        }

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

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public int getViewTypeCount() {
            // TODO Auto-generated method stub
            return 20;
        }

        @Override
        public int getItemViewType(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            Log.v("ConvertView", String.valueOf(position));
            int _intPosition = getItemViewType(position);
            if (convertView == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = vi.inflate(R.layout.listview_radio_raw, null);
                holder = new ViewHolder();
                holder.code = (TextView) convertView
                        .findViewById(R.id.textView1);
                holder.name = (RadioButton) convertView
                        .findViewById(R.id.radioButton1);
                convertView.setTag(holder);

                holder.name.setId(_intPosition);
                holder.name.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        for (int i = 0; i < _intRadio.length; i++) {
                            if (i == v.getId()) {
                                _intRadio[i] = 1;
                            } else {
                                _intRadio[i] = 0;
                            }
                        }
                        notifyDataSetChanged();
                    }
                });
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            if (_intRadio[_intPosition] == 1) {
                holder.name.setChecked(true);
            } else {
                holder.name.setChecked(false);
            }

            return convertView;

        }

        private class ViewHolder {
            TextView code;
            RadioButton name;
            Button btn;
        }
    }
}

listview_radio_raw.xml

<?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="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="60dip"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/button_radio"
            android:paddingLeft="60dip"
            android:text="RadioButton" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>

</LinearLayout>

button_radio.xml

//这个xml添加到你的drawable文件夹中。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/radio_on" android:state_checked="true" android:state_pressed="false"/>
    <item android:drawable="@drawable/radio_off" android:state_checked="false" android:state_pressed="false"/>
    <item android:drawable="@drawable/radio_on_pressed" android:state_checked="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/radio_off_pressed" android:state_checked="false" android:state_pressed="true"/>

</selector>

下载此链接的所有图片

于 2013-03-25T06:14:41.090 回答
0

在您的 getview 中膨胀自定义布局

 public View getView(int arg0, View arg1, ViewGroup arg2) {
    ViewHolder vh;
    vh= new ViewHolder();

    if(arg1==null )
     {
        arg1=mInflater.inflate(R.layout.customlistview, arg2,false);
        vh.iv1= (ImageView)arg1.findViewById(R.id.ivs);
        vh.rb= (RadioButton) arg1.findViewById(R.id.radioButton1);
        vh.tv= (TextView)arg1.findViewById(R.id.textView1);

     }
    else
    {
     arg1.setTag(vh);
    }
           //set data to textview, radiobutton and imageview.
    return arg1;
}

   static class ViewHolder
   {
   TextView tv;
   RadioButton rb;
   ImageView iv1;
   }

自定义列表视图.xml。每行的自定义布局。

<?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="fill_parent"
android:orientation="horizontal"
android:cacheColorHint="#000000"
>
<RadioButton
    android:id="@+id/radioButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="RadioButton" />
   <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

 <ImageView
    android:id="@+id/ivs"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     />
</LinearLayout>
于 2013-03-21T06:19:02.003 回答