CheckView中的CheckedTextView无法单击

时间:2016-03-26 08:10:01

标签: android android-layout listview checkedtextview

当我点击listView查看Checked文本视图时,应用程序停止。这是list_item.xml

        <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >

    <CheckedTextView
        android:id="@+id/checkedTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:gravity="center"
        android:drawableLeft="?android:attr/listChoiceIndicatorMultiple"
        />

</RelativeLayout>

这是活动的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" >

    <ListView
        android:id="@+id/list"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        >

    </ListView>
    <Button
        android:id="@+id/button1"
        android:layout_width="127dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Submit" />

</LinearLayout> 

这是活动的代码:

        package com.example.arf.mycitymyenvironment;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ProblemActivity extends Activity {
    ListView listView ;

    String[] values;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_problem);
//Toast.makeText(ProblemActivity.this,"new activity",Toast.LENGTH_SHORT).show();
        listView = (ListView) findViewById(R.id.list);
        values = new String[] { "Garbage","Water Pollution","Air Pollution" };

        // Define a new Adapter
        // First parameter - Context
        // Second parameter - Layout for the row
        // Third parameter - ID of the TextView to which the data is written
        // Forth - the Array of data

        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.list_item, R.id.checkedTextView, values);



        // Assign adapter to ListView
        listView.setAdapter(adapter);
        // we want multiple clicks
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnItemClickListener(new CheckBoxClick());


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

                // ListView Clicked item index
                int itemPosition = position;

                // ListView Clicked item value
                String itemValue = (String) listView.getItemAtPosition(position);




                //checkbox
                //checkBox.isChecked();

                // Show Alert
                Toast.makeText(getApplicationContext(),
                        "Position :" + itemPosition + "  ListItem : "+itemValue , Toast.LENGTH_LONG)
                        .show();


            }


        });
        */
    }

    public class CheckBoxClick implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // TODO Auto-generated method stub

            CheckedTextView ctv = (CheckedTextView)arg1;
            if(ctv.isChecked()){
                Toast.makeText(ProblemActivity.this, "now it is unchecked", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(ProblemActivity.this, "now it is checked", Toast.LENGTH_SHORT).show();
            }
        }
    }

}
每次

,错误为CheckedTextView ctv = (CheckedTextView)arg1;

我该怎么做才能解决它?

3 个答案:

答案 0 :(得分:1)

试试这个:

public class CheckBoxClick implements AdapterView.OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

        CheckedTextView ctv = (CheckedTextView)arg1.findViewById(R.id.checkedTextView);
        if(ctv.isChecked()){
            Toast.makeText(ProblemActivity.this, "now it is unchecked", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(ProblemActivity.this, "now it is checked", Toast.LENGTH_SHORT).show();
        }
    }
}

您的View arg1RelativeLayoutCheckedTextViewRelativeLayout arg1的孩子,因此您可以使用arg1.findViewById(R.id.checkedTextView);

获取

答案 1 :(得分:0)

public class CheckBoxClick implements AdapterView.OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

        CheckedTextView ctv = (CheckedTextView)findViewById(R.id.checkedTextView);
        if(ctv.isChecked()){
            Toast.makeText(ProblemActivity.this, "now it is unchecked", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(ProblemActivity.this, "now it is checked", Toast.LENGTH_SHORT).show();
        }
    }
}

试试这段代码。

答案 2 :(得分:0)

为每行中的CheckedTextView设置onClickListener,并在onClick事件中切换CheckedtextView的值。

在ListView适配器的getView方法中添加以下代码。

checkedTextView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        checkedTextView.setChecked(!checkedTextView.isChecked());
                    }
                });