Android Get Thing Clicked In Listview To Settext
I have two classes, a listview class with many strings and a class with a text view .. I want when I click on a string on the listview the second class starts and the textView in i
Solution 1:
You dont get data in one method of an activity in another activity, unless you pass the data between activites. They are only locally available to one activities
Use putExtra
to send data from one activity to another. Now in the second activity recieve that value and set it as text to the TextView
Here for e.g
In the first Activity
Intent select = new Intent(LV.this, Main.class);
select.putExtra("item", item)
startActivity(select);
In the next activity, receive this
Bundleextras= getIntent().getExtras();
if (extras != null) {
Stringvalue= extras.getString("item");
}
S.setText(value);
Solution 2:
item
is just a local variable of onListItemClick()
, you cannot access it statically like you tried too. Use Intent
to send data to another activity.
@Override
protectedvoidonListItemClick(android.widget.ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String item = (String) getListAdapter().getItem(position);
Intent select = new Intent(LV.this, Main.class);
select.putExtra("itemClicked", item);
startActivity(select);
}
And in your second activity, probably in onCreate()
:
Bundleextras= getIntent().getExtras();
S.setText(extras.getString("itemClicked");
Solution 3:
To send data between Activities use this:
@Override
protectedvoidonListItemClick(android.widget.ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String item = (String) getListAdapter().getItem(position);
Intent select = new Intent(LV.this, Main.class);
select.putExtra("item", item);
startActivity(select);
}
And in another Activity use this
Stringitem= getIntent().getStringExtra("item");
Solution 4:
MainActivity
String listview_array[]={"01634 ABOHAR","080 Bangalore","011 Delhi","Dell Inspiron"};
lv.setAdapter(newArrayAdapter<String>
(MainActivity.this,
android.R.layout.simple_list_item_1, listview_array));
lv.setOnItemClickListener(newOnItemClickListener()
{
@OverridepublicvoidonItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
for(int i=0;i<listview_array.length;i++)
{
if(arg2==i)
{
Intent t= newIntent("com.example.listviewsearch.detailview");
t.putExtra("Extra", listview_array[arg2]);
startActivity(t);
}
}
}
});
detailview
TextView tv= (TextView) findViewById(R.id.tv1);
Bundle extras = getIntent().getExtras();
System.out.println("Get Intent done");
if(extras !=null)
{
tv.setText(extras.getString("Extra").toString());
}
Post a Comment for "Android Get Thing Clicked In Listview To Settext"