如何从XML解析中正确显示内容?

时间:2012-09-24 18:54:55

标签: java android xml-parsing

我是android / java的新手,并试图通过输入tutorials中的代码来学习。我正试图看看我是否可以制作常见问题解答android模块。 有人可以告诉我如何仅显示问题列表,当您点击问题时,在下一个活动中对应问题和答案显示? 它目前显示两个活动的问题和答案。

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

非常感谢!

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 个答案:

答案 0 :(得分:2)

在我看来,之前的回答可能会给您一个问题,因为您从TextView中提取值以放入下一个Activity的Intent。

相反,我建议您在“questions.xml”中更改答案TextView的可见性,如下所示:

<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> 

答案 1 :(得分: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