代码之家  ›  专栏  ›  技术社区  ›  Ashkan Mobayen Khiabani

获取布局充气器()。循环中的inflate()始终返回第一个视图

  •  1
  • Ashkan Mobayen Khiabani  · 技术社区  · 9 年前

    这个 tics 有3个项目。以下代码创建了3个票据项,但始终设置 TextView 第一张票的s。

    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 ?

    1 回复  |  直到 9 年前
        1
  •  5
  •   Blackbelt    9 年前

    不应该是膨胀的视图吗?

    不,您使用的版本会将膨胀视图添加到父视图,并返回父视图本身。你可以用

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

    提供false作为第三个参数。这样,android将返回膨胀视图(父视图仅用于布局参数)。您必须手动将其添加到父对象中。