SQLite - RecyclerView - 许多表不起作用

时间:2018-01-03 18:26:28

标签: java android android-sqlite

我在sqlite中创建两个表时遇到问题,并在RecyclerView中显示它们,更具体地说是在一个表中显示它们。我在问题中添加了应用程序的屏幕截图,以便您可以了解问题所在。

我有3个活动:FirstActivity,其中有两个按钮(星期一和星期二) - >从这个活动,我们转到MainActivity,按钮选择(星期一) - >在table_Ton中的table_MON和选择按钮(星期二)之后保存数据。问题是一切正常,当我们按下按钮(星期一)时,我们可以保存培训,编辑和删除它。但是,当我们转到按钮(星期二)时,我们只能添加培训,但我们无法编辑或删除它们。但是,我不明白为什么?我已经看了这个问题一个星期,我不知道发生了什么,也许一个新的外观将有助于解决问题。 我的数据库由两个表组成:tableMon和tableTue,但我在其中使用相同的列(练习,重量等),并单独_id,我不知道这是否是一个问题。也许问题是我在两种情况下都使用MainActivity?

截图应用

FirstActivity

enter image description here

点击按钮星期一 - > MainActivity.class enter image description here

点击按钮星期二 - > MainActivity.class enter image description here

按钮星期二 - > DetailActivity和她是我无法更新和删除培训的问题。 enter image description here

FirstActivity.class

public class FirstActivity extends AppCompatActivity {
   Button buttonMon, buttonTue;

    public static final String BUTTON_KEY1 = "BUTTON_KEY";
    public static final String BUTTON_KEY2 = "BUTTON_KEY2";
    public static final String BUTTON_VALUE = "1";
    public static final String BUTTON_VALUE2 = "2";

    public static  boolean WAS_RUNNING;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        buttonMon = (Button) findViewById(R.id.buttonMonday);
        buttonTue = (Button) findViewById(R.id.buttonTuesday);

        WAS_RUNNING = false;

        //OPEN THE SAME MAINACTIVITY
        buttonMon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WAS_RUNNING = true;
                Intent intent = new Intent(FirstActivity.this, MainActivity.class);
                intent.putExtra(BUTTON_KEY1, BUTTON_VALUE);

                startActivity(intent);
            }    });
        //OPEN THE SAME MAINACTIVITY
        buttonTue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WAS_RUNNING = true;
                Intent i = new Intent(FirstActivity.this, MainActivity.class);
                i.putExtra(BUTTON_KEY2, BUTTON_VALUE2);
                startActivity(i);
            }    });}}

