1

我是 android/java 的新手,并试图通过输入教程中的代码来学习。我想看看我是否可以制作一个常见问题解答 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>

常见问题解答Activity.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);

            }
        });
    }
}

常见问题解答活动.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);


    }
}
4

2 回答 2

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>  

此外,由于视图不可见,您可以删除现在不相关的布局属性。我相信你所需要的只是 id 和能见度。

<TextView
        android:id="@+id/answer"
        android:visibility="gone">
</TextView> 
于 2012-09-24T19:35:47.977 回答
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

于 2012-09-24T19:20:57.380 回答