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

  • SEARCH
  • Home
  • 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 8081917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:53:56+00:00 2026-06-05T16:53:56+00:00

I am working in getView() method in the code below. I am trying to

  • 0

I am working in getView() method in the code below. I am trying to iterate through a list of objects and get their index. In the code below I use a while loop to do this. However, I am not getting the expected results. My List that holds the 30 objects and is called items. I’m using a while loop and calling the index by these method: items.get(j) and items.indexOf(j). Instead of getting an integer that corresponds to the index in the list, I get -1 for the index or a memory address. How is it possible for list of size 30 to get have -1 index? Please see my code below.

Question 1:how do I print out the index of each object int the list.

Question 2: is this strange behavior a memory leak that has to do with getview recycling the listview?

package com.convention.notification.app;

import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class NewsRowAdapter extends ArrayAdapter<Item> {

    private Activity activity;
    private List<Item> items;
    private Item objBean;
    private int row;
    private List<Integer> disable;
    View view ;

    public NewsRowAdapter(Activity act, int resource, List<Item> arrayList, List<Integer> disableList) {
        super(act, resource, arrayList);
        this.activity = act;
        this.row = resource;
        this.items = arrayList;
        this.disable=disableList;

        System.out.println("results of delete list a:"+disable.toString()); 

    }

     public long getItemId(int position){
           return position;
        }

        public Item getItem(int position){
             return null;
           }



            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                View view = convertView;
                ViewHolder holder;
                if (view == null) {
                    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = inflater.inflate(row, null);

                    long id=getItemId(position);
                    //System.out.println(" at position "+position);


                     int j=0; 
                     System.out.println("item size at  "+items.size());
                        while (j< items.size())
                        {
                                System.out.println("while loop items.get(index): " +items.get(j) );
                                System.out.println( "while loop item.indexOf(index): "+items.indexOf(j) );
                                j++;
                        }

                    try{



                            //  System.out.println("item size at  "+items.size());

                                    for(int k =0;k< items.size();k++){

                                //      System.out.println("item index at "+items.get(k));
                                //      System.out.println("value of index "+k);

//                                      if(id == disable.get(k)){
//                                          view.setBackgroundColor(Color.YELLOW);
//                                          //System.out.println("background set to yellow at disable list "+disable.get(j));
//                                      //  System.out.println("background set to yellow at id "+id);
//                                          break;
//                                      } else {
//                                          view.setBackgroundColor(Color.WHITE);
//                                      //  System.out.println("background set to white at position "+position);
//                                      }
//                                  

                                    }


                                //}
                    }catch(IndexOutOfBoundsException e){

                    //System.out.println(" crash");
                    }




                    //ViewHolder is a custom class that gets TextViews by name: tvName, tvCity, tvBDate, tvGender, tvAge;
                    holder = new ViewHolder();

                    /* setTag Sets the tag associated with this view. A tag can be used to
                     *  mark a view in its hierarchy and does not have to be unique 
                     *  within the hierarchy. Tags can also be used to store data within
                     *   a view without resorting to another data structure.

        */
                    view.setTag(holder);
                } else {

                    //the Object stored in this view as a tag
                    holder = (ViewHolder) view.getTag();
                }

                if ((items == null) || ((position + 1) > items.size()))
                    return view;

                objBean = items.get(position);


        holder.tv_event_name = (TextView) view.findViewById(R.id.tv_event_name);
        holder.tv_event_date = (TextView) view.findViewById(R.id.tv_event_date);
        holder.tv_event_start = (TextView) view.findViewById(R.id.tv_event_start);
        holder.tv_event_end = (TextView) view.findViewById(R.id.tv_event_end);
        holder.tv_event_location = (TextView) view.findViewById(R.id.tv_event_location);


        if (holder.tv_event_name != null && null != objBean.getName()
                && objBean.getName().trim().length() > 0) {
            holder.tv_event_name.setText(Html.fromHtml(objBean.getName()));

        }
        if (holder.tv_event_date != null && null != objBean.getDate()
                && objBean.getDate().trim().length() > 0) {
            holder.tv_event_date.setText(Html.fromHtml(objBean.getDate()));
        }
        if (holder.tv_event_start != null && null != objBean.getStartTime()
                && objBean.getStartTime().trim().length() > 0) {
            holder.tv_event_start.setText(Html.fromHtml(objBean.getStartTime()));
        }
        if (holder.tv_event_end != null && null != objBean.getEndTime()
                && objBean.getEndTime().trim().length() > 0) {
            holder.tv_event_end.setText(Html.fromHtml(objBean.getEndTime()));
        }
        if (holder.tv_event_location != null && null != objBean.getLocation ()
                && objBean.getLocation ().trim().length() > 0) {
            holder.tv_event_location.setText(Html.fromHtml(objBean.getLocation ()));

        }


        return view;
    }

    public class ViewHolder {
        public TextView 
        tv_event_name,
        tv_event_date,
        tv_event_start,
        tv_event_end,
        tv_event_location
        /*tv_event_delete_flag*/;


    }


}