MainActivity.class

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private MyAdapter myAdapter;

    private ArrayList<Training> trainingArrayList = new ArrayList<Training>();

    private EditText editTextExercise, editTextWeight, editTextRepeat, editTextSeries;
    private Button buttonSave, buttonBack;

    private Dialog dialog;

    private String value;
    private String value2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         Bundle bundle = getIntent().getExtras();

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog();}});

        if(bundle == null)
        {return;}

        value = bundle.getString(FirstActivity.BUTTON_KEY1);

        value2 = bundle.getString(FirstActivity.BUTTON_KEY2);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        if(value != null) {

            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerView.setItemAnimator(new DefaultItemAnimator());

            myAdapter = new MyAdapter(this, trainingArrayList);

            //Refresh MON TABLE
            refreshMonTable();

        }else if(value2 != null) {

            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerView.setItemAnimator(new DefaultItemAnimator());

            myAdapter = new MyAdapter(this, trainingArrayList);

            //Refresh TUE TABLE
            refreshTueTable(); }}

    public void showDialog()
    {
         dialog = new Dialog(this);

        dialog.setContentView(R.layout.dialog_layout);

        editTextExercise = (EditText) dialog.findViewById(editTextExerciseDialog);
        editTextWeight = (EditText) dialog.findViewById(editTextWeightDialog);
        editTextRepeat = (EditText) dialog.findViewById(editTextRepeatDialog);
        editTextSeries = (EditText) dialog.findViewById(editTextSeriesDialog);

        buttonSave = (Button) dialog.findViewById(buttonSaveDialog);
        buttonBack = (Button) dialog.findViewById(buttonBackDialog);

        buttonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();}});

        buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(value != null) {
                    saveDataMon(editTextExercise.getText().toString(),
                            String.valueOf(editTextWeight.getText().toString()),
                            String.valueOf(editTextRepeat.getText().toString()),
                            String.valueOf(editTextSeries.getText().toString()));
                } else if(value2 != null) {
                    saveDataTue(editTextExercise.getText().toString(),
                            String.valueOf(editTextWeight.getText().toString()),
                            String.valueOf(editTextRepeat.getText().toString()),
                            String.valueOf(editTextSeries.getText().toString()));
                }}});

        dialog.show();}

    private void saveDataMon(String exercise, String weight, String repeat, String series)
    {
            DBAdapter adapter = new DBAdapter(this);

            adapter.openDatabase();

            long result = adapter.addMon(exercise, weight, repeat, series);

            if (result > 0) {
                editTextExercise.setText("");
                editTextWeight.setText("");
                editTextRepeat.setText("");
                editTextSeries.setText("");

                dialog.dismiss();

            } else {
                Snackbar.make(editTextExercise, "Unable to save!", Snackbar.LENGTH_SHORT).show();}

            adapter.closeDB();

            refreshMonTable();}

    private void saveDataTue(String exercise, String weight, String repeat, String series)
    {
        DBAdapter adapter = new DBAdapter(this);

            adapter.openDatabase();

            long result = adapter.addTue(exercise, weight, repeat, series);

            if (result > 0) {
                editTextExercise.setText("");
                editTextWeight.setText("");
                editTextRepeat.setText("");
                editTextSeries.setText("");

                dialog.dismiss();

            } else {
                Snackbar.make(editTextExercise, "Unable to save!", Snackbar.LENGTH_SHORT).show();
            }

            adapter.closeDB();

            refreshTueTable();}

    private void refreshMonTable()
    {
        DBAdapter adapter = new DBAdapter(this);

        adapter.openDatabase();

        trainingArrayList.clear();

        Cursor cursor = adapter.getAllTreningMon();

        if(cursor != null)
        { if(cursor.moveToFirst())
         {
            do {
                int id = cursor.getInt(0);
                String cwiczenie = cursor.getString(1);
                String ciezar = cursor.getString(2);
                String powtorzenia = cursor.getString(3);
                String serie = cursor.getString(4);

                Training training = new Training(id, cwiczenie, ciezar, powtorzenia, serie);
                trainingArrayList.add(training);

            }while (cursor.moveToNext());}}

        recyclerView.setAdapter(myAdapter);

        adapter.closeDB(); }

   private void refreshTueTable()
    {
        DBAdapter adapter = new DBAdapter(this);

        adapter.openDatabase();

        trainingArrayList.clear();

        Cursor cursor = adapter.getAllTreningTue();

        if(cursor != null)
        { if(cursor.moveToFirst())
        {
            do {
                int id = cursor.getInt(0);
                String exercise = cursor.getString(1);
                String weight = cursor.getString(2);
                String repeat = cursor.getString(3);
                String series = cursor.getString(4);

                Training training = new Training(id, exercise, weight, repeat, series);
                trainingArrayList.add(training);

            }while (cursor.moveToNext());}}

        recyclerView.setAdapter(myAdapter);
        adapter.closeDB();
    }
    @Override
    protected void onResume() {
        super.onResume();

       if(value != null) {
            //REFRESH
           refreshMonTable();
        } else if(value2 != null) {
           refreshTueTable();}}
    @Override
    public void onBackPressed() {

        new AlertDialog.Builder(this)
                .setTitle("Exit?")
                .setMessage("Do you want to exit?")
                .setNegativeButton("No", null)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                      MainActivity.super.onBackPressed();
                    }
                }).create().show();}}

DetailActivity.class

public class DetailActivity extends AppCompatActivity {

    private EditText editTextExerciseDetail, editTextWeightDetail, editTextRepeatDetail, editTextSeriesDetail;

    private Button buttonUpdate;
    private Button buttonDelete;

    String mon;
    String tue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        Bundle intent = getIntent().getExtras();

        if(intent == null)
        {return;}

                mon = intent.getString(MyAdapter.KEY_MON);
                tue = intent.getString(MyAdapter.KEY_TUE);

            final int idT = intent.getInt("id");
            String cwiczenie = intent.getString("exercise");
            String ciezar = intent.getString("weight");
            String powtorzenia = intent.getString("repeat");
            String serie = intent.getString("series");

        editTextExerciseDetail = (EditText) findViewById(R.id.exerciseEditTxtDetail);
        editTextWeightDetail = (EditText) findViewById(R.id.weightEditTextDetail);
        editTextRepeatDetail = (EditText) findViewById(R.id.repeatEditTextDetail);
        editTextSeriesDetail = (EditText) findViewById(R.id.seriesEditTextDetail);

        buttonUpdate = (Button) findViewById(R.id.updateBtn);
        buttonDelete = (Button) findViewById(R.id.deleteBtn);

        editTextExerciseDetail.setText(cwiczenie);
        editTextWeightDetail.setText(ciezar);
        editTextRepeatDetail.setText(powtorzenia);
        editTextSeriesDetail.setText(serie);

        buttonUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(mon != null) {
                    updateMon(idT, editTextExerciseDetail.getText().toString(),
                            editTextWeightDetail.getText().toString(),
                            editTextRepeatDetail.getText().toString(),
                            editTextSeriesDetail.getText().toString());
                } else if(tue != null) {
                    updateTue(idT, editTextExerciseDetail.getText().toString(),
                            editTextWeightDetail.getText().toString(),
                            editTextRepeatDetail.getText().toString(),
                            editTextSeriesDetail.getText().toString());}}
        });

        buttonDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               AlertDialog.Builder builder = new AlertDialog.Builder(DetailActivity.this);

                builder.setMessage("Do you want delete this training?")
                        .setTitle("Delete")
                        .create();

               builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   @Override
                    public void onClick(DialogInterface dialog, int which) {

                       if(mon != null) {
                           deleteMon(idT);
                       }else if(tue != null){
                           deleteTue(idT);}}});

                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }});
                builder.show();}});}

    //Update MON TABLE
    private void updateMon(int id, String newExercise, String newWeight, String newRepeat, String newSeries)
    {
        DBAdapter dbAdapter = new DBAdapter(this);
        dbAdapter.openDatabase();

        long result = dbAdapter.updateMon(id, newExercise, newWeight, newRepeat, newSeries);

        if(result > 0)
        {
            editTextExerciseDetail.setText(newExercise);
            editTextWeightDetail.setText(newWeight);
            editTextRepeatDetail.setText(newRepeat);
            editTextSeriesDetail.setText(newSeries);

            this.finish();

            Snackbar.make(editTextExerciseDetail, "Updated Succesfully", Snackbar.LENGTH_SHORT).show();

        }else
        {
            Snackbar.make(editTextExerciseDetail, "Unable to updated", Snackbar.LENGTH_SHORT).show();
        }

        dbAdapter.closeDB();
    }

    //Update TUE TABLE
    private void updateTue(int id, String newExercise, String newWeight, String newRepeat, String newSeries)
    {
        DBAdapter dbAdapter = new DBAdapter(this);
        dbAdapter.openDatabase();

        long result = dbAdapter.updateTue(id, newExercise, newWeight, newRepeat, newSeries);

        if(result > 0)
        {
            editTextExerciseDetail.setText(newExercise);
            editTextWeightDetail.setText(newWeight);
            editTextRepeatDetail.setText(newRepeat);
            editTextSeriesDetail.setText(newSeries);

            this.finish();

            Snackbar.make(editTextExerciseDetail, "Updated Succesfully", Snackbar.LENGTH_SHORT).show();

        }else
        {
            Snackbar.make(editTextExerciseDetail, "Unable to updated", Snackbar.LENGTH_SHORT).show();
        }

        dbAdapter.closeDB();}

    //DELETE MON TABLE
   private void deleteMon(int id)
   {
       DBAdapter dbAdapter = new DBAdapter(this);
       dbAdapter.openDatabase();

       long result = dbAdapter.deleteMon(id);

       if(result>0)
       {
           this.finish();

       }else
       {
           Snackbar.make(editTextExerciseDetail, "Unable to deleted", Snackbar.LENGTH_SHORT).show();
       }
       dbAdapter.closeDB();
   }

    //DELETE TUE TABLE
    private void deleteTue(int id)
    {
        DBAdapter dbAdapter = new DBAdapter(this);
        dbAdapter.openDatabase();

        long result = dbAdapter.deleteTue(id);

        if(result>0)
        {
            this.finish();

        }else
        {
            Snackbar.make(editTextExerciseDetail, "Unable to deleted", Snackbar.LENGTH_SHORT).show();
        }
        dbAdapter.closeDB();}

    @Override
    protected void onResume() {
        super.onResume();}
    @Override
    protected void onRestart() {
        super.onRestart();}}

