获取LinearLayout(Android)中的视图位置

时间:2012-12-03 13:43:35

标签: android android-listview position android-linearlayout

最近有一个自定义ListView。然后我必须显示没有滚动条的所有列表项。在method to place items in LinearLayout之后,我更改了代码,但我无法将onClickListener绑定到新布局。在ListView中,我使用position var来确定触摸了哪个视图。但是在LinearLayout onClick中,回调没有位置参数。

这是我的BasketActivity.class:

package ru.**.**;

public class BasketActivity extends Activity {

    ArrayList<Item> items = new ArrayList<Item>();
    SQLiteDatabase database;
    Map<String, ?> all;
    ItemAdapter adapter;
    Item item2delete;
    View deletingView;

    private SharedPreferences settings;
    private SharedPreferences settings2;
    private Basket basket;
    TextView basketSum;
    private int position2delete;
    private Map<String, ?> all2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basket);

        <..cutted..>

        settings = getSharedPreferences("basket", 0);
        settings2 = getSharedPreferences("price", 0);
        basket = new Basket(settings, settings2);
        all = basket.getList();

        LinearLayout l1 = (LinearLayout) findViewById(R.id.l1);
        MyDBAdapter myDBAdapter = new MyDBAdapter(getBaseContext());
        myDBAdapter.open();
        Cursor itemCursor = myDBAdapter.getItemsInBasket(all);
        while (itemCursor.moveToNext()) {

            String[] columns = itemCursor.getColumnNames();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < columns.length; i++) {
                sb.append(columns[i]);
            }

            Item item = new Item();
            item.setId(itemCursor.getString(0));
            item.setArticul(itemCursor.getString(MyDBSchema.ITEM_ARTICUL));
            item.setTitle(itemCursor.getString(MyDBSchema.ITEM_TITLE));
            item.setPrice(itemCursor.getInt(MyDBSchema.ITEM_PRICE));
            item.setPic80(itemCursor.getString(MyDBSchema.ITEM_PIC80));
            item.setPic300(itemCursor.getString(MyDBSchema.ITEM_PIC300));
            item.setMessage(itemCursor.getString(MyDBSchema.ITEM_MESSAGE));
            item.setColor(itemCursor.getString(MyDBSchema.ITEM_COLOR));
            item.setColorpos(itemCursor.getString(MyDBSchema.ITEM_COLORPOS));
            items.add(item);
        }
        itemCursor.close();
        myDBAdapter.close();

        for (int position=0; position<items.size(); position++) {
            LayoutInflater inflater = getLayoutInflater();
            View v = inflater.inflate(R.layout.list_basket, null);

            final Item i = items.get(position);
                if (i != null) {
                    Item ei = (Item) i;

                    final TextView title = (TextView) v.findViewById(R.id.title);
                    if (title != null) title.setText(ei.title);

                    final TextView articul = (TextView) v.findViewById(R.id.articul);
                    if (articul != null) articul.setText(ei.articul);

                    TextView payed = (TextView) v.findViewById(R.id.payed);
                    if (payed != null) payed.setVisibility(TextView.GONE);

                    TextView status = (TextView) v.findViewById(R.id.status);
                    if (status != null) status.setVisibility(TextView.GONE);

                    Context context = getBaseContext();
                    ProgressBar p = new ProgressBar(context);
                    p.setIndeterminate(true);
                    Drawable d = p.getIndeterminateDrawable();          
                    WebImageView wiv = (WebImageView) v.findViewById(R.id.pic80);
                    wiv.setImageWithURL(context, ei.pic80, d);

                    final TextView price = (TextView) v.findViewById(R.id.price);
                    if (price != null) price.setText(ei.price_valid);

                    final TextView quant = (TextView) v.findViewById(R.id.quant);
                    if (quant != null) {
                        int q = (Integer) all.get(ei.articul);
                        if (q > 1) {
                            quant.setText("x "+q+" шт.");
                        }
                    }

                }
                v.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        deletingView = v;
                        int position = 0; //The order number of the view should be here!
                        Item item = (Item) items.get(position);
                        item2delete = item;
                        position2delete = position;

                        AlertDialog.Builder builder = new AlertDialog.Builder(getParent());
                        builder.setMessage(getString(R.string.suredelete))
                                .setCancelable(false)
                                .setPositiveButton(getString(R.string.yes),
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int id) {
                                                BasketActivity.this.removeItem();
                                                deletingView.setVisibility(View.GONE);
                                            }
                                        })
                                .setNegativeButton(getString(R.string.no),
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int id) {
                                                dialog.cancel();
                                            }
                                        });
                        AlertDialog alert = builder.create();
                        alert.show();
                    }
                });
                l1.addView(v);
        }


        basketSum = (TextView) findViewById(R.id.basketsum);
        basketSum.setText(Html.fromHtml(getString(R.string.basketsum) + ": <b>"
                + basket.getBasketPriceValid() + "</b>"));
    }

    protected void removeItem() {
        basket.remove(item2delete);
        Context context = getApplicationContext();
        CharSequence text = getText(R.string.item_deleted);
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, text, duration);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
        basketSum.setText(Html.fromHtml(getString(R.string.basketsum) + ": <b>"
                + basket.getBasketPriceValid() + "</b>"));
        items.remove(position2delete);
    }
}

我的问题是如何在onClick(View v)获得视图的位置?

3 个答案:

答案 0 :(得分:10)

您可以使用View' s标记;

for (int position=0; position<items.size(); position++) {
    v.setTag(position);
}

onClick(View v)

public void onClick(View v) {
    int position = 0;
    if (v.getTag() instanceof Integer) {
       position = (Integer) v.getTag();
    }
}

答案 1 :(得分:2)

你能

吗?
v.setTag(items.get(position).getId();

然后在onClick

public void onClick(View v) {
  int id= v.getTag();
}

答案 2 :(得分:-1)

你可以拥有

val index =  parent.indexOfChild(child)