java - ArrayAdapter ,检查复选框是否被选中并在动画后删除该行

标签 java android android-arrayadapter android-checkbox

您好,我如何检查复选框是否已选中,以及是否已选中 我怎样才能删除有问题的行? 谢谢你

数组适配器:

public class Todoadapter extends ArrayAdapter<Todo> {


    private Context mcontext;
    int mresource;
    public Todoadapter(@NonNull Context context, int resource, @NonNull List<Todo> objects) {
        super(context, resource, objects);
        this.mresource=resource;
        this.mcontext= context;


    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        String todo = getItem(position).getTodo();

        LayoutInflater inflater = LayoutInflater.from(mcontext);
        convertView = inflater.inflate(mresource,parent,false);
        CheckBox box = (CheckBox)convertView.findViewById(R.id.checkBox2);
        box.setText(todo);

    return convertView;
    }

}

我的主要 Activity :

公共(public)类 MainActivity 扩展 AppCompatActivity {

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

    ListView view = (ListView)findViewById(R.id.listview);

    Todo eat = new Todo("Eat ");
    Todo sleep = new Todo("Sleep");

    ArrayList<Todo> todolist = new ArrayList<>();
    todolist.add(eat);
    todolist.add(sleep);

    Todoadapter adapter = new Todoadapter(this,R.layout.custom_adapter_layout,todolist);
    view.setAdapter(adapter);


}

}

我的 XML

enter image description here

Todo enter image description here 问题是该行仍然可见

最佳答案

这就是方法,我不知道您如何填充列表以及是否使用回收器 View ,但这应该是您想要的正文。

box.setText(todo); 
box.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(box.isChecked()){
                    //Delay to see animation
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            objects.remove(getItem(position)); //Your object list where you have the itens
                            notifyDataSetChanged(); //If you are using a recycler view.
                        }
                    },300); //adding 0.3 sec delay
                }
            }
        });
return convertView;

编辑: 为了获得你的列表,你可能应该这样做:

private List<Todo> objects = new List<>(); //NEW
private Context mcontext;
int mresource;

public Todoadapter(@NonNull Context context, int resource, @NonNull List<Todo> objects) {
    super(context, resource, objects);
    this.mresource=resource;
    this.mcontext= context;
    this.objects = objects //NEW

}

关于java - ArrayAdapter ,检查复选框是否被选中并在动画后删除该行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48131901/

相关文章:

java - 将 DateFormatter 与 JFormattedTextField 结合使用

java - Android MediaPlayer 声音延迟

android - 设备可以在前台服务之前休眠吗?

android - 如何从具有两个项目的微调器中获取选定值

java - gradle 中的本地 Jar 依赖项 : how to import into code

java - 如何以百分比设置元素的宽度?

java - 关于使用 Hibernate 将对象映射到数据库

android - 在没有 photoshop 知识的情况下创建简单的启动画面(Ionic/Cordova)

android - 阵列适配器不更新

java - 从 ListView 适配器中删除重复项