开发者

How to get the previous item of clicked listviewitem

I have two activities. In activity1 , I have list view which is populated from the database. On item click it should go t开发者_开发问答o 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 ..


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