簡體   English   中英

在自定義列表視圖android中一次只能選擇一個單選按鈕

[英]Select only one radio button at a time in custom list view, android

我正在嘗試制作自定義列表視圖。 列表視圖中的每個條目都將具有imageview,textview和radiobutton。 我終於做了清單,但現在我在選擇單選按鈕時遇到了問題。

我想要的是每當用戶點擊條目中的任何地方時,這應該設置相應的單選按鈕並自動取消選擇列表中的其他單選按鈕。

這是我的代碼:bus_item.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;
        }
    }
}

activity_bus_list.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的一個實例“選中”並最初將其設為null。
  2. 我在getView函數的每個單選按鈕上實現了onClickListener
  3. 現在我將使用“selected”來保存已檢查的“last”單選按鈕的引用
  4. 然后我只需取消選中上一個單選按鈕並檢查當前單選按鈕
  5. 另外,不要忘記使用“選中”來保存剛檢查過的這個單選按鈕的引用
 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(); } } 
@Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
    View vi = convertView;

    if (vi == null) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        vi = inflater.inflate(R.layout.row_search_engine, null);

    }
    browserImage = (ImageView) vi.findViewById(R.id.list_image);
    browserType = (TextView) vi.findViewById(R.id.browswer_name);
    browserType.setText(browser_names[position]);
    browserImage.setImageResource(image_id[position]);
    check = (RadioButton) vi.findViewById(R.id.check);
    check.setChecked(position == selectedPosition);
    check.setTag(position);
    check.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectedPosition = (Integer)view.getTag();
            notifyDataSetChanged();
        }
    });
    return vi;
}

嘗試這可能有幫助...對於面臨同樣問題的其他人.. selectedPosition = 0在任何地方初始化它。

您的問題的解決方案是這樣做:

ListView l = getListView();
// because you just want a single selection
l.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

然后你的listview行必須實現Checkable,這里是如何:在這里看到解釋 - > ListView與CHOICE_MODE_MULTIPLE在自定義視圖中使用CheckedText

此外,您的問題與此處相同: 如何在ListView自定義適配器中使用RadioGroup?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM