多个复选框不能检查

问题描述 投票:0回答:3

我想,以检查每个项目的复选框,但应用程序不断崩溃。

这是我的NewIngredients:

public class NewIngredients extends AppCompatActivity {

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

       readCSV ();
   } 

   public void readCSV(){
       List<IngredientsHolder> data = new ArrayList<> ();
       try {
           String sCurrentline = null;
           BufferedReader br = new BufferedReader(new FileReader ("/sdcard/TABLE_BF.csv"));
           sCurrentline = br.readLine ();
           while ((sCurrentline = br.readLine()) != null) {
               String[] arr = sCurrentline.split(",");
               IngredientsHolder ingredient = new IngredientsHolder(arr[0], arr[1], arr[2]);
               data.add(ingredient);
           }
           br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Map<String, List<IngredientsHolder>> ingredientsByName = data.stream().collect(Collectors.groupingBy(IngredientsHolder::getName));

        List<IngredientsHolder> main = new ArrayList<>();
        List<IngredientsHolder> other = new ArrayList<>();

        //Sort on `admin` in descending order
        Comparator<IngredientsHolder> comparator = Comparator.comparing(IngredientsHolder:: getAdmin, (i1, i2) -> {
            if (i2 > i1) {
                return -1;
            } else if (i2 < i1) {
                return 1;
            }
            return 0;
        });

        //Go through each list (ingredient) and find the one with max `admin` value
        //and add it to the `main` list then add the rest to `other`
        ingredientsByName.forEach( (k, group) -> {
            IngredientsHolder max = group.stream().max(comparator).get();
            if (max.getAdmin() == 0) {
                max = group.get(0);
            }if(max.getAdmin () > 0){
                //group.forEach(System.out::println);
                int value = max.getAdmin ();

                List<IngredientsHolder> filtered = (group.stream()
                             .filter(mc -> mc.getAdmin() == value)
                             .collect(Collectors.toList()));
                //filtered.forEach(mc -> System.out.println (mc.toString ()));
            }

            main.add(max);
            group.remove(max);
            other.addAll(group);
            System.out.println (other.size ());
        });

        List<IngredientsHolder> newMain = main.stream().distinct().collect(Collectors.toList());
        ListView lv = (ListView) findViewById(R.id.list);
        lv.setAdapter(new CustomAdapter(NewIngredients.this,newMain));
    }
}

这是我的CustomAdapter:

public class CustomAdapter extends ArrayAdapter {
    private LayoutInflater mInflater;
    List<IngredientsHolder> ingredientsList;

    public CustomAdapter(Context context, List<IngredientsHolder> list){
        super(context,0,list);
        ingredientsList = list;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.custom_ni,parent,false);
            // inflate custom layout called row
            holder = new ViewHolder();
            holder.tv =(TextView ) convertView.findViewById(R.id.textview);

            // initialize textview
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder)convertView.getTag();
        }
        IngredientsHolder in = (IngredientsHolder)ingredientsList.get(position);
        holder.tv.setText(in.subName);
        // set the name to the text;

        return convertView;

    }

    static class ViewHolder {
        TextView tv;
    }
}

这是我的IngredientsHolder:

public class IngredientsHolder {
    String name;
    String subName;
    int admin;

    public IngredientsHolder(String name, String subName, String admin) {
        this.name = name;
        this.subName = subName;
        this.admin = Integer.valueOf(admin);
    }

    public String getName() {
        return name;
    }

    public String toString() {
        return subName;
    }

    public int getAdmin() {
        return admin;
    }
}

这是我的custom_ni.xml文件:

<CheckBox
    android:id="@+id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_centerVertical="true"
    android:layout_marginEnd="7dp"
    android:onClick="clickHandler"></CheckBox>

<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="13dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="9dp"
    android:layout_toLeftOf="@id/checkbox"
    android:text="TEST"
    android:textColor="#000"
    android:textSize="18sp" />

</RelativeLayout>

这是堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.kenneth.thisforthat, PID: 27999
              java.lang.IllegalStateException: Could not find method clickHandler(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatCheckBox with id 'checkbox'
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:423)
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:380)
                  at android.view.View.performClick(View.java:6312)
                  at android.widget.CompoundButton.performClick(CompoundButton.java:134)
                  at android.view.View$PerformClick.run(View.java:24802)
                  at android.os.Handler.handleCallback(Handler.java:790)
                  at android.os.Handler.dispatchMessage(Handler.java:99)
                  at android.os.Looper.loop(Looper.java:169)
                  at android.app.ActivityThread.main(ActivityThread.java:6521)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

我尝试了一些教程和一些建议,但我似乎无法实现任何事情的权利。

我真正想要做的是检查多个项目,这些检查的项目存入ArrayList和那些还存储在另一个ArrayList中那些未被检查的项目。

java android arraylist
3个回答
0
投票

clickHandler事件,法中缺少的源代码。


0
投票

复选框id为checkbox有名字clickHandler一个单击处理程序,但它是Java类中没有实现。

要么删除

android:onClick="clickHandler"

clickhandler方法添加到Java类。

public void clickHandler(View view) {

}

0
投票

你应该保持一个标志你的男人的ArrayList设置选中和未选中复选框标志值的基础上。

所以,你没有必要让两个单独的列表。

© www.soinside.com 2019 - 2024. All rights reserved.