Logcat:
Please note this logcat output repeats about 5 to 7 times in one launch. I’ve shortened it here.

06-11 19:58:17.471: I/AndroidRuntime(1347): NOTE: attach of thread 'Binder Thread #3' failed
06-11 19:58:18.541: I/ActivityManager(60): Displayed com.convention.notification.app/.DataView: +1s161ms
06-11 19:58:20.471: I/System.out(1355): item disalbed is at postion :1
06-11 19:58:20.471: I/System.out(1355): item disalbed is at postion :6
06-11 19:58:20.471: I/System.out(1355): item disalbed is at postion :14
06-11 19:58:20.471: I/System.out(1355): item disalbed is at postion :15
06-11 19:58:20.471: I/System.out(1355): item disalbed is at postion :18
06-11 19:58:20.471: I/System.out(1355): results of delete list :[1, 6, 14, 15, 18]
06-11 19:58:20.481: I/System.out(1355): results of delete list a:[1, 6, 14, 15, 18]
06-11 19:58:20.481: I/System.out(1355):  set adapaer to list view called;
06-11 19:58:20.581: I/System.out(1355): item size at  30
06-11 19:58:20.581: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574210
06-11 19:58:20.581: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.581: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574488
06-11 19:58:20.581: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.581: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@405744e0
06-11 19:58:20.631: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.631: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574538
06-11 19:58:20.631: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.631: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574590
06-11 19:58:20.631: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.631: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@405745e8
06-11 19:58:20.642: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.642: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574640
06-11 19:58:20.642: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.642: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574698
06-11 19:58:20.642: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.642: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@405746f0
06-11 19:58:20.642: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.642: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574748
06-11 19:58:20.671: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.671: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@405747c0
06-11 19:58:20.671: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.681: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574818
06-11 19:58:20.681: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.681: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574870
06-11 19:58:20.681: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.681: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574928
06-11 19:58:20.681: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.721: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574980
06-11 19:58:20.721: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.721: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@405749d8
06-11 19:58:20.721: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.731: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574a30
06-11 19:58:20.731: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.731: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574a88
06-11 19:58:20.731: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.731: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574ae0
06-11 19:58:20.761: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.761: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574bb8
06-11 19:58:20.761: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.761: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574c30
06-11 19:58:20.761: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.761: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574c88
06-11 19:58:20.761: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.771: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574ce0
06-11 19:58:20.781: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.781: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574d38
06-11 19:58:20.811: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.811: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574d90
06-11 19:58:20.811: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.811: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574de8
06-11 19:58:20.811: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.811: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574e40
06-11 19:58:20.811: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.811: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574e98
06-11 19:58:20.811: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.811: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40574fa8
06-11 19:58:20.811: I/System.out(1355): while loop item.indexOf(index): -1
06-11 19:58:20.811: I/System.out(1355): while loop items.get(index): com.convention.notification.app.Item@40575000
06-11 19:58:20.811: I/System.out(1355): while loop item.indexOf(index): -1
  • 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-05T16:53:58+00:00Added an answer on June 5, 2026 at 4:53 pm

    the indexOf Function takes an Object, and should be an Object of the same type as the List. So items.indexOf(items.get(index)) is the correct syntax. See http://docs.oracle.com/javase/7/docs/api/java/util/List.html#indexOf%28java.lang.Object%29 for details

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey im trying to get a List working, in that case i have a
I'm trying to get a custom adapter working with XML Inflater, though I get
Working on a rather small, and simple layout, I decided to use Meyer's CSS
Working on a new app and using restful-authentication. I was trying to make it
This is one of those cases where I had the code working, tried to
While working over listView I have came across a trouble situation. Actually I am
I am working on customize (inflated) list view. In which I have used the
Hi i'm working on just making a simple list i just want an icon
I want to display some text below each grid image. Please see my code
Below is my code for setting up a listview. I want to implement a

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.