DBHelper.class

public class DBHelper extends SQLiteOpenHelper {

    //DB
    static final String DB_NAME = "training_db";

    static final int DB_VERSION = '1';

    //TABLES
    static final String TB_NAME_MON = "training_tb_mon";
    static final String TB_NAME_TUE = "training_tb_tue";

    //COLUMNS
    static final String ROW_ID_MON = "id_Mon";
    static final String ROW_ID_TUE = "id_Tue";

    //ROW
    static final String EXERCISE = "exercise";
    static final String WEIGHT = "weight";
    static final String REPEAT = "repeat";
    static final String SERIES = "series";

    static final String CREATE_TB_MON = "CREATE TABLE training_tb_mon (id_Mon INTEGER PRIMARY KEY AUTOINCREMENT,"
            + "exercise TEXT NOT NULL, weight TEXT NOT NULL, repeat TEXT NOT NULL, series TEXT NOT NULL);";

    static final String CREATE_TB_TUE = "CREATE TABLE training_tb_tue (id_Tue INTEGER PRIMARY KEY AUTOINCREMENT,"
           + "exercise TEXT NOT NULL, weight TEXT NOT NULL, repeat TEXT NOT NULL, series TEXT NOT NULL);";

    public DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);}

    @Override
    public void onCreate(SQLiteDatabase db) {

        try {
            db.execSQL(CREATE_TB_MON);
            db.execSQL(CREATE_TB_TUE);
        }catch (Exception e) {
            e.printStackTrace();
        }}
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TB_NAME_MON);
        db.execSQL("DROP TABLE IF EXISTS " + TB_NAME_TUE);

        onCreate(db);
    }}

DBAdapter.class

public class DBAdapter {

    Context c;
    DBHelper dbHelper;
    SQLiteDatabase db;

    public DBAdapter(Context c) {
        this.c = c;
        dbHelper = new DBHelper(c);
    }

    //OPEN DATABASE
    public DBAdapter openDatabase() {

        try {
            db = dbHelper.getWritableDatabase();
        }catch (SQLiteException e) {
            e.printStackTrace();
       }
        return this;
    }
    //CLOSE DATABASE
    public void closeDB()
    {
        try{
            dbHelper.close();
        }catch (SQLiteException e){
            e.printStackTrace();
        }}

    //Add training Mon
    public long addMon(String exercise, String weight, String repeat, String series)
    {
        try{
            ContentValues contentValues = new ContentValues();
            contentValues.put(DBHelper.EXERCISE, exercise);
            contentValues.put(DBHelper.WEIGHT, weight);
            contentValues.put(DBHelper.REPEAT, repeat);
            contentValues.put(DBHelper.SERIES, series);
            return db.insert(DBHelper.TB_NAME_MON, DBHelper.ROW_ID_MON, contentValues);

        }catch (SQLiteException e) {
         e.printStackTrace();   }
        return 0;}
    //Add training Tue
    public long addTue(String exercise, String weight, String repeat, String series)
    {
        try{
            ContentValues contentValues = new ContentValues();
            contentValues.put(DBHelper.EXERCISE, exercise);
            contentValues.put(DBHelper.WEIGHT, weight);
            contentValues.put(DBHelper.REPEAT, repeat);
            contentValues.put(DBHelper.SERIES, series);
            return db.insert(DBHelper.TB_NAME_TUE, DBHelper.ROW_ID_TUE, contentValues);

        }catch (SQLiteException e) {
            e.printStackTrace();
        }
        return 0;
    }
    //REFRESH TABLE MON

    public long updateMon(int id, String exercise, String weight, String repeat, String series)
    {
       try{
            ContentValues contentValues = new ContentValues();
            contentValues.put(DBHelper.EXERCISE, exercise);
            contentValues.put(DBHelper.WEIGHT, weight);
            contentValues.put(DBHelper.REPEAT, repeat);
            contentValues.put(DBHelper.SERIES, series);
            contentValues.put(DBHelper.ROW_ID_MON, id);

            return db.update(DBHelper.TB_NAME_MON, contentValues, DBHelper.ROW_ID_MON + " =?", new String[]{String.valueOf(id)});

        }catch (SQLiteException e) {
            e.printStackTrace();   }
       return 0;
    }

