我正在尝试检查每个项目的复选框,但应用程序继续崩溃。
这是我的新成分:
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中。
答案 0 :(得分:0)
clickHandler,源代码中缺少方法。
答案 1 :(得分:0)
ID为checkbox
的复选框具有名称为clickHandler
的点击处理程序,但未在Java类中实现。
要么删除
android:onClick="clickHandler"
或将clickhandler
方法添加到java类。
public void clickHandler(View view) {
}
答案 2 :(得分:0)
您应该在man ArrayList中保留一个标志,以根据标志值设置选中和未选中的复选框。
因此您无需制作两个单独的列表。