【发布时间】:2015-12-09 18:24:20
【问题描述】:
我在这里创建一个动态布局。我有一个父布局,我在其中添加了所有动态创建的视图。现在点击特定视图我需要位置。
根据该位置,我必须在 Android 中发送包含此视图的 JsonObject。在这里我无法获得确切的视图位置。
这是我的代码:
int len = result.length();
View addView;
if (!(len == 0)) {
finalResult.clear();
for (int i = 0; i <= len; i++) {
try {
finalResult.add(result.getJSONObject(i));
} catch (JSONException e) {
e.printStackTrace();
}
}
for (int i = 0; i < result.length(); i++) {
JSONObject obj = result.getJSONObject(i);
if (i == 0) {
addView = mInflater.inflate(R.layout.sightseeing_firstview, null);
image = (ImageView) addView.findViewById(R.id.imagesightseeing_first);
textview = (TextView) addView.findViewById(R.id.textView1);
llfirst = (LinearLayout) addView.findViewById(R.id.llfirstview);
llayout.addView(addView);
String category1 = finalResult.get(i).getString("CategoryName");
imageurl1 = finalResult.get(i).getString("ImageUrl");
((BaseActivity) SightseeingToursActivity.this).imageLoader.displayImage(imageurl1, image,
Common.optionsSmall);
textview.setText(category1);
} else if (i % 5 == 0) {
addView = mInflater.inflate(R.layout.sightseeing_firstview, null);
images = (ImageView) addView.findViewById(R.id.imagesightseeing_first);
textview = (TextView) addView.findViewById(R.id.textView1);
llfirst = (LinearLayout) addView.findViewById(R.id.llfirstview);
llayout.addView(addView);
String category1 = finalResult.get(i).getString("CategoryName");
imageurlss = finalResult.get(i).getString("ImageUrl");
((BaseActivity) SightseeingToursActivity.this).imageLoader.displayImage(imageurlss, images,
Common.optionsSmall);
textview.setText(category1);
} else {
addView = mInflater.inflate(R.layout.sightseeing_secondview, null);
iv = (ImageView) addView.findViewById(R.id.secondimagesightseeing_second);
tv = (TextView) addView.findViewById(R.id.textView2);
llsecond = (LinearLayout) addView.findViewById(R.id.llsecondview);
String category2 = finalResult.get(i).getString("CategoryName");
imageurl2 = finalResult.get(i).getString("ImageUrl");
((BaseActivity) SightseeingToursActivity.this).imageLoader.displayImage(imageurl2, iv,
Common.optionsSmall);
tv.setText(category2);
if (result.length() > i + 1) {
i++;
ivview = (ImageView) addView.findViewById(R.id.secondimagesightseeing_third);
text = (TextView) addView.findViewById(R.id.textView3);
llthird = (LinearLayout) addView.findViewById(R.id.llthirdview);
String category3 = finalResult.get(i).getString("CategoryName");
imageurl3 = finalResult.get(i).getString("ImageUrl");
((BaseActivity) SightseeingToursActivity.this).imageLoader.displayImage(imageurl3, ivview,
Common.optionsSmall);
text.setText(category3);
}
llayout.addView(addView);
}
position = llayout.indexOfChild(addView);
addView.setTag(position);
addView.setOnClickListener(this);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
position = (Integer) v.getTag();
Toast.makeText(SightseeingToursActivity.this, "Position" + position, Toast.LENGTH_SHORT).show();
try {
Intent in = new Intent(SightseeingToursActivity.this, sightseeing_list.class);
in.putExtra("TourList", result.getJSONObject(position).toString());
startActivity(in);
} catch (Exception e) {
}
}
【问题讨论】:
-
看起来
llayout.addView(addView);应该在最后一个else块之外。也就是说,将它移到紧接在position = llayout.indexOfChild(addView);之前,在else的右括号之后。 -
嗨,迈克...我还没有得到确切的位置。有两个布局。第一个,我添加单个图像,在第二个布局中,我有两个水平图像。序列是一个图像填充父级,然后是第二个布局,两张图片,再两张图片,然后是下一张图片,这是重复的。现在我想要点击每个图片的位置。
-
你在添加视图后写了set Tag语句,所以在添加视图之前写
标签: android