简体   繁体   English

ArrayList奇怪的行为

[英]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. 我正在做一个应用程序,它从xml中获取数据,并放置在数据库中,并在图片的gridview中显示此数据(图片的名称和url)。 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. 我添加了一个onitemclicklistener来获取url(从ArrayList),并转移到另一个活动,因此我可以将url转换为位图并显示在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: 现在是onitemclicklistsner代码:

@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? 它不是显示链接,而是显示链接:com.example.partedoxml.Post@42b191d8问题是我怎么不能从arraylist中获得单个项目的位置?

your problem is 你的问题是

names.get(position);

what you are trying to do is getting the Post object not the name 您想要做的是获取Post对象而不是名称

so in your Post Class there will be a filed named by Name 因此在您的Post Class中将有一个以Name命名的文件

get that name like 得到这样的名字

in post class create getter and setter for name and then in your listener write 在post类中,为名称创建getter和setter ,然后在您的侦听器中编写

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). 您正在按索引获得一个帖子(索引是您在position变量中指定的)。

You need to implement public String toString(){} method in your Post class to see field values within the Post object. 您需要在Post类中实现public String toString(){}方法,以查看Post对象中的字段值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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