How to get the previous item of clicked listviewitem

泪湿孤枕 提交于 2019-12-10 21:55:49

问题


I have two activities. In activity1 , I have list view which is populated from the database. On item click it should go to activity2. Activity2 contains two buttons (next and previous) and display the product details. In stead of "next" and "previous" button text, i was trying to get the previous item of clicked listview item and set text in button. and so for the "next" button.

myList.setOnItemClickListener(new OnItemClickListener() {

 public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
 MyClass item = (MyClass) adapter.getItem(position-1);
Intent i = new Intent(this, Activity2.class);
 i.putExtra("item",item.toString());
startActivity(Activity2);
}
}

and in activity2.class

 buttonprevious = (Button) findviewById(R.id.previous);
 Bundle b = getIntent().getExtras();
 String preitem = b.getString("item");
 buttonprevious.setText(preitem);

Is this the correct way or am i doing something wrong??

How can I display product details of next and previous items?? How to use the view flipper in this case? Thanks ..


回答1:


You can use getItem(int position) method of the adapter, which return you the list item of the specified position.

(OR)

Get and Store all the database data into one static List. Set adapter with that list. In Activity2 access data from that list.



来源:https://stackoverflow.com/questions/7269830/how-to-get-the-previous-item-of-clicked-listviewitem

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