ListView上的按钮正在工作,但并不总是有效

时间:2015-08-06 07:06:52

标签: android button expandablelistview

       public class ListAdapter extends ArrayAdapter<ListItem> {
        private ArrayList<ListItem> listItems;
        private Context context;
        private boolean[] check = new boolean[10];
        private boolean checked;/////STRANGE!!


        public ListAdapter(Context context, int textViewResourceId, ArrayList<ListItem> listItems) {
            super(context, textViewResourceId, listItems);
            this.listItems = listItems;
            this.context = context;
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public int getItemViewType(int position) {
            if(position > 0) return 1;
            else return 0;
        }

        @Override
        @SuppressWarnings("deprecation")
        public View getView(final int position, View convertView, ViewGroup parent) {
            Log.i("EXPAND","in getView : "+position);

            int viewType =this.getItemViewType(position);
            ListViewHolder holder = null;
            ListItem listItem = listItems.get(position);
            switch(viewType){
                case 0:
                    if(convertView == null){
                        Log.i("EXPAND","null");
                        LinearLayout textViewWrap;
                        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        TextView text;
                        convertView = vi.inflate(R.layout.setting_list_item, null);
                        textViewWrap = (LinearLayout) convertView.findViewById(R.id.set_pull);
                        text = (TextView) convertView.findViewById(R.id.just_setting);
                        holder = new ListViewHolder(text);

                        // setViewWrap IS REQUIRED
                        holder.setViewWrap(textViewWrap);
                        //
                    }
                    else{
                        holder = (ListViewHolder) convertView.getTag();
                    }
                    holder.getViewWrap().setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, listItem.getCurrentHeight()));

                    holder.getTextView().setText(listItem.getText());

                    holder.getTextView().setCompoundDrawablesWithIntrinsicBounds(listItem.getDrawable(), 0, 0, 0);

                    convertView.setTag(holder);

                    // setHolder IS REQUIRED
                    listItem.setHolder(holder);
                    //
                    final Button scanBtn = (Button)convertView.findViewById(R.id.ScanAll);
                    scanBtn.setOnClickListener(new View.OnClickListener(){
                       public void onClick(View view){
                           Log.i("EXPAND","ScanBtn clicked");
                       }
                    });
                    return convertView;
                case 1:
                    if(convertView == null){
                        Log.i("EXPAND","null");
                        LinearLayout textViewWrap;
                        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        TextView text;
                        convertView = vi.inflate(R.layout.list_item, null);
                        textViewWrap = (LinearLayout) convertView.findViewById(R.id.text_wrap);
                        text = (TextView) convertView.findViewById(R.id.text);
                        holder = new ListViewHolder(text);

                        // setViewWrap IS REQUIRED
                        holder.setViewWrap(textViewWrap);
                        //
                    }
                    else{
                        holder = (ListViewHolder) convertView.getTag();
                    }
                    holder.getViewWrap().setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, listItem.getCurrentHeight()));

                    holder.getTextView().setText(listItem.getText());

                    holder.getTextView().setCompoundDrawablesWithIntrinsicBounds(listItem.getDrawable(), 0, 0, 0);

                    convertView.setTag(holder);

                    // setHolder IS REQUIRED
                    listItem.setHolder(holder);
                    //
                    //
                    final ImageButton check_box = (ImageButton) convertView.findViewById(R.id.check_button);
                    check_box.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {

                            // record what is selected
                            //.............
                            //change appearance
                            if (check[position]) {
                                Log.i("EXPAND","checked : "+position );
                                check[position] = false;
                                check_box.setBackgroundResource(R.drawable.empty);
                            } else {
                                Log.i("EXPAND","not checked : "+position);
                                check[position] = true;
                                check_box.setBackgroundResource(R.drawable.checked_check_box);
                            }
                        }
                    });
                    check_box.setFocusable(true);
                    //
                    return convertView;
                default:
                    Log.i("EXPAND","serverly default!!");
            }

           return convertView;
      } 
 }

Button正在工作但并不总是有效,特别是对于类型为0的scanBtn。

1 个答案:

答案 0 :(得分:0)

是的按钮正在工作,但并不总是因为你只是在viewType == 0,但是如果它等于1时定义了它,那么按钮onClick()就不会做任何事情

如果您希望按钮始终工作,则必须在其中的情况下再次定义该按钮 viewType == 1