OnListItemClick无法正常工作

时间:2012-08-08 19:54:52

标签: java android listview

在我的应用程序中,我有一个扩展ListActivity并使用ListView的类。我的onListItemClick方法只是不想工作。在方法内部,它必须确定被点击的项目是锻炼A还是锻炼B.这是代码:

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        Long rowId = id;

        Cursor cursor = mDbHelper.fetchNote(rowId);

        String activity = cursor.getString(cursor.getColumnIndexOrThrow(StrongDbAdapter.WORKOUT_STATE));

        if(activity == "Workout A"){
            workoutAA = true;
        }else if(activity == "Workout B"){
            workoutAA = false;
        }


        if(workoutAA == true){
            Intent i = new Intent(this, WorkoutEditA.class);
            i.putExtra(StrongDbAdapter.KEY_ROWID, id);
            startActivityForResult(i, ACTIVITY_EDIT);
        }else if(activity == "Workout B"){
            Intent i = new Intent(this, WorkoutEditB.class);
            i.putExtra(StrongDbAdapter.KEY_ROWID, id);
            startActivityForResult(i, ACTIVITY_EDIT);
        }


    }

1 个答案:

答案 0 :(得分:2)

activity.equals( "Workout A")。当字符串比较使用equals()

避免NullPointerException的另一种方法是

"Workout A".equals(acitivty)