    //REFRESH TABLE TUE
    public long updateTue(int id, String exercise, String weight, String repeat, String series)
    {
        try{
            ContentValues contentValues = new ContentValues();
            contentValues.put(DBHelper.EXERCISE, exercise);
            contentValues.put(DBHelper.WEIGHT, weight);
            contentValues.put(DBHelper.REPEAT, repeat);
            contentValues.put(DBHelper.SERIES, series);
            contentValues.put(DBHelper.ROW_ID_TUE, id);

            return db.update(DBHelper.TB_NAME_TUE, contentValues, DBHelper.ROW_ID_TUE + " =?", new String[]{String.valueOf(id)});

        }catch (SQLiteException e) {
            e.printStackTrace();
        }
        return 0;
    }
    //DELETE ROW TABLE MON
    public long deleteMon(int id)
    {
        try{
            return db.delete(DBHelper.TB_NAME_MON, DBHelper.ROW_ID_MON + " =?", new String[]{String.valueOf(id)});
       }catch (SQLiteException e) {
            e.printStackTrace();
        }
        return 0;    }

    //DELETE ROW TABLE TUE
    public long deleteTue(int id)
    {
        try{
            return db.delete(DBHelper.TB_NAME_TUE, DBHelper.ROW_ID_TUE + " =?", new String[]{String.valueOf(id)});

        }catch (SQLiteException e) {
           e.printStackTrace();}

        return 0;}

    //ALL TRAINING TABLE MON

    public Cursor getAllTreningMon() {

        try{
            String[] columns = {DBHelper.ROW_ID_MON, DBHelper.EXERCISE, DBHelper.WEIGHT, DBHelper.REPEAT, DBHelper.SERIES};

            return db.query(DBHelper.TB_NAME_MON, columns, null, null, null, null, null);

        }catch (SQLiteException e)
        {
            e.printStackTrace();
        }
        return null;
    }
    //ALL TRAINING TABLE TUE
   public Cursor getAllTreningTue() {

        try{
            String[] columns = {DBHelper.ROW_ID_TUE, DBHelper.EXERCISE, DBHelper.WEIGHT, DBHelper.REPEAT, DBHelper.SERIES};

            return db.query(DBHelper.TB_NAME_TUE, columns, null, null, null, null, null);

        }catch (SQLiteException e)
        {
            e.printStackTrace();
        }
        return null;}}

MyAdapter.class

public class MyAdapter extends RecyclerView.Adapter<MyHolder> {

    Context c;
    ArrayList<Training> training;

    public static String KEY_MON = "KEY_MON";
    public static String KEY_TUE = "KEY_TUE";
    public static String VALUE_MON = "10";
    public static String VALUE_TUE = "20";

    public <T extends Training> MyAdapter(Context c, ArrayList<Training> training) {
        this.c = c;
        this.training = training;
    }
    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(c).inflate(R.layout.card_view_model, parent, false);

        return new MyHolder(v);
    }
    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {

        holder.textViewModel.setText(training.get(position).getExercise());

        holder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(View v) {
                if (FirstActivity.WAS_RUNNING)

                {Intent intent = new Intent(c, DetailActivity.class);

                    intent.putExtra(KEY_MON, VALUE_MON);
                    intent.putExtra("id", training.get(position).getId());
                    intent.putExtra("exercise", training.get(position).getExercise());
                    intent.putExtra("weight", training.get(position).getWeight());
                    intent.putExtra("repeat", training.get(position).getRepeat());
                    intent.putExtra("series", training.get(position).getSeries());

                    c.startActivity(intent);

                } else if (FirstActivity.WAS_RUNNING)

                {Intent intent2 = new Intent(c, DetailActivity.class);

                    intent2.putExtra(KEY_TUE, VALUE_TUE);
                    intent2.putExtra("id", training.get(position).getId());
                    intent2.putExtra("exercise", training.get(position).getExercise());
                    intent2.putExtra("weight", training.get(position).getWeight());
                    intent2.putExtra("repeat", training.get(position).getRepeat());
                    intent2.putExtra("series", training.get(position).getSeries());

                    c.startActivity(intent2);
                }}});}

    @Override
    public int getItemCount() {
        return training.size();}}

Training.class

public class Training {

