2016-08-22 3 views
1

tics에는 3 개의 항목이 있습니다. 다음 코드는 3 개의 티켓 항목을 생성하지만 항상 첫 번째 티켓 TextView의 텍스트를 설정합니다.getLayoutInflater(). 루프의 inflate()는 항상 첫 번째보기를 반환합니다.

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

t가 팽창 View해야하지 : 다음 코드에서

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){} 
       } 

?

답변

5

비정상적인보기가 아니어야합니까?

아니요, 사용중인 버전이 비정상보기를 부모에 추가하고 부모 자체를 반환합니다. 당신은 사용할 수 있습니다

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

거짓 세 번째 매개 변수를 제공합니다. 이 방법으로 안드로이드는 팽창 된 뷰를 반환 할 것이다 (부모는 레이아웃 parmas를 위해서만 사용될 것이다). 부모에게 수동으로 추가해야합니다.

+0

보기가 부모에게 추가되지 않습니다. 나는'p.addView (t); '를 사용해야한다. 맞지? –

+1

예, 정확히 ... – Blackbelt

+1

좋아요! 고마워. –