2013年8月18日星期日

android listview each item which has a radiobutton, how to getradibutton radio event

If that android listview each item which has a radiobutton, how to get radibutton radio events, click on a radio when it is selected, and the other to cancel, cattle people, please give the specific code

find online have no effect, or that can not be replaced by clicking one of the other, or that are chosen. . .

final RadioButton mRadioButton = (RadioButton) view
. findViewById (R.id.report_item_check);
mRadioButton.setChecked (false) ;/ / first of all set to false

if (mRadioButton.getId () <= position) {
mRadioButton.setId (position);
}


mRadioButton.setOnCheckedChangeListener (new OnCheckedChangeListener () {

@ Override
public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {
/ / Here achieve radioButton entire listView item in the radio
if (isChecked)
{
if (temp! = -1)
{
RadioButton tempRadio = (RadioButton) view.findViewById (temp);
if (tempRadio! = null)
{
tempRadio.setChecked (false);
}
}
temp = buttonView.getId ();
}

}
});
if (position == mRadioButton.getId ()) {
mRadioButton.setChecked (true);
} else {
mRadioButton.setChecked (false);
}

/ / radioButton.setId (position);
/ / radioButton.setChecked (false);
/ / radioButton
/ /. setOnCheckedChangeListener (new OnCheckedChangeListener () {
/ /
/ / public void onCheckedChanged (
/ / CompoundButton buttonView, boolean isChecked) {
/ / if (radioButton.isChecked () == true) {
/ / temp = btn_id;
/ / btn_id = radioButton.getId ();
/ / if (temp! = -1 && temp! = btn_id) {
/ / RadioButton tempButton = (RadioButton) view
/ /. findViewById (temp);
/ / if (tempButton! = null) {
/ / tempButton.setChecked (false);
/ /}
/ /}
/ /}
/ /
/ /}
/ /});
/ / if (btn_id == position) {
/ / radioButton.setChecked (true);
/ / checked_map = map;
/ /} else {
/ / radioButton.setChecked (false);
/ /}
/ /

------ Solution ------------------------------------ --------
made about your adapter code.
http://bbs.csdn.net/topics/390422655
I answered a post, similar to your needs.
------ Solution ---------------------------------------- ----
is to set up a isClickedPosition adapter
Then when getview
only isClickedPosition of view of the radio set isChecked (true)

In oncheckedchange the right adapter to refresh
------ Solution ----------------------------- ---------------
ListView itself comes with single and multi selection mode via

listview.setChoiceMode ()

can set
------ Solution ---------------------------------- ----------

package com.example.zdemo1;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

public class ListBackground extends Activity {

    private List<String> dataList = new ArrayList<String>();
   
    private ListView lv;
   
    private int clickPosition = -1;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_background);
        for (char tempChar = 'a' ; tempChar <= 'z'; tempChar++){
            dataList.add(String.valueOf(tempChar));
        }
        lv = (ListView)findViewById(R.id.listView1);
        final MyAdapter ma = new MyAdapter();
        lv.setAdapter(ma);
        /*lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                if(position != clickPosition){
                    clickPosition = position;
                }
                else{
                    clickPosition = -1;
                }
                ma.notifyDataSetChanged();
            }
        });*/
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_list_background, menu);
        return true;
    }
   
    private class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return dataList.size();
        }

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            /*TextView tv = new TextView(ListBackground.this);
            tv.setText(dataList.get(position));
            tv.setBackgroundColor(Color.WHITE);
            if(position == clickPosition){
                tv.setBackgroundColor(Color.BLACK);
            }
            return tv;
*/
            final int p = position;
            RadioButton rb = new RadioButton(ListBackground.this);
            rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
               
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        clickPosition=p;
                    }
                    else{
                        clickPosition = -1;
                    }
                    notifyDataSetChanged();
                }
            });
            if(clickPosition == position){
                rb.setChecked(true);
            }
            else{
                rb.setChecked(false);
            }
            return rb;
        }
       
    }
}



my original code changed a bit. Changed all radio
you look at the

do not find the radio before
just need an int to save the currently selected positon, then refresh the entire list just fine
only the current radio is set to true, the others are set to false

mainly look at the adapter's getview method.
------ For reference only -------------------------------------- -


set is useless, the problem is that this line of code, RadioButton tempButton = (RadioButton) view. findViewById (temp); through temp before found Set off the radio. . . Each of which can lead to but can not cancel selected
------ For reference only ----------------------------- ----------




public void setItemView (final View view, int position) {
final HashMap map = (HashMap ) helper
. getPageData (). getList (). get (position);
ImageView img = (ImageView) view
. findViewById (R.id.report_item_img);
TextView name = (TextView) view.findViewById (R.id.report_item_name);
TextView money = (TextView) view.findViewById (R.id.report_item_money);
TextView type = (TextView) view.findViewById (R.id.report_item_type);
TextView content = (TextView) view
. findViewById (R.id.report_item_content);

if (map! = null) {
imageloader.DisplayImage (map.get ("img"). toString (), img);
name.setText (map.get ("name"). toString ());
money.setText (map.get ("money"). toString ());
type.setText (map.get ("mailway"). toString ());
content.setText (! "". equals (map.get ("repay_content"). toString ()) && map.get ("repay_content"). toString (). length ( )> 140? map.get ("repay_content"). toString (). substring (0, 140). concat ("..."): map.get ("repay_content"). toString ());

final RadioButton radioButton = (RadioButton) view
. findViewById (R.id.report_item_check);
radioButton.setChecked (false) ;/ / first of all set to false

if (radioButton.getId () <= position) {
radioButton.setId (position);
}


/ / radioButton
/ /. setOnCheckedChangeListener (new OnCheckedChangeListener () {
/ /
/ / public void onCheckedChanged (
/ / CompoundButton buttonView, boolean isChecked) {
/ / if (radioButton.isChecked () == true) {
/ / temp = btn_id;
/ / btn_id = radioButton.getId ();
/ / if (temp! = -1 && temp! = btn_id) {
/ / RadioButton tempButton = (RadioButton) view
/ /. findViewById (temp);
/ / if (tempButton! = null) {
/ / tempButton.setChecked (false);
/ /}
/ /}
/ /}
/ /
/ /}
/ /});
/ / if (btn_id == position) {
/ / radioButton.setChecked (true);
/ / checked_map = map;
/ /} else {
/ / radioButton.setChecked (false);
/ /}

} else {
Utils.showToast (ReportListActivity.this, "no data");
}

}


The problem is not found through temp had previously selected radio. . .
------ For reference only -------------------------------------- -


setChoiceMode (), do not need to be placed radioButton, listView behind every line there will be a small √, for selection.
------ For reference only -------------------------------------- -
inside the adapter to add click event
------ For reference only ---------------------------- -----------


solve as you say, but under the code to be changed, if (clickPosition == position) {
rb.setChecked (true);
}
else {
rb.setChecked (false);
}
else should be removed, or else will not be selected.
thanks ~
------ For reference only -------------------------------- -------

  
solve as you say, but under the code to be changed, if (clickPosition == position) {   
rb.setChecked (true);   
}   
else {   
rb.setChecked (false);   
}   
else should be removed, or else will not be selected.   
thanks ~  


wrong, is not it if (isChecked) {
clickPosition = p;
}
else {
clickPosition = -1;
} where else to remove ~ ·

没有评论:

发表评论