使用自定义的BaseAdapter,在GridView中检查复选框。

问题描述 投票:1回答:1

早晨好,下午好,晚上好。

在我的项目中,我遇到了一个问题,我似乎无法解决它。我想创建一个对话框,在这个对话框中,用户可以检查几个项目,然后用一个 "添加 "按钮完成。

我创建了一个自定义baseadapter和一个带有网格视图的自定义对话框,对话框中也包含了'add'按钮。我的目标是能够检查哪些项目被选中。

BaseAdapter的布局有一些文本、图像和复选框。

对话框的构造函数。

public Custom_dialog_add(Context context, List<Oefening> oefeningList) {
    super(context);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.setContentView(R.layout.custom_dialog_add);

    gridView_add = (GridView) findViewById(R.id.custom_dialog_add_gridview);
    imageAdapter_add = new ImageAdapter_Add(getContext(), oefeningList);
    gridView_add.setAdapter(imageAdapter_add);
    imageAdapter_add.notifyDataSetChanged();

    // This is the add button
    Button add = (Button) findViewById(R.id.custom_dialog_add_button);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //Need something to do here 

        }
    });
}

BaseAdapter的getView

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

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    if (convertView == null) {

        gridView = new View(context);

        gridView = inflater.inflate(R.layout.custom_adapter_add, null);

        // set value into textview
        TextView textView = (TextView) gridView
                .findViewById(R.id.custom_adapter_add_textview);
        textView.setText(oefeningen.get(position).getName());

        //Checkbox
        final CheckBox checkBox = (CheckBox) gridView.findViewById(R.id.custom_adapter_add_checkbox);
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox.isChecked()){

                }else{
                }
            }
        });

        // set image based on selected text
        ImageView imageView = (ImageView) gridView
                .findViewById(R.id.custom_adapter_add_image);

    } else {
        gridView = (View) convertView;
    }
    return gridView;
}

我在网上找到了一些变通的方法,比如把东西保存在SharedPrefferences里,但是一定有更有效更简洁的方法... ... 如果需要更多的代码解释,请告诉我。

任何帮助将被感激!

如果需要更多的代码解释,请告诉我。

android gridview checkbox dialog baseadapter
1个回答
2
投票

你可以把这个放在你的Button CLickListener中。

checkGridView(gridView); //declare the GridView ad final that you could call it inside the listener

并定义这个方法

private void checkGridView(GridView gridView) {
    for (int i = 0; i < gridView.getChildCount() ; i++ ){
        View v = gridView.getChildAt(i);
        CheckBox checkBox = (CheckBox) v.findViewById(R.id.custom_adapter_add_checkbox);
        boolean itemChecked = checkBox.isChecked();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.