如何在单键hashmap中获取两个字符串值并将此键转换为数组

时间:2013-01-30 05:20:54

标签: android arraylist hashmap

我正面临着hashmap和arraylist的问题....我的问题是......

我有像这样的HashMap ..

public class QuestionData {
    private String questionID;
    private String question;
    private String answer;
    public String getQuestionID() {
        return questionID;
    }
    public void setQuestionID(String questionID) {
        this.questionID = questionID;
    }
    public String getQuestion() {
        return question;
    }
    public void setQuestion(String question) {
        this.question = question;
    }
    public String getAnswer() {
        return answer;
    }
    public void setAnswer(String answer) {
        this.answer = answer;
    }
}
while (is another question to add) {
  QuestionData questionData = new QuestionData();
  questionData.setQuestionID(questionID);
  questionData.setQuestion(question);
  questionData.setAnswer(answer);
  ArrayList < QuestionData > questionDatas = new ArrayList < QuestionData > ();
  questionDatas.add(questionData);
}
int counter = 0;
Button nextQuestion = (Button) findViewById(R.id.your_button);
nextQuestion.setOnTouchListener(new OnTouchListener() {
   @Override
   public boolean onTouch(View v, MotionEvent event) {
     if (counter < questionDatas.size()) {
         QuestionData question = questionDatas.get(counter);
         showNextQuestion(question);
         counter++;
     }
     return false;
  }
 });
  showNextQuestion(QuestionData data) {
  data.getQuestionID();
  data.getQuestion();
  data getAnswer();
}

但我需要在hashmap的单个键中使用两个字符串questionid和问题,然后我想将此单个键转换为arraylist ...

之后我想在点击下一个按钮时增加arraylist ...根据questionid显示下一个问题......

怎么做?

2 个答案:

答案 0 :(得分:1)

仅为您的问题创建数据类

public class QuestionData {

    private String questionID;
    private String question;
    private String answer;
    public String getQuestionID() {
        return questionID;
    }
    public void setQuestionID(String questionID) {
        this.questionID = questionID;
    }
    public String getQuestion() {
        return question;
    }
    public void setQuestion(String question) {
        this.question = question;
    }
    public String getAnswer() {
        return answer;
    }
    public void setAnswer(String answer) {
        this.answer = answer;
    }
}

并使用具有此数据结构的哈希映射。

    QuestionData questionData = new QuestionData();
        questionData.setQuestionID(questionID);
        questionData.setQuestion(question);
        questionData.setAnswer(answer);
//        HashMap<String, QuestionData> map = new HashMap<String, QuestionData>();
        ArrayList<QuestionData> questionDatas = new ArrayList<QuestionData>();
        questionDatas.add(questionData);

答案 1 :(得分:0)

我更喜欢@ ViVart的答案。要添加到他的,您可以实现您的按钮:

ArrayList<QuestionData> questionDatas = new ArrayList<QuestionData> ();
while (is another question to add) {
    if (firstQuestionParsed) {
        TextView questionID_tv = (TextView) findViewById(R.id.your_textview);
        TextView question_tv = (TextView) findViewById(R.id.your_textview);
        TextView answer_tv = (TextView) findViewById(R.id.your_textview);
        questionID_tv.setText(questionID);
        question_tv.setText(question);
        answer_tv.setText(answer);
    }
    QuestionData questionData = new QuestionData();
    questionData.setQuestionID(questionID);
    questionData.setQuestion(question);
    questionData.setAnswer(answer);
    questionDatas.add(questionData);
}
 int counter = 0;
 Button nextQuestion = (Button) findViewById(R.id.your_button);
 nextQuestion.setOnTouchListener(new OnTouchListener() {
     @Override
     public boolean onTouch(View v, MotionEvent event) {
         if (counter < questionDatas.size()) {
             QuestionData question = questionDatas.get(counter);
             showNextQuestion(question);
             counter++;
         }
         return false;
     }
 });

然后你只需要实现

showNextQuestion(QuestionData data) {
    data.getQuestionID();
    data.getQuestion();
    data getAnswer();
}