0

我正在为我的自定义微调器使用 baseadapter,并带有允许用户选择多个值的复选框。我的应用程序中有一个更新按钮,我需要在复选框中将数据库中的值设置为 true。我的问题是我不知道该怎么做。例如,我的微调器中有 ["A","B","C","D"] 值,在我的数据库中我有 B 和 D。当我打开活动时,我将如何自动检查这些值。

这是填充我的自定义微调器的代码

private void initializeCustomerSegment() {
    final ArrayList<String> consumerSegments = new ArrayList<String>();
    List<String> consumerSegment = databaseHandler.setItemOnConsumerSeg();
    consumerSegments.addAll(consumerSegment);

    checkSelectedConsumerSegment = new boolean[consumerSegments.size()];
    //initialize all values of list to 'unselected' initially
    for (int i = 0; i < checkSelectedConsumerSegment.length; i++) {
        checkSelectedConsumerSegment[i] = false;
    } 

    final TextView tv_ConsumerSegment = (TextView) findViewById(R.DropDownList.tv_ConsumerSegment);
    tv_ConsumerSegment.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(!expandedConsumerSegment) {
                // display all selected values
                String selected = "";
                int flag = 0;
                for (int i = 0; i < consumerSegments.size(); i++) {
                    if (checkSelectedConsumerSegment[i] == true) {
                        selected += consumerSegments.get(i);
                        selected += ", ";
                        flag = 1;
                    }
                }

                if(flag == 1) {
                    tv_ConsumerSegment.setText(selected);
                }
                expandedConsumerSegment =true;
            } else {
                //display shortened representation of selected values
                tv_ConsumerSegment.setText(BrandListAdapter.getSelected());
                expandedConsumerSegment = false;
            }
        }
    });

    //onClickListener to initiate the dropDown list
    TextView tv_customerSegment = (TextView)findViewById(R.DropDownList.tv_ConsumerSegment);
    tv_customerSegment.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            initiatePopUpCustomerSegment(consumerSegments,tv_ConsumerSegment);
        }
    });
}

private void initiatePopUpCustomerSegment(ArrayList<String> customerSegments, TextView tv_CustomerSegment) {
    LayoutInflater inflater = (LayoutInflater)S_10th_IReportMain.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //get the pop-up window i.e.  drop-down layout
    LinearLayout layoutCustomerSegment = (LinearLayout)inflater.inflate(R.layout.pop_up_window_customersegment, (ViewGroup)findViewById(R.id.PopUpView1));

    //get the view to which drop-down layout is to be anchored
    RelativeLayout layout4 = (RelativeLayout)findViewById(R.id.relativeLayout4);
    pwConsumerSegment = new PopupWindow(layoutCustomerSegment, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);

    //Pop-up window background cannot be null if we want the pop-up to listen touch events outside its window
    pwConsumerSegment.setBackgroundDrawable(new BitmapDrawable());
    pwConsumerSegment.setTouchable(true);

    //let pop-up be informed about touch events outside its window. This  should be done before setting the content of pop-up
    pwConsumerSegment.setOutsideTouchable(true);
    pwConsumerSegment.setHeight(LayoutParams.WRAP_CONTENT);

    //dismiss the pop-up i.e. drop-down when touched anywhere outside the pop-up
    pwConsumerSegment.setTouchInterceptor(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                pwConsumerSegment.dismiss();
                return true;                    
            }
            return false;
        }
    });

    //provide the source layout for drop-down
    pwConsumerSegment.setContentView(layoutCustomerSegment);

    //anchor the drop-down to bottom-left corner of 'layout1'
    pwConsumerSegment.showAsDropDown(layout4);

    //populate the drop-down list
    final ListView listCustomerSegment = (ListView) layoutCustomerSegment.findViewById(R.DropDownList.dropDownCustomerSegment);
    ConsumerSegmentListAdapter adapter = new ConsumerSegmentListAdapter(this, customerSegments, tv_CustomerSegment);
    listCustomerSegment.setAdapter(adapter);
}

这是我的 ConsumerSegmentListAdapter。列表视图充当我的微调器。

public class ConsumerSegmentListAdapter extends BaseAdapter {
    private ArrayList<String> mListCustomerSegment;
    private LayoutInflater mInflater;
    private TextView mSelectedItems;
    private static int selectedCount = 0;
    private static String firstSelected = "";
    private ViewHolder holder;
    private static String selected = "";    //shortened selected values representation

    public static String getSelected() {
        return selected;
    }

    public void setSelected(String selected) {
        ConsumerSegmentListAdapter.selected = selected;
    }

    public ConsumerSegmentListAdapter(Context context, ArrayList<String> customerSegment,
            TextView tv) {
        mListCustomerSegment = new ArrayList<String>();
        mListCustomerSegment.addAll(customerSegment);
        mInflater = LayoutInflater.from(context);
        mSelectedItems = tv;
    }

