操作栏项目单击侦听器在第一次单击时不起作用

时间:2014-04-01 12:09:37

标签: android android-actionbar onclicklistener menuitem

我在操作栏上有收藏夹按钮,当用户点击它时,OnClickListener信息会保存到数据库或从数据库中删除。 但点击监听器在第二次点击时工作,而不是在第一次点击。所以在第一次点击时功能不会保存或删除任何内容,但它会通过第二次点击完成所有操作 onOptionsItemSelected函数运行良好,因为操作栏中的其他项目在第一次单击时起作用。 这部分来自onOptionsItemSelected;

 case R.id.action_favourite:
            String buttonTitle = item.getTitle().toString();
            if(buttonTitle.equals("Remove from favourites")){
                //item.setChecked(true);
                checked = true;}
            else {//item.setChecked(false);
                checked = false;}
            item.setOnMenuItemClickListener(new FavouriteStarListener(getActivity(), Integer.parseInt(cinemaid),name,url,ll,Integer.parseInt(movieNo),phone,checked));
            return true;

这是听众;

public class FavouriteStarListener implements android.view.MenuItem.OnMenuItemClickListener {

Context context;
int cinemaId;
String name,url,ll,phone;
int movieNumber;
boolean checked;

public FavouriteStarListener(Context context, int cinemaId, String url, String name, String ll, Integer movieNumber, String phone,Boolean ischecked){
    this.context = context;
    this.cinemaId = cinemaId;
    this.name = name;
    this.url = url;
    this.ll = ll;
    this.phone = phone;
    this.movieNumber = movieNumber;
    this.checked = ischecked;
}
public boolean onMenuItemClick(MenuItem menuItem) {

    FavouriteCinemasHelper dbHelper = new FavouriteCinemasHelper(context);
    SQLiteDatabase db = dbHelper.getWritableDatabase();

    //boolean checked = menuItem.isChecked();
    String tableName = FavouriteCinemasContract.FavouriteCinemasRow.TABLE_NAME;

    if(checked == false){
        // Add to the DB
        //Step 1: Build a map of values to be inserted
        ContentValues values = new ContentValues();
        values.put(FavouriteCinemasContract.FavouriteCinemasRow.COLUMN_NAME_CINEMA_ID, cinemaId);
        values.put(FavouriteCinemasContract.FavouriteCinemasRow.COLUMN_NAME_CINEMA_NAME, name);
        values.put(FavouriteCinemasContract.FavouriteCinemasRow.COLUMN_NAME_CINEMA_URL, url);
        values.put(FavouriteCinemasContract.FavouriteCinemasRow.COLUMN_NAME_CINEMA_LL, ll);
        values.put(FavouriteCinemasContract.FavouriteCinemasRow.COLUMN_NAME_CINEMA_MOVIE_NUMBER, movieNumber);
        values.put(FavouriteCinemasContract.FavouriteCinemasRow.COLUMN_NAME_CINEMA_PHONE, phone);

        //Step 2: Run the query
        long newRowId;
        newRowId = db.insert(tableName, null, values);
        Log.wtf("InsertID", newRowId + "");
        menuItem.setIcon(R.drawable.ic_action_important);
        String removeReminder = new String("Remove from favourites");
        menuItem.setTitle(removeReminder);
    } else {
        // Delete
        String selection = FavouriteCinemasContract.FavouriteCinemasRow.COLUMN_NAME_CINEMA_ID + " = ?";
        String[] selectionArgs = {String.valueOf(cinemaId)};
        db.delete(tableName, selection, selectionArgs);
        menuItem.setIcon(R.drawable.ic_action_not_important);
        String removeReminder = new String("Add to favourites");
        menuItem.setTitle(removeReminder);
    }
    return false;
}

}

0 个答案:

没有答案