Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8933361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:31:11+00:00 2026-06-15T09:31:11+00:00

A had a custom ListView recently. Then I had to display all the list

  • 0

A had a custom ListView recently. Then I had to display all the list items without scrollbar. Following the method to place items in LinearLayout I changed my code but I can’t bind onClickListener to new layout. In ListView I used position var to determine what view was touched. But in LinearLayout onClick callback hasn’t position parameter.

Here is my 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);
    }
}

My question is how to get view’s position at onClick(View v)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:31:12+00:00Added an answer on June 15, 2026 at 9:31 am

    You can use the View's tag;

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

    and in the onClick(View v)

    public void onClick(View v) {
        int position = 0;
        if (v.getTag() instanceof Integer) {
           position = (Integer) v.getTag();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We had a custom object extension method that would handle the following. Source is
I was implementing a ListView activity with custom drawn views and had the following
I am using the following method when adding items to my ListView . I've
I had a custom listview with two textviews and an image view. On clicking
My Custom Adapter that extends SimpleCursorAdapter for my ListFragment does not display the items
Currently I face the following issue - I create a custom ListView ( 1
I created a listview that has a custom SimpleCursorAdapter. I want to place a
I want to create custom AlertDialog, but without AlertDialog.Builder. I set ListView as content
previously i had custom tableviewcell and was loading from Nib.in that i have specified
Hi, I had Generate custom layouts in my screen, i want change color or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.