2013-01-23 40 views
2

我是新來的android。我正在測驗應用程序上做一個項目。我在顯示問題和答案時遇到問題。如何從數組列表中獲取測驗答案?

我的問題是我已經將問題和答案存儲在數組中。問題和答案是顯示,但不是爲了順序。

我從數據庫中得到了問題和答案。他們是按順序進來的,但是在我的數組列表中它不是按順序排列的。問題和答案不匹配。

如何通過匹配正確的順序以正確的順序顯示問題。

請幫助我,並提前致謝。

code

 protected void onPostExecute(String file_url) { 
     pDialog.dismiss(); 
     ques1=new ArrayList<String>(new HashSet<String>(ques1)); 

     System.out.println(" Question array:"+ques1); 
     String[] quesArr = new String [ques1.size()]; 
     quesArr = ques1.toArray(quesArr); 
     for(String s: quesArr){ 
      Collections.sort(ques1, new Comparator<String>() { 

       @Override 
       public int compare(String lhs, String rhs) { 
        // TODO Auto-generated method stub 
        return 0; 
       } 

      }); 
     System.out.println("All Array Questions:"+s); 
      final TextView txtque = (TextView) findViewById(R.id.que_txt); 
      final String text=ques1.get(j).toString(); 
      System.out.println("Array:"+text); 
      txtque.setText(text); 
     } 

     try { 
      int i = 0; 
     // answ1=new ArrayList<String>(new HashSet<String>(answ1)); 
     JSONObject c = groups.getJSONObject(i); 

     String answer = c.getString(TAG_ANSW); 

     System.out.println("Checking ::"+answer); 
     answ=answer; 
     HashMap<String, String> map1 = new HashMap<String, String>(); 
     answ1.add(answer); 
      System.out.println(" Answer array:"+answ1); 
      String[] answArr = new String [answ1.size()]; 
      answArr = answ1.toArray(answArr); 
      for(String A: answArr){ 
       Collections.sort(answ1, new Comparator<String>() { 
        @Override 
        public int compare(String s1, String s2) { 
         return s1.compareToIgnoreCase(s2); 
        } 
       }); 
      System.out.println("All Array Answers:"+A); 
      } 

      final TextView txtRadio = (TextView) findViewById(R.id.rdtxt); 
      btn_practicerg =(RadioGroup) findViewById(R.id.rdgroup); 
      btn_practice1 = (RadioButton) findViewById(R.id.RB1); 
      btn_practice1.setText(String.valueOf(answArr[0])); 
      btn_practice2 = (RadioButton) findViewById(R.id.RB2); 
      btn_practice2.setText(String.valueOf(answArr[1])); 
      btn_practice3 = (RadioButton) findViewById(R.id.RB3); 
      btn_practice3.setText(String.valueOf(answArr[2])); 
      btn_practice4 = (RadioButton) findViewById(R.id.RB4); 
      btn_practice4.setText(String.valueOf(answArr[3])); 

      btn_practicerg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 

       @Override 
       public void onCheckedChanged(RadioGroup group, int checkedId) 
       { 
        RadioButton checkedRadioButton = (RadioButton) findViewById(checkedId); 

       } 
      }); 
      } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
      btn_practicerg.clearCheck(); 

      Button nextBtn = (Button) findViewById(R.id.nxt_btn); 
      nextBtn.setOnClickListener(new Button.OnClickListener(){ 
      public void onClick(View v){ 
       j++; 
       TextView txtque = (TextView) findViewById(R.id.que_txt); 
        txtque.setText(ques1.get(j).toString());     
       k++; 
       btn_practice1.setText(answ1.get(k).toString()); 
       k++; 
       btn_practice2.setText(answ1.get(k).toString()); 
       k++; 
       btn_practice3.setText(answ1.get(k).toString()); 
       k++; 
       btn_practice4.setText(answ1.get(k).toString()); 

      } 
      }); 
     } 

回答

2

你存儲在一個HashSet,這是不保留順序的問題。此外,您比較總是返回0

+0

Im new to android。我應該做些什麼 –

+0

@ God'sGrace你對Java也是新手 – Blackbelt

+0

@blackbelt是的。我應該做些什麼改變? –

0

有它的另一個更容易的方法,我根本就不會choosen HashSet的,我覺得他們很複雜

創建一個包裝類 類QA { 字符串疑問句; String and; }

現在創建此類類型ArrayList的ArrayList並以這種方式填充值,您可以將所有數據放在同一個位置,並且可以減輕您的任務,因爲您是新的。

+0

我應該如何新這個..爲什麼我得到這個問題 –