1

私はAndroid/Javaが初めてで、チュートリアルからコードを入力して学習しようとしています。FAQs 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);


    }
}
4

2 に答える 2

2

TextView から値を取得して次のアクティビティのインテントに入れるため、前の回答で問題が発生する可能性があるように思えます。

代わりに、「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から回答テキストビューを削除します

于 2012-09-24T19:20:57.380 に答える