    @Override
    public int getCount() {
        return mListCustomerSegment.size();
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.drop_down_customersegment, null);
            holder = new ViewHolder();
            holder.tv = (TextView) convertView.findViewById(R.DropDownList.SelectOptionCustomerSegment);
            holder.chkbox = (CheckBox) convertView.findViewById(R.DropDownList.checkboxCustomerSegment);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tv.setText(mListCustomerSegment.get(position));

        final int position1 = position;

        //whenever the checkbox is clicked the selected values textview is updated with new selected values
        holder.chkbox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                setText(position1);
            }
        });

        if(S_10th_IReportMain.checkSelectedConsumerSegment[position])
            holder.chkbox.setChecked(true);
        else
            holder.chkbox.setChecked(false);     
        return convertView;
    }


    /*
     * Function which updates the selected values display and information(checkSelectedConsumerSegment[])
     * */
    private void setText(int position1){
        if (!S_10th_IReportMain.checkSelectedConsumerSegment[position1]) {
            S_10th_IReportMain.checkSelectedConsumerSegment[position1] = true;
            selectedCount++;
        } else {
            S_10th_IReportMain.checkSelectedConsumerSegment[position1] = false;
            selectedCount--;
        }

        if (selectedCount == 0) {
            mSelectedItems.setText(R.string.select_consumersegment);
        } else if (selectedCount == 1) {
            for (int i = 0; i < S_10th_IReportMain.checkSelectedConsumerSegment.length; i++) {
                if (S_10th_IReportMain.checkSelectedConsumerSegment[i] == true) {
                    firstSelected = mListCustomerSegment.get(i);
                    break;
                }
            }
            mSelectedItems.setText(firstSelected);
            setSelected(firstSelected);
        } else if (selectedCount > 1) {
            for (int i = 0; i < S_10th_IReportMain.checkSelectedConsumerSegment.length; i++) {
                if (S_10th_IReportMain.checkSelectedConsumerSegment[i] == true) {
                    firstSelected = mListCustomerSegment.get(i);
                    break;
                }
            }
            mSelectedItems.setText(firstSelected + " & "+ (selectedCount - 1) + " more");
            setSelected(firstSelected + " & "+ (selectedCount - 1) + " more");
        }
    }

    private class ViewHolder {
        TextView tv;
        CheckBox chkbox;
    }
}
4

2 回答 2

1

我认为您需要为选定/未选定项目管理另一个存储并将其移动到适配器。例如可以HashSet<String>。然后代码将如下所示(注意,我将其设为可编译,因为无法编译问题中提供的代码):

public class S_10th_IReportMain extends Activity {

    boolean expandedConsumerSegment;
    ConsumerSegmentListAdapter mAdapter;

    private static class DatabaseHandler {

        List<String> setItemOnConsumerSeg() {
            return Collections.emptyList();
        }
    }

    private static class BrandListAdapter {

        static String getSelected() {
            return "string";
        }
    }

    DatabaseHandler databaseHandler = new DatabaseHandler();

