具有单选按钮的自定义ListViewAdapter。

时间:2018-08-16 15:00:37

标签: android listview android-radiobutton android-radiogroup

我正在编码一个从mySQL Server获取数据的listView。 我创建了以下类。 类ListView。它有两个String的承包商。我坐在吸气器和吸气器上。 我相信适配器本身存在问题。我可以单击多个选项。当我在布局中创建自定义适配器时,如下所示:

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/RG_Adapter"
    android:layoutDirection="rtl"
    android:orientation="horizontal">
    <RadioButton
        android:id="@+id/TRIP_NAME"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:textAlignment="center"
        android:textSize="25sp"
        android:textColor="#000000"
        />

    <TextView
        android:id="@+id/SUM_TRIPS"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:textColor="#000000"
        android:textAlignment="center"
        android:textSize="25sp" />

</RadioGroup>

我相信我的问题出在Adapter类上。

我创建它的方式如下:

  public class TRIPS_LISTVIEW_ADAPTER extends ArrayAdapter<TRIPS_LISTVIEW> {
private Context mContext;
private ArrayList<TRIPS_LISTVIEW> mData;
private MyFunctionsClass myFunctionsClass = new MyFunctionsClass();

public TRIPS_LISTVIEW_ADAPTER (Context mContext, ArrayList<TRIPS_LISTVIEW> mData) {
    super(mContext, R.layout.summary_shape_layout,mData);
    this.mContext = mContext;
    this.mData = mData;
}


public int getCount() {
    return mData.size();
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    if (convertView == null)
    {
        LayoutInflater mInflater = (LayoutInflater)
                mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.summary_shape_layout, null);
    }
    RadioGroup RG = (RadioGroup) convertView.findViewById(R.id.RG_Adapter);
    TextView TRIP_NAME = (TextView) convertView.findViewById(R.id.TRIP_NAME);
    TRIP_NAME.setTypeface(myFunctionsClass.FONT( TRIP_NAME.getContext().getAssets(),1));
    TRIP_NAME.setText(myFunctionsClass.get_The_trip(mData.get(position).getTRIP_TITLE()));
    TextView SUM_TRIPS = (TextView) convertView.findViewById(R.id.SUM_TRIPS);
    SUM_TRIPS.setTypeface(myFunctionsClass.FONT( SUM_TRIPS.getContext().getAssets(),1));
    SUM_TRIPS.setText(mData.get(position).getTRIP_COUNT());
    return convertView;
}

}

我的MainActivity类中的数据已正确检索。但是正如我提到的,我有多种模式选择。 enter image description here

1 个答案:

答案 0 :(得分:0)

尝试以下操作:

1)Demo2.class:-------------

public class Demo2 extends AppCompatActivity {

private ListView lv;
private CheckBox cb;
private Adapter adapter;
private List<Boolean> checkBoxState;
private List<String> checkBoxText;

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

    checkBoxState = new ArrayList<>();
    checkBoxText = new ArrayList<>();

    for(int i = 0 ; i<10 ; i++){
        if(i == 0) {
            checkBoxState.add(i, true);
        }else{
            checkBoxState.add(i , false);
        }
        checkBoxText.add( i , "C" + (i+1));
    }

    lv = (ListView) findViewById(R.id.lv);
    adapter = new Adapter(getApplicationContext() , checkBoxState , checkBoxText);
    lv.setAdapter(adapter);


}
}

2)Adapter.class:------

public class Adapter extends BaseAdapter {

private Context context;
private LayoutInflater layoutInflater;
private List<Boolean> checkBoxState;
private List<String> checkBoxText;

public Adapter(Context context, List<Boolean> checkBoxState , List<String> checkBoxText) {

    this.context = context;

    layoutInflater = LayoutInflater.from(context);

    this.checkBoxState = checkBoxState;

    this.checkBoxText = checkBoxText;

}


public int getCount() {
    return checkBoxState.size();
}

public Object getItem(int position) {
    return checkBoxState.get(position);
}

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

public View getView(final int position, View convertView, ViewGroup parent) {

    View view = convertView;
    final CheckBox cb_list_item;

    if (convertView == null) {
        if (layoutInflater != null) {
            view = layoutInflater.inflate(R.layout.list_item, null);
        }
    }

    cb_list_item = (CheckBox) view.findViewById(R.id.cb_list_item);
    cb_list_item.setText(checkBoxText.get(position));
    cb_list_item.setOnCheckedChangeListener(null); // mask onCheckedChangeListener()
    cb_list_item.setChecked(checkBoxState.get(position));
    cb_list_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (!b) { // already selected
                compoundButton.setChecked(true);
            } else { // is selected now
                checkSelected(position);
            }
        }
    });

    return view;
}


private void checkSelected(int position){
    try {
        for (int i = 0; i < checkBoxState.size(); i++) {
            checkBoxState.set(i, false);
        }
        checkBoxState.set(position, true);
        this.notifyDataSetChanged();
    }catch (Exception e){
        e.printStackTrace();
    }
}
}

3)demo2.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:weightSum="100"
android:orientation="vertical">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/lv"
    android:layout_marginStart="10dp"
    android:layout_marginEnd="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp">
</ListView>

</LinearLayout>

4)list_item.xml:----------

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

<CheckBox
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="C"
    android:layout_marginStart="10dp"
    android:layout_marginEnd="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:id="@+id/cb_list_item"/>

</android.support.constraint.ConstraintLayout>

5)输出:--------

Output