【问题标题】:Android ListFragment multiple checkbox in a single rowAndroid ListFragment 单行中的多个复选框
【发布时间】:2023-04-04 02:35:02
【问题描述】:

我面临一些问题,所以我想也许有人可以帮助我,因为我处于死胡同并且不知道如何解决这个问题。所以我正在创建一个应用程序,它使用 List Fragment 在一行中具有多个(正好 5 个)复选框和 1 个文本视图。所以第一个问题是:我只想检查 5 个复选框中的一个(这意味着如果我检查第一个,那么其他(第二个、第三个、第四个、第五个框)不应该被选中,依此类推......) .第二个也是最重要的问题是会有 10-20 行,所以当我向下/向上滚动时,一些行被回收并且选中的框变得未选中或随机选中,所以我应该如何存储数据,这样被选中的复选框在向上或向下滚动后不会被取消选中或随机选中。 onListItemClick 也不起作用:(

列表片段:

package com.android4dev.navigationview;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
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.CheckBox;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import static android.support.design.widget.Snackbar.*;


public class SymptomsSelectionFragment extends ListFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle  savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_symptoms_selection, container, false);
        return root;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                "Linux", "OS/2","shitPhone","testPhone", "boomPhone", "meskaPhone" };
        ArrayAdapter<Question> adapter = new InteractiveArrayAdapter(getActivity(),
                getQuestion());
        FloatingActionButton fab = (FloatingActionButton) getActivity().findViewById(R.id.fab);
        fab.hide();
        setListAdapter(adapter);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO implement some logic
        super.onListItemClick(l, v, position, id);
        String item = (String) getListAdapter().getItem(position);
        Snackbar.make(getListView(), item+" selected", Snackbar.LENGTH_LONG).show();
        Toast.makeText(getActivity(),position+" <--- Nr.",Toast.LENGTH_SHORT).show();
    }
    private List<Question> getQuestion() {
        List<Question> list = new ArrayList<Question>();
        list.add(get("Linux"));
        list.add(get("Windows7"));
        list.add(get("Suse"));
        list.add(get("Eclipse"));
        list.add(get("Ubuntu"));
        list.add(get("Solaris"));
        list.add(get("Android"));
        list.add(get("iPhone"));
        // Initially select one of the items
        //list.get(1).setSelected(true);
        return list;
    }

    private Question get(String s) {
        return new Question(s);
    }







}

ArrayAdapter:

import java.util.List;
package com.android4dev.navigationview;
import android.app.Activity;
import android.media.Image;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.TextView;

public class InteractiveArrayAdapter extends ArrayAdapter<Question> {

    private final List<Question> list;
    private final Activity context;

    public InteractiveArrayAdapter(Activity context, List<Question> list) {
        super(context, R.layout.row_layout_symptoms_selection, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox1;
        protected CheckBox checkbox2;
        protected CheckBox checkbox3;
        protected CheckBox checkbox4;
        protected CheckBox checkbox5;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;

        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.row_layout_symptoms_selection, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.label_symptom);
            viewHolder.checkbox1 = (CheckBox) view.findViewById(R.id.button1);
            viewHolder.checkbox2 = (CheckBox) view.findViewById(R.id.button2);
            viewHolder.checkbox3 = (CheckBox) view.findViewById(R.id.button3);
            viewHolder.checkbox4 = (CheckBox) view.findViewById(R.id.button4);
            viewHolder.checkbox5 = (CheckBox) view.findViewById(R.id.button5);
            viewHolder.checkbox1
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                                     boolean isChecked) {
                            Question element = (Question) viewHolder.checkbox1
                                    .getTag();
                            element.setSelected(buttonView.isChecked());

                        }
                    });
            viewHolder.checkbox2
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                                     boolean isChecked) {
                            Question element2 = (Question) viewHolder.checkbox2
                                    .getTag();
                            element2.setSelected(buttonView.isChecked());

                        }
                    });
            viewHolder.checkbox3
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                                     boolean isChecked) {
                            Question element3 = (Question) viewHolder.checkbox3
                                    .getTag();
                            element3.setSelected(buttonView.isChecked());

                        }
                    });
            viewHolder.checkbox4
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                                     boolean isChecked) {
                            Question element4 = (Question) viewHolder.checkbox4
                                    .getTag();
                            element4.setSelected(buttonView.isChecked());

                        }
                    });
            viewHolder.checkbox5
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                                     boolean isChecked) {
                            Question element5 = (Question) viewHolder.checkbox5
                                    .getTag();
                            element5.setSelected(buttonView.isChecked());

                        }
                    });
            view.setTag(viewHolder);
            viewHolder.checkbox1.setTag(list.get(position));
            viewHolder.checkbox2.setTag(list.get(position));
            viewHolder.checkbox3.setTag(list.get(position));
            viewHolder.checkbox4.setTag(list.get(position));
            viewHolder.checkbox5.setTag(list.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox1.setTag(list.get(position));
            ((ViewHolder) view.getTag()).checkbox2.setTag(list.get(position));
            ((ViewHolder) view.getTag()).checkbox3.setTag(list.get(position));
            ((ViewHolder) view.getTag()).checkbox4.setTag(list.get(position));
            ((ViewHolder) view.getTag()).checkbox5.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        holder.checkbox1.setChecked(list.get(position).isSelected());
        holder.checkbox2.setChecked(list.get(position).isSelected());
        holder.checkbox3.setChecked(list.get(position).isSelected());
        holder.checkbox4.setChecked(list.get(position).isSelected());
        holder.checkbox5.setChecked(list.get(position).isSelected());
        return view;
    }
}