    private void initializeCustomerSegment() {
        final ArrayList<String> consumerSegments = new ArrayList<String>();
        List<String> consumerSegment = databaseHandler.setItemOnConsumerSeg();
        consumerSegments.addAll(consumerSegment);

        final TextView tv_ConsumerSegment = (TextView) findViewById(R.id.tv_ConsumerSegment);
        tv_ConsumerSegment.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(!expandedConsumerSegment) {
                    // display all selected values
                    String selected = "";
                    int flag = 0;
                    for (String segment : consumerSegments) {
                        if (mAdapter.isChecked(segment)) {
                            selected += segment;
                            selected += ", ";
                            flag = 1;
                        }
                    }

                    if(flag == 1) {
                        tv_ConsumerSegment.setText(selected);
                    }
                    expandedConsumerSegment =true;
                } else {
                    //display shortened representation of selected values
                    tv_ConsumerSegment.setText(BrandListAdapter.getSelected());
                    expandedConsumerSegment = false;
                }
            }
        });

        //onClickListener to initiate the dropDown list
        TextView tv_customerSegment = (TextView)findViewById(R.id.tv_ConsumerSegment);
        tv_customerSegment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initiatePopUpCustomerSegment(consumerSegments,tv_ConsumerSegment);
            }
        });
    }

    PopupWindow pwConsumerSegment;

    private void initiatePopUpCustomerSegment(ArrayList<String> customerSegments, TextView tv_CustomerSegment) {
        LayoutInflater inflater = (LayoutInflater)S_10th_IReportMain.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        //get the pop-up window i.e.  drop-down layout
        LinearLayout layoutCustomerSegment = (LinearLayout)inflater.inflate(R.layout.pop_up_window_customersegment, (ViewGroup)findViewById(R.id.PopUpView1));

        //get the view to which drop-down layout is to be anchored
        RelativeLayout layout4 = (RelativeLayout)findViewById(R.id.relativeLayout4);
        pwConsumerSegment = new PopupWindow(layoutCustomerSegment, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        //Pop-up window background cannot be null if we want the pop-up to listen touch events outside its window
        pwConsumerSegment.setBackgroundDrawable(new BitmapDrawable());
        pwConsumerSegment.setTouchable(true);

        //let pop-up be informed about touch events outside its window. This  should be done before setting the content of pop-up
        pwConsumerSegment.setOutsideTouchable(true);
        pwConsumerSegment.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        //dismiss the pop-up i.e. drop-down when touched anywhere outside the pop-up
        pwConsumerSegment.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    pwConsumerSegment.dismiss();
                    return true;
                }
                return false;
            }
        });

        //provide the source layout for drop-down
        pwConsumerSegment.setContentView(layoutCustomerSegment);

        //anchor the drop-down to bottom-left corner of 'layout1'
        pwConsumerSegment.showAsDropDown(layout4);

        //populate the drop-down list
        final ListView listCustomerSegment = (ListView) layoutCustomerSegment.findViewById(R.id.dropDownCustomerSegment);
        ConsumerSegmentListAdapter adapter = new ConsumerSegmentListAdapter(this, customerSegments, tv_CustomerSegment);
        listCustomerSegment.setAdapter(adapter);
    }

    public static class ConsumerSegmentListAdapter extends BaseAdapter {
        private ArrayList<String> mListCustomerSegment;
        private LayoutInflater mInflater;
        private TextView mSelectedItems;
        private static int selectedCount = 0;
        private static String firstSelected = "";
        private ViewHolder holder;
        private static String selected = "";    //shortened selected values representation

        private HashSet<String> mCheckedItems;

        public static String getSelected() {
            return selected;
        }

        public void setSelected(String selected) {
            ConsumerSegmentListAdapter.selected = selected;
        }

        public ConsumerSegmentListAdapter(Context context, ArrayList<String> customerSegment,
                                          TextView tv) {
            mListCustomerSegment = new ArrayList<String>();
            mListCustomerSegment.addAll(customerSegment);
            mInflater = LayoutInflater.from(context);
            mSelectedItems = tv;
        }

        /**
         * Should be called then new data obtained from DB
         *
         * @param checkedItems array of strings obtained from DB
         */
        public void setCheckedItems(final String[] checkedItems) {
            mCheckedItems.clear();

            Collections.addAll(mCheckedItems, checkedItems);

            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mListCustomerSegment.size();
        }

        @Override
        public Object getItem(int arg0) {
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.drop_down_customersegment, null);
                holder = new ViewHolder();
                holder.tv = (TextView) convertView.findViewById(R.id.SelectOptionCustomerSegment);
                holder.chkbox = (CheckBox) convertView.findViewById(R.id.checkboxCustomerSegment);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            final String text = mListCustomerSegment.get(position);
            final boolean checked = isChecked(text);

            holder.tv.setText(mListCustomerSegment.get(position));

            //whenever the checkbox is clicked the selected values textview is updated with new selected values
            holder.chkbox.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    setText(position, checked, text);
                }
            });

            if(checked) {
                holder.chkbox.setChecked(true);
            } else {
                holder.chkbox.setChecked(false);
            }

            return convertView;
        }

        /*
         * Function which updates the selected values display and information(checkSelectedConsumerSegment[])
         * */
        private void setText(int position, boolean checked, String text){
            if (!checked) {
                mCheckedItems.add(text);
                selectedCount++;
            } else {
                mCheckedItems.remove(text);
                selectedCount--;
            }

            if (selectedCount == 0) {
                mSelectedItems.setText(R.string.select_consumersegment);
            } else if (selectedCount == 1) {
                firstSelected = mCheckedItems.iterator().next();
                mSelectedItems.setText(firstSelected);
                setSelected(firstSelected);
            } else if (selectedCount > 1) {
                firstSelected = mCheckedItems.iterator().next();
                mSelectedItems.setText(firstSelected + " & "+ (selectedCount - 1) + " more");
                setSelected(firstSelected + " & "+ (selectedCount - 1) + " more");
            }
        }

        /**
         * @param segment to be checked
         *
         * @return true if the segment is checked
         */
        public boolean isChecked(final String segment) {
            return mCheckedItems.contains(segment);
        }

        private class ViewHolder {
            TextView tv;
            CheckBox chkbox;
        }
    }
}

所以,然后你从数据库中获取新数据,应该检查你可以调用mAdapter.setCheckedItems().

于 2013-07-09T05:22:05.770 回答
0

我已经成功了。这样:

for (int j=0; j<brands.size(); j++)
    {
        for(String chosenElement : Brands) 
        {
           int index = brands.indexOf(chosenElement);
          checkSelected[index] = true;

        }
        }

我所做的是寻找我选择的数组列表到我的微调器适配器的索引,并将复选框索引设置为 true。就这么简单。无论如何,谢谢你的想法。

于 2013-07-10T09:22:34.153 回答