如何逐步检查listview项目

时间:2014-03-27 16:04:08

标签: android listview checkbox android-listview

我是初学者,所以请容易。我有两个listview并行,我想要点击这个列表视图中的项目进行检查。我试图制作,但我不知道我哪里出错了,如果你能解释一下,如果它不正确,我理解如何使用复选标记制作这个列表视图。

这里onclick()我知道什么时候按下它并且我得到消息CHECK或UNCHECK,但是当我从listViewSelectFile CheckedTextView item =(CheckedTextView)视图中获取视图时;他给我这个错误:android.widgetlinearlayout无法强制转换为android.widget.checkedtextview。任何想法如何解决这个问题?

ArrayAdapter<String> adapterFile = new ArrayAdapter<String>(this,
                R.layout.list_view_rows, R.id.textView1, mImageFilenames);
        ArrayAdapter<String> adapterStudent = new ArrayAdapter<String>(
                this, R.layout.list_view_rows, R.id.textView1, Username_s);
    listViewSelectFile.setAdapter(adapterFile);
    listViewSelectStudent.setAdapter(adapterStudent);

    listViewSelectFile
            .setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent,
                        View view, int position, long id) {
                    // TODO Auto-generated method stub
                    // When clicked, show a toast with the TextView text
                    try {
                        LinearLayout item = (LinearLayout) view;
                        CheckedTextView CheckedTextView = (CheckedTextView)item.findViewById(R.id.textView1);

                        if (!item.isActivated()) {
                            String state = mImageFilenames[position]
                                    .toString();
                            // if(listViewSelectFile.isActivated())
                            Toast.makeText(getApplicationContext(),
                                    "Unclicked on : " + state,
                                    Toast.LENGTH_LONG).show();                          
                            listViewSelectFile.setItemChecked(position,
                                    false);                                 
                            CheckedTextView.setChecked(false);
                        } else {
                            String state = mImageFilenames[position]
                                    .toString();
                            // if(listViewSelectFile.isActivated())
                            Toast.makeText(getApplicationContext(),
                                    "Clicked on : " + state,
                                    Toast.LENGTH_LONG).show();
                            listViewSelectFile.setItemChecked(position,
                                    true);
                             CheckedTextView.setChecked(true);

                            }
                    } catch (Exception e) {
                        Toast.makeText(getApplicationContext(),
                                e.getMessage(), Toast.LENGTH_LONG)
                                .show();
                    }
                }
            });
    listViewSelectStudent
            .setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent,
                        View view, int position, long id) {
                    // TODO Auto-generated method stub

                    try{
                    String state = Username_s[position].toString();
                    Toast.makeText(getApplicationContext(),
                            "Clicked on : " + state, Toast.LENGTH_LONG)
                            .show();

                }catch(Exception e)
                {
                    Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
                }
                }

            });

和我的xml,2个并行列表视图:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false" >



        <ListView
            android:id="@+id/listviewSelectStudents"
            android:layout_width="0dip"
            android:layout_height="match_parent" 
            android:layout_weight="1"
            android:choiceMode="multipleChoice"
            >
        </ListView>


        <ListView
            android:id="@+id/listviewSelectFiles"
            android:layout_width="0dip"
            android:layout_height="match_parent" 
            android:layout_weight="1"
            android:choiceMode="multipleChoice">
        </ListView>


</LinearLayout>

list_view_rows.xml:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >



    <CheckedTextView  
      android:id="@+id/textView1" 
      android:paddingLeft="20dip" 
      android:paddingRight="20dip" 
      android:paddingTop="10dip"
      android:paddingBottom="10dip" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="?android:attr/listPreferredItemHeight"  
      android:gravity="center_vertical"  
      android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
      android:onClick="toggle" /> 

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您没有指定R.layout.list_view_rows代码,但我敢打赌您使用的是TextView而不是CheckedTextView。您需要CheckedTextView才能添加CheckBoxes。