如何在主线性布局中动态创建视图的位置?

时间:2015-12-09 18:24:21

标签: android

我在这里创建一个动态布局。我有一个父布局,我在其中添加所有动态创建的视图。现在点击特定视图我需要位置。

根据该位置,我必须在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) {

    }

}

1 个答案:

答案 0 :(得分:0)

run
  

试试这个