如何通过Android中的适配器从其他类中获取数据

时间:2017-12-21 14:46:14

标签: android android-studio abstract-class

我试图在不使用数据库的情况下制作测验应用程序(要求)。每个问题都有4个选项。

我上课了。现在,在我想要显示数据的活动中,我无法获取从QuestionModelClass获取数据的方法。

我做了2D Array,但得到它会变得更复杂。有没有办法绑定3个类(QuestionModelClass,Adapter类和Activity类)?

public class QuestionsModelClass {

private String sQuestion;
private String sRightAnswer;
private List<String> sOptions;

QuestionsModelClass(){
    sQuestion = null;
    sRightAnswer = null;
    sOptions = null;
}

public QuestionsModelClass(String sQuestion, String sRightAnswer, List<String> sOptions) {
    this.sQuestion = sQuestion;
    this.sRightAnswer = sRightAnswer;
    this.sOptions = sOptions;
}

public String getsQuestion() {
    return sQuestion;
}

public void setsQuestion(String sQuestion) {

    this.sQuestion = sQuestion;
}

public String getsRightAnswer() {

    return sRightAnswer;
}

public void setsRightAnswer(String sRightAnswer) {
    this.sRightAnswer = sRightAnswer;
}

public List<String> getsOptions() {

    return sOptions;
}

public void setsOptions(List<String> sOptions) {

    this.sOptions = sOptions;
}
}

我的适配器类

public class QuizAdapter extends BaseAdapter {
private Context context;
private List<QuestionsModelClass> questionClassList;

private String[][] options;

private LayoutInflater inflater;

private QuizAdapter(Context c, List<QuestionsModelClass> l){
    this.context= c;
    this.questionClassList = l;
    inflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return  questionClassList.size();

}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = inflater.inflate(R.layout.questionpattern, parent,false);
    QuestionsModelClass questions = questionClassList.get(position);

    TextView quesText= convertView.findViewById(R.id.questionTextView);
    RadioButton radioButtonA = convertView.findViewById(R.id.optionA);
    RadioButton radioButtonB = convertView.findViewById(R.id.optionB);
    RadioButton radioButtonC = convertView.findViewById(R.id.optionC);
    RadioButton radioButtonD = convertView.findViewById(R.id.optionD);

    return convertView;
}

这是我试图实现所有函数的Activity类

public class QuizActivity extends Activity {

final Context context= this;
private List<QuestionsModelClass> classObject;
Button okayButton;

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


   String[] question= new String[]{"Q1. ABDE", "Q2. ADDASD"};
    String[][] op;
    String[] right = new String[]{"abc","def"};
    classObject = new ArrayList<>();
    op= new String[][]{
            {"a1", "2", "3", "4"},
            {"b1","b2","b3","b4"}};

    final Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.customdialoguebox);
    dialog.show();


    okayButton = (Button) dialog.findViewById(R.id.okayButton);
    okayButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(QuizActivity.this,"Good Luck!", Toast.LENGTH_SHORT).show();
                dialog.cancel();
        }
    });


}

0 个答案:

没有答案