     int id;
     String exercise;
     String weight;
     String repeat;
     String series;
    public Training(int id, String exercise, String weight, String repeat, String series) {
        this.id = id;
        this.exercise = exercise;
        this.weight = weight;
        this.repeat = repeat;
        this.series = series;
    }
    public int getId() {return id;}
    public void setId(int id) { this.id = id;}
    public String getExercise() {return exercise; }
    public void setExercise(String exercise) {this.exercise = exercise;}
    public String getWeight() {return weight;}
    public void setWeight(String weight) {this.weight = weight; }
    public String getRepeat() {return repeat;}
    public void setRepeat(String repeat) { this.repeat = repeat;}
    public String getSeries() {return series;}
    public void setSeries(String series) {this.series = series;}}

1 个答案:

答案 0 :(得分:0)

在MyAdapter课程中,在onItemClick方法的onBindViewHolder方法中,对于 Tue 处理,第二次检查将永远无法运行。

你有: -

if (FirstActivity.WAS_RUNNING) {
    .......
} else if (FirstActivity.WAS_RUNNING) {
    .......
}

即。你是说如果FirstActivity没有运行并且FirstActivity正在运行那么你就做了。  即只有在FirstActivity未运行时才会输入 else ,在这种情况下,if (FirstActivity.WAS_RUNNING)永远不会为真。

也许有2个布尔值MONDAY_WAS_RUNNING和TUESDAY_WAS RUNNING(你可以使用现有的布尔值,在buttonTue监听器中将其设置为false,在else中进行星期二处理(不带if))。

e.g。 : -

FirstActivity类: -

    public static  boolean MONDAY_WAS_RUNNING;
    public static  boolean TUESDAY_WAS_RUNNING;

    ..........


    buttonMon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MONDAY_WAS_RUNNING = true;
            TUESDAY_WAS_RUNNING = false;
            Intent intent = new Intent(FirstActivity.this, MainActivity.class);
            intent.putExtra(BUTTON_KEY1, BUTTON_VALUE);

            startActivity(intent);
        }    });
        buttonTue.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TUESDAY_WAS_RUNNING = true;
            MONDAY_WAS_RUNNING = false;
            Intent i = new Intent(FirstActivity.this, MainActivity.class);
            i.putExtra(BUTTON_KEY2, BUTTON_VALUE2);
            startActivity(i);
        }    });}}

MyAdapter: -

@Override
    public void onBindViewHolder(MyHolder holder, final int position) {

        holder.textViewModel.setText(training.get(position).getExercise());

        holder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(View v) {
                if (FirstActivity.MONDAY_WAS_RUNNING)

                {Intent intent = new Intent(c, DetailActivity.class);

                    intent.putExtra(KEY_MON, VALUE_MON);
                    intent.putExtra("id", training.get(position).getId());
                    intent.putExtra("exercise", training.get(position).getExercise());
                    intent.putExtra("weight", training.get(position).getWeight());
                    intent.putExtra("repeat", training.get(position).getRepeat());
                    intent.putExtra("series", training.get(position).getSeries());

                    c.startActivity(intent);

                } else if (FirstActivity.TUESDAY_WAS_RUNNING)

                {Intent intent2 = new Intent(c, DetailActivity.class);

                    intent2.putExtra(KEY_TUE, VALUE_TUE);
                    intent2.putExtra("id", training.get(position).getId());
                    intent2.putExtra("exercise", training.get(position).getExercise());
                    intent2.putExtra("weight", training.get(position).getWeight());
                    intent2.putExtra("repeat", training.get(position).getRepeat());
                    intent2.putExtra("series", training.get(position).getSeries());

                    c.startActivity(intent2);
                }}});}

但我可能会选择: -

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
           holder.textViewModel.setText(training.get(position).getExercise());
        holder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(View v) {
                Intent intent = new Intent(c, DetailActivity.class);
                if (FirstActivity.MONDAY_WAS_RUNNING) {
                    intent.putExtra(KEY_MON, VALUE_MON);
                } else {
                    intent.putExtra(KEY_TUE, VALUE_TUE);
                }                       
                intent.putExtra("id", training.get(position).getId());
                intent.putExtra("exercise", training.get(position).getExercise());
                intent.putExtra("weight", training.get(position).getWeight());
                intent.putExtra("repeat", training.get(position).getRepeat());
                intent.putExtra("series", training.get(position).getSeries());
                c.startActivity(intent);
        });
    }

注意!上面的代码尚未检查,而是原则上的代码