2012-09-24 42 views
1

我是新来的android/java和尝试通过从tutorials键入代码学习。我试图看看我是否可以制作一个常见问题的Android模块。 有人可以告诉我如何只显示问题列表,当你点击一个问题时,对应的问题和答案显示在下一个活动上? 它目前显示两个活动的问题和答案。如何从XML解析正确显示内容?

希望我的问题有道理。敬请问,我会尽我所能解释。

非常感谢!

faq_list.xml

<item>  
    <id>1</id> 
    <question>Whats my phone number?</question> 
    <answer>my phone number here</answer>   
</item> 

<item>  
    <id>2</id> 
    <question>Whats my fax number?</question> 
    <answer>my fax number here</answer>   
</item> 

<item>  
    <id>3</id> 
    <question>What is my mailing address?</question> 
    <answer>my address here</answer>   
</item> 

faqs_questions.xml

<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <!-- qnestion --> 
     <TextView 
      android:id="@+id/question" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textColor="#000" 
      android:textSize="14sp" 
      android:layout_marginBottom="10dip" 
      android:layout_marginTop="10dip" 
      android:layout_weight="1" 
      android:paddingLeft="5sp" /> 
     <!-- answer --> 
     <TextView 
      android:id="@+id/answer" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textColor="#000" 
      android:textSize="14sp" 
      android:layout_marginBottom="10dip" 
      android:layout_marginTop="10dip" 
      android:layout_weight="2" 
      android:paddingLeft="5sp"> 
     </TextView>  

     </LinearLayout> 

faqs_answers.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:background="#fff" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView android:id="@+id/question_label" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textSize="14dip"  
      android:textStyle="bold"  
      android:textColor="#000"    
      android:paddingLeft="5sp" 
      /> 

    <TextView android:id="@+id/answer_label" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textSize="14dip"  
      android:textColor="#000"    
      android:paddingLeft="5sp" 
      /> 

</LinearLayout> 

FaqsQuestionsActivity.java

public class FaqsQuestionsActivity extends ListActivity { 


    static final String URL = "http://myweb.com/faq_list.xml"; 
    // XML node keys 
    static final String KEY_ITEM = "item"; // parent node 
    static final String KEY_ID = "id"; 
    static final String KEY_QUESTION = "question"; 
    static final String KEY_ANSWER = "answer"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.faqs_questions); 

     ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>(); 

     XMLParser parser = new XMLParser(); 
     String xml = parser.getXmlFromUrl(URL); // getting XML 
     Document doc = parser.getDomElement(xml); // getting DOM element 

     NodeList nl = doc.getElementsByTagName(KEY_ITEM); 

     for (int i = 0; i < nl.getLength(); i++) { 

      HashMap<String, String> map = new HashMap<String, String>(); 
      Element e = (Element) nl.item(i); 

      map.put(KEY_ID, parser.getValue(e, KEY_ID)); 
      map.put(KEY_QUESTION, parser.getValue(e, KEY_QUESTION)); 
      map.put(KEY_ANSWER, parser.getValue(e, KEY_ANSWER)); 

      menuItems.add(map); 
     } 

     ListAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.faqs_question_list, 
     new String[] { KEY_QUESTION, KEY_ANSWER }, new int[] {R.id.question, R.id.answer}); 


     setListAdapter(adapter); 

     ListView lv = getListView(); 

     lv.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 


       String question = ((TextView) view.findViewById(R.id.question)).getText().toString(); 
       String answer = ((TextView) view.findViewById(R.id.answer)).getText().toString(); 


       Intent in = new Intent(getApplicationContext(), FaqsAnswersActivity.class); 

       in.putExtra(KEY_QUESTION, question); 
       in.putExtra(KEY_ANSWER, answer); 
       startActivity(in); 

      } 
     }); 
    } 
} 

FaqsAnswersActivity.java

public class FaqsAnswersActivity extends Activity { 

    // XML node keys 

    static final String KEY_QUESTION = "question"; 
    static final String KEY_ANSWER = "answer"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.faqs_answers); 

     // getting intent data 
     Intent in = getIntent(); 

     // Get XML values from previous intent 
     String question = in.getStringExtra(KEY_QUESTION); 
     String answer = in.getStringExtra(KEY_ANSWER); 

     // Displaying all values on the screen   
     TextView lblQuestion = (TextView) findViewById(R.id.question_label); 
     TextView lblAnswer = (TextView) findViewById(R.id.answer_label);   

     lblQuestion.setText(question); 
     lblAnswer.setText(answer); 


    } 
} 

回答

2

它看起来像我以前的答案可能会给你一个问题,因为你是从你的TextView的价值拉放入下一个活动的意图。

相反,我建议你改变你的答案的TextView的知名度“questions.xml”是这样的:

<TextView 
     android:id="@+id/answer" 
     android:visibility="gone" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#000" 
     android:textSize="14sp" 
     android:layout_marginBottom="10dip" 
     android:layout_marginTop="10dip" 
     android:layout_weight="2" 
     android:paddingLeft="5sp"> 
</TextView> 

此外,由于观点是不可见的,你可能会删除它是布局属性现在无关紧要。我相信你所需要的只是身份证和知名度。

<TextView 
     android:id="@+id/answer" 
     android:visibility="gone"> 
</TextView> 
+0

是的,你是对的,没有注意到 – triggs

+0

有趣的是,它的作品。我期待在我的课堂文件中看到一些调整,但这对我来说很好,谢谢! – etrademom

0

简单的列表适配器建立基于XML的文件列表,只需删除提及从列表视图的答案,它应该工作

ListAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.faqs_question_list, 
    new String[] { KEY_QUESTION }, new int[] {R.id.question}); 

并从faq_questions.xml中删除答案textview

+0

怎么样的ListView LV = getListView();我是否应该删除字符串答案? – etrademom

+0

,因为我没有得到FaqsAnswerActivity类的答案 – etrademom

+0

没有注意到Reynard指出的问题,只是将可见性设置为消失,他建议将工作,并且将是最快的解决方案。如果你想尝试一种不同的方式使hashmap成为一个类变量,那么从itemclicklistener – triggs