2016-08-22 1 views
1

ticsには3つの項目があります。次のコードは3つのチケット項目を作成しますが、常に最初のチケットのTextViewのテキストを設定します。次のコードでgetLayoutInflater()ループ内のinflate()は常に最初のビューを返します

public void onSuccess(int i, Header[] headers, byte[] bytes) { 
        progress.dismiss(); 
        String response = new String(bytes); 
        try{ 
         JSONObject obj = new JSONObject(response); 
         JSONArray tics = obj.getJSONArray("tickets"); 
         LinearLayout p = (LinearLayout)findViewById(R.id.tickets); 

         for(int j = 0;j<tics.length();j++){ 
         LinearLayout t =(LinearLayout) getLayoutInflater().inflate(R.layout.ticket_row, p); 
          TextView topic = (TextView)t.findViewWithTag("topic"); 
          TextView section = (TextView)t.findViewWithTag("section"); 
          TextView datetime = (TextView)t.findViewWithTag("datetime"); 
          JSONObject item = tics.getJSONObject(j); 
          Toast.makeText(getApplicationContext(),item.getString("Caption") ,Toast.LENGTH_LONG).show(); 
          topic.setText(item.getString("Caption")); 
          datetime.setText(item.getString("DateString")); 
          section.setText(item.getString("Section")); 
         } 
        }catch (Exception e){} 
       } 

LinearLayout t =(LinearLayout) getLayoutInflater().inflate(R.layout.ticket_row, p); 

tは膨張しViewではないでしょうか?

答えて

5

膨らんだビューではい​​けませんか?

いいえ、使用しているバージョンでは、インフレされたビューが親に追加され、親自体が返されます。第3パラメータとしてfalseを指定すると、

View inflate (XmlPullParser parser, 
       ViewGroup root, 
       boolean attachToRoot) 

を使用することができます。このようにしてアンドロイドは膨張したビューを返します(親はレイアウトparmasのためだけに使用されます)。あなたはそれを親に手動で追加する必要があります。

+0

ビューは親に追加されていません。私は 'p.addView(t);'を使うべきですか? –

+1

はい、まさに... – Blackbelt

+1

素晴らしい!どうもありがとう。 –

関連する問題