2

カスタムラジオボタン(単一選択アイテムオプション)を使用して、カスタムリストビュー(アイテムごとに1つの画像と1つのテキスト)を実行したい。

私のコード:

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

私のAdapterクラス:

   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 をドローアブル フォルダーに追加します。

<?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;
   }

customlistview.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 に答える