简体   繁体   中英

ArrayList strange behavior

Im doing a app that takes data from a xml, put on a database, and the display this data (the name of the picture and the url) in a gridview of pictures. I added a onitemclicklistener to get the url (from the ArrayList), and transfere to another activity, so I can transform the url to a bitmap and display in a imageview.

   //cursor that returns all items from databse to a arraylist
    final Cursor cursor = entry.getAllRows();

//arraylist
            names = new ArrayList<Post>();

            cursor.moveToFirst();
            //adding all database values to the arraylist
            while (cursor.isAfterLast() == false) {
                names.add(new Post(cursor.getString(cursor.getColumnIndex("name")), cursor.getString(cursor.getColumnIndex("link"))));
                cursor.moveToNext();



            }

now the onitemclicklistsner code:

@Override
            public void onItemClick(AdapterView<?> parent, View v, int position,
                    long id) {
                Intent i = new Intent(getApplicationContext(), FullImageActivity.class);

                position++;
            Post text = names.get(position);
            Log.i("POSITION", "Exeption:"+position + " " + text);

                //i.putExtra("id", text);
                //startActivity(i);

            }
        });

Im getting the right position, but the problem is that i cannot get the corresponding url from the position. Its not displayng a link, but displayng this: com.example.partedoxml.Post@42b191d8 The question is how I cant get a single item position from the arraylist?

your problem is

names.get(position);

what you are trying to do is getting the Post object not the name

so in your Post Class there will be a filed named by Name

get that name like

in post class create getter and setter for name and then in your listener write

String name =names.get(position).getName();

or

in your post class make name public

public String name;

and then in your listener

 String name =names.get(position).name;

or what you are doing

   Post text = names.get(position);  
   Log.i("POSITION", "Exeption:"+position + " " + text.name);

You are getting one post by index (index being you specifying in the position variable).

You need to implement public String toString(){} method in your Post class to see field values within the Post object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM