如何获得单击的列表的上一项viewitem [英] How to get the previous item of clicked listviewitem

查看:133
本文介绍了如何获得单击的列表的上一项viewitem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个活动.在activity1中,我具有从数据库填充的列表视图.在项目上单击它应该转到activity2. Activity2包含两个按钮(下一个和上一个),并显示产品详细信息.代替下一个"和上一个"按钮的文本,我试图获取被单击的列表视图项的上一个项目,并在按钮中设置文本.以此类推.

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);
}
}

和activity2.class中

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 ..

推荐答案

您可以使用适配器的getItem(int position)方法,该方法将向您返回指定位置的列表项.

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

(OR)

获取所有数据库数据并将其存储到一个静态列表中. 用该列表设置适配器. 在Activity2中,访问该列表中的数据.

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

这篇关于如何获得单击的列表的上一项viewitem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