Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to display content correctly from XML Parsing?
    primarykey
    data
    text
    <p>I am new to android/java and trying to learn by typing code from <a href="http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/" rel="nofollow">tutorials</a>. I'm trying to see if I can make a FAQs android module. Could someone show me how to display only the list of questions and when you click on a question, correspond question and answer display on the next activity? It's currently displaying both question and answer for the both activities. </p> <p>Hope my question makes sense. please kindly ask and I will explain as best as I can.</p> <p>Thank you very much! </p> <p>faq_list.xml</p> <p> </p> <pre><code>&lt;item&gt; &lt;id&gt;1&lt;/id&gt; &lt;question&gt;Whats my phone number?&lt;/question&gt; &lt;answer&gt;my phone number here&lt;/answer&gt; &lt;/item&gt; &lt;item&gt; &lt;id&gt;2&lt;/id&gt; &lt;question&gt;Whats my fax number?&lt;/question&gt; &lt;answer&gt;my fax number here&lt;/answer&gt; &lt;/item&gt; &lt;item&gt; &lt;id&gt;3&lt;/id&gt; &lt;question&gt;What is my mailing address?&lt;/question&gt; &lt;answer&gt;my address here&lt;/answer&gt; &lt;/item&gt; </code></pre> <p></p> <p>faqs_questions.xml</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"&gt; &lt;!-- qnestion --&gt; &lt;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" /&gt; &lt;!-- answer --&gt; &lt;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"&gt; &lt;/TextView&gt; &lt;/LinearLayout&gt; </code></pre> <p>faqs_answers.xml</p> <pre><code>&lt;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"&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>FaqsQuestionsActivity.java</p> <pre><code> 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&lt;HashMap&lt;String, String&gt;&gt; menuItems = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); 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 &lt; nl.getLength(); i++) { HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); 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&lt;?&gt; 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); } }); } } </code></pre> <p>FaqsAnswersActivity.java</p> <pre><code>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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload