在自定义列表视图中一次仅选择一个单选按钮,android

2024-02-22

我正在尝试制作自定义列表视图。列表视图中的每个条目都有一个图像视图、一个文本视图和一个单选按钮。我终于列出了列表,但现在我在选择单选按钮时遇到了问题。

我想要的是每当用户单击条目中的任意位置时,这应该设置相应的单选按钮并自动取消选择列表中的其他单选按钮。

这是我的代码: 总线项目.xml:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imglist_icon"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="center_vertical"
        android:layout_margin="10dp"
        android:src="@drawable/alaknanda" />

    <TextView
        android:id="@+id/txtlist_name"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:text="Alaknanda"
        android:textSize="22sp"
        android:textStyle="bold" />

    <RadioButton
        android:id="@+id/radio_list"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />


</LinearLayout>

BusListActivity.java :-

package com.hpubts50.hpubustrackerserver;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

public class BusListActivity extends ListActivity {

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

        // Setting List Adapter for custom list
        setListAdapter(new MyBusListAdapter(this, android.R.layout.simple_list_item_1, R.id.txtlist_name, getResources().getStringArray(R.array.HPU_Buses)));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        //super.onListItemClick(l, v, position, id);
        Log.e("MYTAG",""+position);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.bus_list, menu);
        return true;
    }

    // Custom Adapter inner class for custom list

    private class MyBusListAdapter extends ArrayAdapter<String> {

        public MyBusListAdapter(Context context, int resource, int textViewResourceId, String[] strings) {
            super(context, resource, textViewResourceId, strings);
            // TODO Auto-generated constructor stub
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Log.e("MYTAG","one");
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.bus_item, parent, false);
            String[] items = getResources().getStringArray(R.array.HPU_Buses);
            Log.e("MYTAG","two");

            ImageView imglist_icon = (ImageView) row.findViewById(R.id.imglist_icon);
            TextView txtlist_name = (TextView) row.findViewById(R.id.txtlist_name);
            RadioButton radio_list = (RadioButton) row.findViewById(R.id.radio_list);
            Log.e("MYTAG","three");

            txtlist_name.setText(items[position]);
            Log.e("MYTAG","four");

            if (items[position].equals("Alakananda")) {
                imglist_icon.setImageResource(R.drawable.alaknanda);
            } else if (items[position].equals("Alakananda")) {
                imglist_icon.setImageResource(R.drawable.alaknanda);
            }else if (items[position].equals("Chetanya")) {
                imglist_icon.setImageResource(R.drawable.chetanya);
            }else if (items[position].equals("Garuda")) {
                imglist_icon.setImageResource(R.drawable.garuda);
            }else if (items[position].equals("Nandi")) {
                imglist_icon.setImageResource(R.drawable.nandi);
            }else if (items[position].equals("Neela")) {
                imglist_icon.setImageResource(R.drawable.neela);
            }
            Log.e("MYTAG","five");

            return row;
        }
    }
}

活动_总线_列表.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".BusListActivity" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

你可能想看看我的作品。我使用自定义适配器作为子类,但我所做的显然可以应用于列表活动。

  1. 我采用了“选定”RadioButton 的一个实例,并最初将其设置为空。
  2. 我在 getView 函数中的每个单选按钮上实现了 onClickListener
  3. 现在我将使用“selected”来保存已选中的“last”单选按钮的引用
  4. 然后我只需取消选中上一个单选按钮并选中当前的单选按钮
  5. 另外不要忘记使用“selected”来保存刚刚选中的单选按钮的引用
class FolderAdapter extends ArrayAdapter<String>
{
    ArrayList<File> list;
    Context context;
    RadioButton selected=null;

    public FolderAdapter(Context context, int resource, ArrayList<File> list)
    {
        super(context, resource);
        this.list = list;
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.fragment_e_row, parent, false);
        final RadioButton rbFolder = (RadioButton) view.findViewById(R.id.rbFolder);

        rbFolder.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if(selected != null)
                {
                    selected.setChecked(false);
                }

                rbFolder.setChecked(true);
                selected = rbFolder;
            }
        });
        rbFolder.setText(list.get(position).getName());
        return view;
    }

    @Override
    public int getCount()
    {
        return list.size();
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在自定义列表视图中一次仅选择一个单选按钮,android 的相关文章

随机推荐