如何通过单击ListItem

时间:2015-06-02 04:50:14

标签: android android-layout listview

我在listview中有一个包含(TextView)和(CheckBox)的适配器。当我单击(onCLick)CheckBox时,我得到它的值,但是我希望通过单击List Item来选择CheckBox。下面是我的适配器。应该做出哪些更改,以便我可以选择listitem,它将检查CheckBox。

活动:

Vector<View> pages = new Vector<View>();

        for(int i = 0 ; i< page_section_group.size() ; i++){
            adapter = new UserSurveyAdapter(
                    UserSurActivity.this, (ArrayList<SurveyItem>)page_section_group.get(i));
            ListView l = new ListView(mContext);
            l.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            l.setDescendantFocusability(ListView.FOCUS_BEFORE_DESCENDANTS);
            l.setAdapter(adapter);
            listview_items.add(l);
            pages.add(l);
        }

        vp = (ViewPager) findViewById(R.id.viewpager);
        CustomPagerAdapter padapter = new CustomPagerAdapter(mContext,pages);
        vp.setAdapter(padapter);
        PageListener pageListener = new PageListener();

        vp.setOnPageChangeListener(pageListener);

        int index = vp.getCurrentItem();

        Log.d(TAG, "Total pages" + padapter.getCount());
        return padapter.getCount();

适配器:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d(TAG, "getView");
        View v = convertView;
        final int getPosition = position;
        final SurveyItem i = items.get(position);
        if (i != null) {
            Log.d(TAG, "i is not null");
            if(i.isSurvey()){
                Survey s = (Survey) i;
                v = vi.inflate(R.layout.survey, null);

                Log.d(TAG, "isSurvey");
                return v;   

            }else if(i.isQuestion()){
                Log.d(TAG, "isQuestion");
                Question qi = (Question) i;
                v = vi.inflate(R.layout.question, null);
                TextView question_text = (TextView) v.findViewById(R.id.question_text);
                question_text.setText(qi.getQuestionText());

            } else if(i.isSection()){
                Log.d(TAG, "isSection");
                Section si = (Section) i;
                v = vi.inflate(R.layout.section, null);
                TextView section_text = (TextView) v.findViewById(R.id.section_text);
                section_text.setText(si.getSectionName());
                section_text.setTypeface(null, Typeface.BOLD);
                section_text.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

            } else if(i.isSubSection()){
                Log.d(TAG, "isSubSection");
                SubSection si = (SubSection) i;
                v = vi.inflate(R.layout.subsection, null);
                TextView subsection_text = (TextView) v.findViewById(R.id.subsection_text);
                subsection_text.setText(si.getSubSectionText());


            } else if(i.isOption()){
                Log.d(TAG, "isOption");
                v = vi.inflate(R.layout.option_item, null);

                final Option oi = (Option) i;               
                TextView option_text = (TextView) v.findViewById(R.id.option_text);
                CheckBox option_image = (CheckBox) v.findViewById(R.id.option_image);
                Log.d(TAG, "Item selected " + oi.getOptionId());
                Log.d(TAG, "Item name " + oi.getOptionName());
                Log.d(TAG, "Item subid " + oi.getSubid());
                Log.d(TAG, "state is " + oi.isSelected());
                    if (oi.isSelected()) {
                        Log.d(TAG, "enable now");
                        option_image.setChecked(true);
                    } else {
                        Log.d(TAG, "disable now");
                        option_image.setChecked(false);
                    }

                option_image.setOnClickListener(new View.OnClickListener() {
                      public void onClick(View v) {
                        if (((CheckBox) v).isChecked()){
                            ifchecked = true;
                            Log.d("Button", "Checked");

                        }
                      else{
                            Log.d("Button", "Unchecked Checked");

                      }}
                });


                option_image.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                            @Override
                            public void onCheckedChanged(CompoundButton view,
                                    boolean isChecked) {
                                try{
                                    Log.d(TAG, "check now");
                                    Log.d(TAG, "state before" + oi.isSelected());
                                    oi.setSelected(view.isChecked());
                                    Log.d(TAG, "state after" + oi.isSelected());
                                    Log.d(TAG, "this state changed" + oi.getOptionName());

                                }catch(Exception e){
                                    Log.d(TAG, "exception");
                                }
                            }
                });

            }
        }
        return v;
    }

2 个答案:

答案 0 :(得分:1)

我认为你需要这个

lstviewName.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterview, View view,
                    int position, long id) {
                // this check the selected item from listview
                CheckBox chkboxName = (CheckBox) view
                        .findViewById(R.id.chkbox_name);
                chkboxName.performClick();

            }
        });

答案 1 :(得分:0)

XML

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
         <--! it should blocksDescendants -->
          android:descendantFocusability="blocksDescendants"
          android:layout_height="50dp"
          android:layout_marginBottom="10dp"
          android:layout_marginTop="10dp">

<CheckBox
    android:layout_width="0dp"
    android:layout_weight=".15"
    android:layout_height="wrap_content"
    android:clickable="false"
    android:id="@+id/checkBoxFriend"/>

listView.setOnItemClickListener( new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {

            CheckBox checkBox=(CheckBox)view.findViewById(R.id.check_box_id_in_xml);
            checkBox.performClick();
}