Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android onListItemClick给出错误?_Android_Listview_Checkbox - Fatal编程技术网

Android onListItemClick给出错误?

Android onListItemClick给出错误?,android,listview,checkbox,Android,Listview,Checkbox,我有一个列表视图,每行包含一个文本视图和复选框。现在我想要的是,当我单击textview时,必须相应地选中复选框。我尝试使用以下代码: protected void onListItemClick(ListView l, View v, int position, long id) { Toast.makeText(getApplicationContext(), "You have selected item no." +(position+1)+"", Toast

我有一个列表视图,每行包含一个文本视图和复选框。现在我想要的是,当我单击textview时,必须相应地选中复选框。我尝试使用以下代码:

protected void onListItemClick(ListView l, View v, int position, long id) {

Toast.makeText(getApplicationContext(), "You have selected item no."
             +(position+1)+"", Toast.LENGTH_SHORT).show();

super.onListItemClick(l, v, position, id);

if (v != null) {
    CheckBox checkBox = (CheckBox)v.findViewById(R.id.Checkbox);
    checkBox.setChecked(!checkBox.isChecked());
    }
} 
这是我的全部代码:

public class MyActivity3 extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my3);
    Button m = (Button) findViewById(R.id.button3);
    tv = (TextView) findViewById(R.id.textViewcat);
    Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
    tv.setTypeface(typeface);

    String[] listArray = new String[] { "All", "Friends & Family", "Sports", "Outside", "At School", "Fitness", "Photography", "Food", "Beach", "Money" };
    SharedPreferences sharedPreferences = getSharedPreferences("status", MODE_PRIVATE);

    Boolean[] checkedStatus = new Boolean[listArray.length];
    for ( int index = 0; index < checkedStatus.length; index++)
        checkedStatus[index] = sharedPreferences.getBoolean(Integer.toString(index), false);

    ListView listView = (ListView) findViewById(R.id.listView);
    MyAdapter adapter = new MyAdapter(this, R.layout.row_layout, listArray, checkedStatus);
    listView.setAdapter(adapter);
}
protected void onListItemClick(ListView l, View v, int position, long id) {

    Toast.makeText(getApplicationContext(), "You have selected item no."
            +(position+1)+"", Toast.LENGTH_SHORT).show();

    super.onListItemClick(l, v, position, id);

    if (v != null) {
        CheckBox checkBox = (CheckBox)v.findViewById(R.id.chk);
        checkBox.setChecked(!checkBox.isChecked());
    }
}
@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.animation8, R.anim.animation7);
    }
}
公共类MyActivity3扩展活动{
私家图文电视;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my3);
按钮m=(按钮)findViewById(R.id.button3);
tv=(TextView)findViewById(R.id.textViewcat);
Typeface-Typeface=Typeface.createFromAsset(getAssets(),“BebasNeue Bold.ttf”);
设置字体(字体);
String[]listArray=新字符串[]{“所有”、“朋友和家人”、“体育”、“户外”、“学校”、“健身”、“摄影”、“食品”、“海滩”、“金钱”};
SharedReferences SharedReferences=GetSharedReferences(“状态”,模式\私有);
Boolean[]checkedStatus=新的Boolean[listary.length];
对于(int index=0;index
现在在super.onListItem行中单击(l,v,position,id),onListItemClick是红色的。知道为什么吗


Cristiano

因为活动没有
onListItemClick()
,所以当您调用
super.onListItemClick()
时,它会抛出编译时错误,因为它的父类中没有这样的函数。如果您想使用
onListItemClick()
您应该从
ListActivity
扩展您的类,请使用以下代码处理itemClickListener

我们需要使用列表视图对象来设置MClickListener。在其中,我们重写了mclick方法

更新

public class MyActivity3 extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my3);
Button m = (Button) findViewById(R.id.button3);
tv = (TextView) findViewById(R.id.textViewcat);
Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
tv.setTypeface(typeface);

String[] listArray = new String[] { "All", "Friends & Family", "Sports", "Outside", "At School", "Fitness", "Photography", "Food", "Beach", "Money" };
SharedPreferences sharedPreferences = getSharedPreferences("status", MODE_PRIVATE);

Boolean[] checkedStatus = new Boolean[listArray.length];
for ( int index = 0; index < checkedStatus.length; index++)
    checkedStatus[index] = sharedPreferences.getBoolean(Integer.toString(index), false);

ListView listView = (ListView) findViewById(R.id.listView);
MyAdapter adapter = new MyAdapter(this, R.layout.row_layout, listArray, checkedStatus);
listView.setAdapter(adapter);

listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // do here whatever you want :-)
            Toast.makeText(getApplicationContext(), "You have selected item no."
        +(position+1)+"", Toast.LENGTH_SHORT).show();  

        if (view != null) {
        CheckBox checkBox = (CheckBox)v.findViewById(R.id.chk);
        checkBox.setChecked(!checkBox.isChecked());
      }
        }
    });
   }
公共类MyActivity3扩展活动{
私家图文电视;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my3);
按钮m=(按钮)findViewById(R.id.button3);
tv=(TextView)findViewById(R.id.textViewcat);
Typeface-Typeface=Typeface.createFromAsset(getAssets(),“BebasNeue Bold.ttf”);
设置字体(字体);
String[]listArray=新字符串[]{“所有”、“朋友和家人”、“体育”、“户外”、“学校”、“健身”、“摄影”、“食品”、“海滩”、“金钱”};
SharedReferences SharedReferences=GetSharedReferences(“状态”,模式\私有);
Boolean[]checkedStatus=新的Boolean[listary.length];
对于(int index=0;index
您可以使用:listView.setonItemClickListener(此),或者您可以从ListActivity扩展您的类。@CristianoWilson:请阅读本教程:而不是我的onListItem单击“我现在有了”?在oncreate方法中编写它,而不是在oncreate之外好的,它不再是红色的。我需要将此代码放在我的问题的方法中吗?:CheckBox cb=(CheckBox)v.findViewById(R.id.CheckBox)如果(cb.isChecked())cb.setChecked(false);else cb.setChecked(true);检查我的更新答案..你可以在onItemclickListener中实现你的逻辑,比如复选框id是否选中,v.是否为红色,删除v是否重要?**编辑它不起作用..应用程序不会崩溃,但当我点击该行时,复选框不会被标记