单选按钮上的Android onClick事件无法正常工作

时间:2014-09-05 08:12:22

标签: android radio-button onclicklistener

我想列出餐馆名单,并且除了每个名字外都有radiobutton。所以我制作了两个xml文件。在第一个中有一个listview,另一个有一个radioButton。 现在我想在每个radioButton上设置onClick监听器。请告诉我如何做到这一点。我在下面发布我的代码。

MainActivity.java

package com.example.radiogroup;


import java.util.ArrayList;
import java.util.HashMap;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends Activity {

private ArrayList<HashMap<String,Object>> data;
private ListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView lv=(ListView)findViewById(R.id.listView1);
    data=new ArrayList<HashMap<String, Object>>();
    prepareListData();
    adapter = new ListAdapter(this,data);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id)
        {
        RadioButton rb=(RadioButton)findViewById(R.id.radioButton1);
        {
            if(position==0)
            {
                Toast.makeText(getApplicationContext(), "Move forward",   
     Toast.LENGTH_SHORT).show();
            }


        }
        }

    });





}
private void prepareListData()
{
    HashMap<String,Object> row1=new HashMap<String,Object>();
    row1.put("ID",1 );
    row1.put("NAME","Taj Mahal");
    data.add(row1);

    HashMap<String,Object> row2=new HashMap<String,Object>();
    row2.put("ID",2 );
    row2.put("NAME","Agra Fort");
    data.add(row2);

    HashMap<String,Object> row3=new HashMap<String,Object>();
    row3.put("ID",3 );
    row3.put("NAME","Ram Baugh");
    data.add(row3);

    HashMap<String,Object> row4=new HashMap<String,Object>();
    row4.put("ID",4 );
    row4.put("NAME","Panch Mahal");
    data.add(row4);

    HashMap<String,Object> row5=new HashMap<String,Object>();
    row5.put("ID",5 );
    row5.put("NAME","Musamman Burj");
    data.add(row5);

}
class ListAdapter extends BaseAdapter
{
    private Context context;
    private ArrayList<HashMap<String,Object>> list;
     public ListAdapter(Context context,ArrayList<HashMap<String,Object>> list){
            this.context = context;
            this.list = list;
     }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
         ViewHolder holder;
            if(convertView == null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.row,null,false);
                holder.rb = (RadioButton) convertView.findViewById(R.id.radioButton1);

                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

            holder.rb.setText(list.get(position).get("NAME").toString());

        return convertView;
    }
     class ViewHolder
        {
            RadioButton rb;
        }

}

}

1 个答案:

答案 0 :(得分:0)

ListAdapter -

中使用它
 holder.rb.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "" + position,
                    Toast.LENGTH_LONG).show();
        }
    });