2014-04-30 6 views
2

내 앱에서 다음 활동 (곱하기 퀴즈 게임)에서 사용자가 제공 한 질문 및 답변 목록을 표시했습니다. 현재 답변이 잘못 표시되면 빨간색으로 표시되고 올바른 경우 녹색으로 표시됩니다. 목록의 하위 항목은 항상 정답 (녹색)입니다. 답변이 정확하지 않은 경우에만 하위 항목을 표시하려고합니다. 나는. 정답을 두 번 표시 할 필요가 없습니다. 하지만 그렇게하는 방법을 모르겠습니다. 활동Android ListView : 항목에 따라 하위 항목 제거

이미지 (사용자가 이미 정답을 주신 경우에도 하위 항목에 표시되는 방법을 정답 주) : 활동의

enter image description here

코드 :의

public class RandomTestResults extends Activity { 

    // Set up variables 
    TextView scoreText; 
    String phrase; 

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

     // List view to hold the test results 
     ListView itemList = (ListView) findViewById(R.id.lvRandomTestresults); 

     // getting data from the previous activity via intents 
     int[] results = getIntent().getIntArrayExtra("results"); 
     String[] questions = getIntent().getStringArrayExtra("Questions"); 
     int[] correctAnswer = getIntent().getIntArrayExtra("CorrectAnswer"); 
     int score = getIntent().getIntExtra("score", 0); 

     // if else statements to determine what phrase to present to user 
     if (score <= 4) { 
      phrase = "Please try again! "; 
     } else if (score <= 6) { 
      phrase = "Good effort! "; 
     } else if (score <= 9) { 
      phrase = "Well done! "; 
     } else if (score == 10) { 
      phrase = "Perfect! "; 
     } 

     // Set text to tell user what they scored in test 
     scoreText = (TextView) findViewById(R.id.tvRandomTestresults); 
     scoreText.setText(phrase + "You got " + score + " correct out of 10!"); 

     // ArrayList containing Hashmap 
     ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); 

     // loop to give list view (10 items and sub items) 
     for (int i = 1; i <= 10; i++) { 

      // set userAnswer equal to correct place in results array 
      int userAnswer = results[i - 1]; 
      // Setting expected answer to correct place in correctAnswer array 
      int expectedAnswer = correctAnswer[i - 1]; 
      // Set string to present to user 
      String userString = questions[i - 1] + " " + userAnswer; // mybe 
                     // change 
      // correct answer 
      String expectedString = "" + expectedAnswer; 
      // HashMap containing 2 strings 
      HashMap<String, String> map = new HashMap<String, String>(); 
      // add strings to HashMap 
      map.put("user", userString); 
      map.put("expected", expectedString); 
      // add HashMap to list 
      list.add(map); 
     } 

     // Instantiate custom array adapter 
     MyArrayAdapter adapter = new MyArrayAdapter(
       this.getApplicationContext(), R.layout.list_row, list, 
       questions, results); 

     // Set custom adapter to your ListView. 
     itemList.setAdapter(adapter); 

    } 

    /** 
    * Method that ensures user is returned 
    * to main menu when they press back button 
    */ 
    @Override 
    public void onBackPressed() { 
     Intent t = new Intent(this, Menu.class); 
     startActivity(t); 
    } 

} 

코드 활동과 관련된 어댑터 :

public class MyArrayAdapter extends 
    ArrayAdapter<ArrayList<HashMap<String, String>>> { 
    Context mContext; 
    ArrayList<HashMap<String, String>> mQuestionArrayList; 
    int mLayoutResourceId; 
    String[] mQuestionsArray; 
    int[] mUsersAnswers; 

    /** 
    * Constructor with arguments 
    * @param context 
    * @param layoutResourceId 
    * @param questionsArrayList 
    * @param questionsArray 
    * @param usersAnswers 
    */ 
    public MyArrayAdapter(Context context, int layoutResourceId, 
      ArrayList<HashMap<String, String>> questionsArrayList, 
      String[] questionsArray, int[] usersAnswers) { 
     super(context, layoutResourceId); 
     mContext = context; 
     this.mQuestionArrayList = questionsArrayList; 
     this.mLayoutResourceId = layoutResourceId; 
     this.mQuestionsArray = questionsArray; 
     this.mUsersAnswers = usersAnswers; 
    } 

    /** 
    * Method that returns the size 
    * of the ArrayList 
    */ 
    @Override 
    public int getCount() { 
     return mQuestionArrayList.size(); 
    } 

    /** 
    * 
    * Method that will get the view to display to user 
    */ 
    @Override 
    public View getView(int position, View row, ViewGroup parent) { 
     HashMap<String, String> question = mQuestionArrayList.get(position); 
     //set layout inflater equal to context 
     LayoutInflater inflater = LayoutInflater.from(mContext); 

     // Initialize the row layout by inflating the xml file list_row. 
     row = inflater.inflate(this.mLayoutResourceId, parent, false); 

     // Initialize TextViews defined in the list_row layout. 
     TextView questionTxtView = (TextView) row.findViewById(R.id.question); 
     TextView answerTxtView = (TextView) row.findViewById(R.id.answer); 
     TextView correctAnswerTxtView = (TextView) row 
       .findViewById(R.id.correct); 

     // Set text for each TextView 
     questionTxtView.setText(mQuestionsArray[position]); 
     answerTxtView.setText(String.valueOf(mUsersAnswers[position])); 
     correctAnswerTxtView.setText(question.get("expected").toString()); 

     // Setting colour of the user answer dependent on if its correct 
     if (mUsersAnswers[position] != Integer.parseInt(question 
       .get("expected").toString())) 
      answerTxtView.setTextColor(Color.RED); 
     else 
      answerTxtView.setTextColor(Color.GREEN); 

     return row; 
    } 
} 

답변

1

이 같은 사용자의 대답은 무엇인가에 따라 correctAnswerTxtView 가시성을 변경할 수있을 : 도움이

// Setting colour of the user answer dependent on if its correct 
    if (mUsersAnswers[position] != Integer.parseInt(question 
      .get("expected").toString())) { 
     answerTxtView.setTextColor(Color.RED); 
     correctAnswerTxtView.setVisibility(View.GONE); 
    } 
    else { 
     answerTxtView.setTextColor(Color.GREEN); 
     correctAnswerTxtView.setVisibility(View.VISIBLE); 
    } 

희망.

+0

완벽한, 감사합니다! –

관련 문제