onClickListener中的Android调用方法,带有外部参数

时间:2015-10-23 22:16:41

标签: java android arraylist onclicklistener

所以,我正在为我的毕业最终项目开发一个应用程序,在我的应用程序中我有一个带有imageView和textView的布局,设置这些元素的内容我收到一个ArrayList作为参数并使用setText( ),它的确有效。

但是我的Arraylist有多个元素,我的大问题是我想在单击按钮时更改imageView和textView的内容。 我的想法是,当我单击按钮时,它将增加值并更改arrayList中下一个元素的对象内容。

我在onClickListeners中遇到了全局变量的问题,但我用辅助方法解决了这些问题,但这次不起作用,因为我需要在按钮事件中增加迭代的值。

你们能帮助我提出建议或告诉我自己做错了什么吗?

我希望你理解我的问题,如果我不能很好地解释我,我很抱歉,如果我发布的内容不够,请随时提出任何问题并请求更多的代码块。

先感谢你们所有人,我们赐予你们所有帮助!

从我的数据库获取数据并将其存储到arraylist

private ArrayList<Exercises> getExercisesSelectedPlan() {
    ArrayList<ProgramExercises> arrayList_programExercises = new ArrayList<();
    ArrayList<Exercises> arrayList_exercises = new ArrayList<>();
    Button btnNext = (Button) findViewById(R.id.btnFinishAquecimento);

    //with the id of the selected plan, select all the exercises from the plan and load them into the layout
    int idProgram = getIdPlan();

    DatabaseHelper databaseHelper = new DatabaseHelper(this);
    SQLiteDatabase db = databaseHelper.getWritableDatabase();
    String queryProgramExercises = "SELECT * FROM PROGRAM_EXERCISES WHERE PROGRAM_ID_PROGRAM = " + "'" + idProgram + "'" + " ORDER BY ORDER_EX ASC";
    Cursor cursorProgramExercises = db.rawQuery(queryProgramExercises, null);
    int numRows = cursorProgramExercises.getCount();
    if (numRows > 0) {
        cursorProgramExercises.moveToFirst();
        while (numRows > 0) {
            for (int i = 0; i < cursorProgramExercises.getCount(); i++) {
                ProgramExercises nProgramExercises = new ProgramExercises();
                nProgramExercises.setIdProgramExercises(cursorProgramExercises.getInt(0));
                nProgramExercises.setOrder(cursorProgramExercises.getString(1));
                nProgramExercises.setRepetition(cursorProgramExercises.getInt(2));
                nProgramExercises.setIdExercises(cursorProgramExercises.getInt(3));
                arrayList_programExercises.add(nProgramExercises);
                numRows--;
            }
        }
    }

    //for each select result from program_exercises i have to select the exercise with the id correspondent to nProgramExercies.getidExercises
    for (int i = 0; i < arrayList_programExercises.size(); i++) {
        String queryExercises = "SELECT * FROM EXERCISES WHERE ID_EXERCISES = " + "'" + arrayList_programExercises.get(i).getIdExercises() + "'";
        Cursor cursorExercises = db.rawQuery(queryExercises, null);
        cursorExercises.moveToNext();

        if (cursorExercises.getCount() > 0) {
            Exercises exercises = new Exercises();
            for (int j = 0; j < cursorExercises.getCount(); j++) {
                exercises.setIdExercises(cursorExercises.getInt(0));
                exercises.setMedia(cursorExercises.getString(1));
                exercises.setDesignation(cursorExercises.getString(2));
                exercises.setIdExercisesPhase(cursorExercises.getInt(3));
                arrayList_exercises.add(exercises);
                cursorExercises.moveToNext();
            }
            cursorExercises.close();
        }
    }

    cursorProgramExercises.close();
    db.close();
    databaseHelper.close();
}

当我点击btnNext

时,我想更改textview的内容并增加迭代次数
 public void prepareExercise(final ArrayList<Exercises> exercises) {
    setContentView(R.layout.activity_execute_ex_warmup);

    TextView txtPrepare = (TextView) findViewById(R.id.tvAquecimento);
    ImageView imageExercise = (ImageView) findViewById(R.id.ivAquecimento);
    Button btnNext = (Button) findViewById(R.id.btnFinishAquecimento);
    txtPrepare.setText(exercises.get(1).getDesignation());
    imageExercise.setImageResource(R.drawable.imgTest);

    for (int i = 0; i < exercises.size(); i++) {
        if (exercises.get(i).getIdExercisesPhase() == 1) {
            txtPrepare.setText(exercises.get(i).getDesignation());
            System.out.println("------ TEST" + exercises.get(i).getDesignation());
        }
    }


    //Click Next Button
    btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //for (int i = 1; i < exercises.size(); i++) {
            //TextView txtPrepare = (TextView) findViewById(R.id.tvAquecimento);
            //txtPrepare.setText(exercises.get(finalI).getDesignation());
            //}
            //executeExercise(); //next Exercise
        }
    });
}

我如何调用上一个方法

        prepareExercise(getExercisesSelectedPlan());

1 个答案:

答案 0 :(得分:1)

希望我能正确理解。

如果您只想在每次单击按钮时更新视图,最简单的方法就是创建变量并存储数组列表的位置。

public int listIncrement = 0;
private TextView txtPrepare;
private ImageView imageExercise;

然后在prepareExercise()方法中

btnNext.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(listIncrement <= excersises.size()) {
            executeExercise(excersises, listIncrement); //next Exercise
            listIncrement++;
        }
    }
});

然后在executeExercise(ArrayList exercise,int position)//猜到的方法名称,可以是你喜欢的任何东西

txtPrepare.setText(exercises.get(position).getDesignation());
imageExercise.setImageResource(R.drawable.imgTest);