Java 循环中的GetLayoutFlater().inflate()始终返回第一个视图
Java 循环中的GetLayoutFlater().inflate()始终返回第一个视图,java,android,android-studio,layout-inflater,Java,Android,Android Studio,Layout Inflater,tics有3项。以下代码创建3个票据项目,但始终设置第一张票据的TextViews文本 public void onSuccess(int i, Header[] headers, byte[] bytes) { progress.dismiss(); String response = new String(bytes); try{
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){}
}
t
不应该是膨胀的视图吗
不应该是夸大的观点吗
否,您正在使用的版本将膨胀视图添加到父视图,并返回父视图本身。你可以用
View inflate (XmlPullParser parser,
ViewGroup root,
boolean attachToRoot)
提供false作为第三个参数。这样,android将返回膨胀视图(父视图仅用于布局参数)。您必须手动将其添加到父级
不应该是夸大的观点吗
否,您正在使用的版本将膨胀视图添加到父视图,并返回父视图本身。你可以用
View inflate (XmlPullParser parser,
ViewGroup root,
boolean attachToRoot)
提供false作为第三个参数。这样,android将返回膨胀视图(父视图仅用于布局参数)。您必须手动将其添加到父视图。此时视图不会添加到父视图。我应该使用p.addView(t)代码>对吗?视图不会添加到父视图中。我应该使用p.addView(t)代码>对吗?