2

I want to do custom listview(one image & one text per item) with custom radio buttons(Single selection item option).

My Code:

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

my Adapter class:

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

My output list item should be:

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

2 Answers 2

1

Try this Solution

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

//this xml add in your drawable folder.

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

Download all image for thislink

Sign up to request clarification or add additional context in comments.

Comments

0

In your getview inflate custom layout

 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. Custom layout for each row.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.