简体   繁体   English

如何从ListView获取数据。 (我的列表视图有3个项目)

[英]How to get data from ListView. (My List View has 3 items)

I want retrieve the data from the Listview, where my list is having 4 items like, name, date, sem, category. 我想从Listview检索数据,我的列表中有4个项目,例如名称,日期,sem,类别。

i want to read the text present in the list view and pass it to another activity. 我想阅读列表视图中显示的文本,并将其传递给另一个活动。

If you are retrieving your data from server then you need to get item on item click of ListView. 如果要从服务器检索数据,则需要在ListView的项目单击上获取项目。 Like 喜欢

  listview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

              String name =urarraylist.get(position).get("name");// here you have to pas keyname which is put inyour Hashmap arraylist
              String category =urarraylist.get(position).get("category");
              String date=urarraylist.get(position).get("date");
              String sem=urarraylist.get(position).get("sem");
              Intent i = new Intent(getBaseContext(),anotheractivity.class);
              i.putExtra("name", name);
              i.putExtra("category ", category );
              i.putExtra("date", date);
              i.putExtra("sem", sem);
              startActivity(i); 
            }
        });

Now retrieve this data in your next Activity on onCreate() method. 现在,在onCreate()方法的下一个Activity中检索此数据。 Like 喜欢

       Intent n = getIntent();
       String name = n.getStringExtra("name");
       String category = n.getStringExtra("category ");
       String date= n.getStringExtra("date");
       String sem= n.getStringExtra("sem");

As it's name suggest, a ListView doesn't contains data, it only contains Views . 顾名思义, ListView不包含数据,仅包含Views These views are often created from an array or a List via an Adapter . 这些视图通常是通过Adapter从数组或List创建的。 So you have to ask your adapter to retrieve the data. 因此,您必须要求适配器检索数据。

Unfortunately there is no default method for that, so you'll have to create your own getter, or you can also loop on the getItem() method associated with the getCount() one on your ListView's adapter. 不幸的是,没有为此的默认方法,因此您必须创建自己的getter,或者也可以在与ListView的适配器上的与getCount()相关联的getItem()方法上循环。

listview.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                 String name=(String)parent.getItemAtPosition(position);
                  Intent i = new Intent(getBaseContext(),anotheractivity.class);
                  i.putExtra("USERNAME", name);
                  startActivity(i); 
                }
            });

if you click the listview row the value from corresponding position will be fetched and passed to intent and further to next activity. 如果您单击列表视图行,则将从相应位置获取值,并将其传递给intent并进一步传递给下一个活动。

The magic lies here: 魔术就在这里:

 String name=(String)parent.getItemAtPosition(position);

暂无
暂无

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

相关问题 如何为列表视图中的每一行设置图标(图片)。 列表视图项通过sdcard中的某个文件夹进行访问 - How to set an icon(picture) for each row in listview. List view items access through some folder in sdcard EditText用于过滤listview中的项目。 如何? - EditText to filter items in listview. How to? Listview中的错误。 当我滚动列表视图时,imageview会自动改变。 我怎么了 - Bug in Listview. When I roll my list view, the imageview is changing by it self. What was my mistake? 滚动视图中的ListView我的滚动视图移到列表视图的顶部。 我该如何预防? - ListView in Scroll View my scrollview moves to top of listview. How do I prevent this? 将数据库中的存储数据导入ListView。 - Getting stored data from database into ListView. 如何从Listview获取列表项的计数? - How to get a count of list items from Listview? 如何动态地将子项添加到可展开的列表视图中。 - How do I add child items dynamically to an expandable listview.? 我崩溃了我的listView。 异常调度输入事件 - I get crash my listView. Exception dispatching input event 我如何在列表视图中显示两列。 列数据来自SQLite数据库 - how can i display two columns in listview. that columns data is coming from SQLite database 我想将用户输入的数据保存到列表视图。 我在列表视图中没有任何数据。 请突出问题 - I want to save data entered by user to listview. i am not getting any data in my listview. highlight the issue please
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM