onListItemClick给出错误?

时间:2014-08-19 09:12:28

标签: android listview checkbox

我有一个listview,每行包含一个textview和复选框。现在我想要的是,当我点击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);
    }
}

现在在行super.onListItemClick(l,v,position,id); ,onListItemClick为红色。知道为什么吗?

克里斯蒂

2 个答案:

答案 0 :(得分:3)

因为Activity没有onListItemClick(),所以当你调用super.onListItemClick()时,它会抛出编译时错误,因为它的父类中没有这样的函数。如果您想使用onListItemClick(),则应该从ListActivity扩展您的课程。

答案 1 :(得分:1)

使用以下代码处理itemClickListener。

我们需要使用列表视图对象设置OnItemclickListener。我们重写onItemClick方法。

更新

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());
      }
        }
    });
   }