0

I need help here. I'm trying to take a value from my class and take it to another class to be processed.... My problem is, the value is from an adapter. I'm stuck here.

Here is my code :

lv.setOnItemClickListener(new OnItemClickListener()
{
 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
 {
     //Toast.makeText(getApplicationContext(), data.get(arg2).toString(),3000).show();
     Intent newI = new Intent (this, PDetail.class); 
     startActivity (newI);
 }});

This is the class i want to call (PDetail.class) :

public class PDetail extends Activity {
 //Create ct;
 Toast.makeText(getApplicationContext(), data.get(arg2).toString(),3000).show();
}

My question is, how can i get the "data" value from the first class??? Any suggestion appreciated.

Charles Lynch
  • 221
  • 1
  • 2
  • 8
  • 1
    please look at: http://stackoverflow.com/a/819427/513413 – Hesam Jul 31 '13 at 02:25
  • Thanks for the references – Charles Lynch Jul 31 '13 at 02:28
  • I think his problem is actually getting it from the adapter. Usually adapters have the method `getItem` which will return the object associated with that position – FabianCook Jul 31 '13 at 02:28
  • Well, actually thats what i mean....@SmartLemon – Charles Lynch Jul 31 '13 at 02:31
  • @SmartLemon sometimes not always. sometimes (if you are using holder pattern) for better performance you need to return null instead. Look at here for more info: http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/ – Hesam Jul 31 '13 at 02:34
  • In the context of an ArrayAdapter it will return an object with the type it was created with, or object. http://developer.android.com/reference/android/widget/ArrayAdapter.html#getItem(int) – FabianCook Jul 31 '13 at 02:44
  • Returning null would suggest the method has been overridden, just not implemented – FabianCook Jul 31 '13 at 02:45

1 Answers1

1

In your first activity

lv.setOnItemClickListener(new OnItemClickListener()
{
 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
 {
     Intent newI = new Intent (this, PDetail.class); 
     newI.putExtra("value",data.get(arg2));
     startActivity (newI);
 }});

In your second activity

   String value =getIntent().getStringExtra("value");

I hope it can help you.

  • That's exactly what i did but it won't work...... the error says that i have to erase the argument .... it became like this : **Intent newI = new Intent ();** Any Idea ??? why i cant put **Intent newI = new Intent (this, PDetail.class);** – Charles Lynch Jul 31 '13 at 03:48
  • 1
    You should use classaName Intent newI = new Intent (firstclassName.this, PDetail.class); – user2633519 Jul 31 '13 at 04:08
  • It shows no error.... but still it forced close when i run it.... anyway thx for your reply – Charles Lynch Jul 31 '13 at 04:20