Android Next item on listview from second Activity

好久不见. 提交于 2019-12-13 04:46:19

问题


I have a listview connected to an adapter showing messages but, I am trying to implement next and previous buttons in a different activity.

The problem is how do u show the next or previous item on the listview when a button is clicked from another activity (detailed view activity).

I have entered image description here :

Attached are screen shots of the project with the next and previous buttons


回答1:


You need to get the data from the array or arraylist whatever you are using for adapter. Adapter needs either array or list of data while creating. you can store this array or list for further access

Try with the following steps

  • When you select any particular item you are getting position of the item. store this position as a counter variable.
  • Depending on the position you are showing some data in some view.
  • when you click on next button increase the counter
  • Similarly when you click on previous button reduce the counter
  • Use this counter value to get the data from the array or list which is used in listview



回答2:


1 create your arraylist public static which is adapter use like this: public static ArrayList arraylist =new ArrayList<>(); in your class where you are set adapter

2.When you select any item you are getting position of the item from arraylist and store this value in store shared preference.

3.your arraylist from your MainActivity to SecondActivity. like this;

public static ArrayList arraylistSecond=MainActivity.arraylist;

  1. when you click NEXT button get position the position from shared preference and increase position and get item.

  2. and same when you click previous button decrease position and get item

happy coding



来源:https://stackoverflow.com/questions/33386572/android-next-item-on-listview-from-second-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!