如何将int变量从列表视图适配器传递给活动

时间:2018-07-28 08:06:36

标签: listview android-arrayadapter

/ 每个listView项在单击时得分均增加1。我应该在适配器和活动中添加什么代码,以将该变量(mscore)从适配器传递到活动 /

public QuestionList(Activity context, List<completeQuestion> questionList) {
    super(context, R.layout.list_layout, questionList);
    this.context = context;
    this.questionList = questionList;
}



@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    final View listViewItem = inflater.inflate(R.layout.list_layout, null, true);

    TextView textview = (TextView) listViewItem.findViewById(R.id.textView);
    Button ioanswer1 = (Button) listViewItem.findViewById(R.id.radio_answer1);


     completequestion1 = questionList.get(position);



    textview.setText(completequestion1.getQuestionI());

    ioanswer1.setText(completequestion1.getQuestionAnswer1());

    mAnswer = completequestion1.getQuestionCorrectAnswer();

   ioanswer1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ...
            if (mAnswer.equals completequestion1.getQuestionAnswer1()){
                mScore = mScore + 1;

            }
        }
    });


    return listViewItem;
}

//谢谢

1 个答案:

答案 0 :(得分:0)

您需要向模型类中添加一些属性。

首先将Score属性或字段添加到您的商品类(似乎您正在使用completeQuestion)。

然后向您的适配器添加getItem方法。

然后您可以致电adapter.getItem(i).getScore()