簡體   English   中英

具有單選按鈕的自定義ListViewAdapter。

[英]Custom ListViewAdapter with Radio Buttons.

我正在編碼一個從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類中的數據已正確檢索。 但是正如我提到的,我有多種模式選擇。 在此處輸入圖片說明

請嘗試以下操作:

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)輸出:--------

產量

暫無
暫無

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

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