问题类:

 package com.android4dev.navigationview;

/**
 * Created by Home-PC on 10/19/2016.
 */

public class Question {
    private String name;
    private boolean selected;

    public Question(String name) {
        this.name = name;
        selected = false;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }
}

fragment_symptoms_selection.xml:

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


    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:layout_weight="1"
        android:drawSelectorOnTop="false" />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:text="No data" />
</LinearLayout>

row_layout_symptoms_selection.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:showDividers="middle"
    android:divider="?android:attr/listDivider"
    tools:context=".SymptomsActivity"
    >

    <TextView
        android:id="@+id/label_symptom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/text"
        android:textSize="19dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="4dp"
        android:layout_marginTop="4dp" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        >

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:button="@drawable/checkbox_drawable"
            android:id="@+id/button1"
            android:layout_weight="1"
            android:background="@color/CheckBox1"
            />
        <CheckBox
            android:button="@drawable/checkbox_drawable"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:id="@+id/button2"
            android:layout_weight="1"
            android:background="@color/CheckBox2"/>
        <CheckBox
            android:button="@drawable/checkbox_drawable"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:id="@+id/button3"
            android:layout_weight="1"
            android:background="@color/CheckBox3"/>
        <CheckBox
            android:button="@drawable/checkbox_drawable"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:id="@+id/button4"
            android:layout_weight="1"
            android:background="@color/CheckBox4"/>
        <CheckBox
            android:button="@drawable/checkbox_drawable"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:id="@+id/button5"
            android:layout_weight="1"
            android:background="@color/CheckBox5"/>
    </LinearLayout>
</LinearLayout>

【问题讨论】:

  • 你能举例说明我应该怎么做吗?因为我是新手,刚刚学习安卓。我将不胜感激:)
  • 您还可以将您的状态保存在共享首选项中。如果你想处理以便用户只能选择一个选项,你可以使用 if 语句来做到这一点。
  • 首先,onCheckChangeListener 有“isChecked”变量。你怎么在视图上调用 isChecked() ? IsChecked 在许多情况下是出了名的不正确。我认为您不会从按钮视图中获得正确的信息。请改用 isChecked 布尔值。
  • 下面有一些例子。

标签: android checkbox android-recyclerview android-arrayadapter android-listfragment


【解决方案1】:

首先,如果您只需要一次选择一个选项,请考虑使用RadioButton 实现。 其次,您是否将数据存储在 sql 数据库中?如果您已经在使用 sql 数据库,这是存储复选框状态的方式:

a- 在Question Class 中添加:

int status   
public int getStatus() {
        return status;
   }
public void setStatus(int status) {
        this.status = status;
    }

b- 在您的数据库适配器中,将 STATUS 添加到您的表项。

c- 在您的 getView 方法中处理检查的项目,如下所示:

holder.check.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CheckBox cb = (CheckBox) v;
                        Item changeItem = (Item) cb.getTag();
                        changeItem.setStatus(cb.isChecked() ? 1 : 0);
                        mDbHelper.updateItem(changeItem);
                             }
                     });

请参阅this 了解如何在 sql 数据库中存储数据。 如果不使用数据库,可能需要使用SharedPreferences

希望对你有帮助!

【讨论】:

    【解决方案2】:

    这是一种让用户只选择一个选项的方法。更好的方法是实现 onCheckChangeListener 并做同样的事情,这样您就不必重新使用相同的方法:) 来存储用户选择的值,您应该使用“扩展应用程序”方法。

        final CheckBox cbOne = (CheckBox) findViewById(R.id.cbOne);
        final CheckBox cbTwo = (CheckBox) findViewById(R.id.cbTwo);
        final CheckBox cbThree = (CheckBox) findViewById(R.id.cbThree);
    
        cbOne.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    
                if (isChecked) {
    
                    cbTwo.setClickable(false);
                    cbThree.setClickable(false);
    
                    cbTwo.setChecked(false);
                    cbThree.setChecked(false);
    
    
                } else {
    
                    cbTwo.setClickable(true);
                    cbThree.setClickable(true);
    
                }
    
            }
        });
    
        cbTwo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    
    
                if (isChecked) {
    
                    cbOne.setClickable(false);
                    cbThree.setClickable(false);
    
                    cbOne.setChecked(false);
                    cbThree.setChecked(false);
    
    
                } else {
    
                    cbOne.setClickable(true);
                    cbThree.setClickable(true);
    
    
                }
    
    
            }
        });
    
    
        cbThree.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    
    
                if (isChecked) {
    
                    cbTwo.setClickable(false);
                    cbOne.setClickable(false);
    
                    cbTwo.setChecked(false);
                    cbOne.setChecked(false);
    
    
                } else {
    
                    cbTwo.setClickable(true);
                    cbOne.setClickable(true);
    
    
                }
    
    
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多