I have the following code:
chart.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final String aux= (String) lt.getItemAtPosition(position);
Intent myIntent = new Intent(infoList.this, tabList.class);
startActivity(myIntent);
}
});
I also have a ListView. When I click on an item from that ListView I navigate to another activity that shows me the info for that activity. How do I do that? I want that info to correspond to the item I clicked.
Here is what I did in this situation, if you don't want to just pass an id back:
I call the other activity with this:
Intent intent = new Intent(myapp, CreateExerciseActivity.class);
intent.putExtra("selected_id", workout.getId());
startActivityForResult(intent, CREATE_ACTIVITY);
I then do
Intent returnIntent = new Intent();
returnIntent.putExtra("name", m.getName());
returnIntent.putExtra("unit", m.getUnit());
returnIntent.putExtra("quantity", m.getQuantity());
if (getParent() == null) {
setResult(Activity.RESULT_OK, returnIntent);
} else {
getParent().setResult(Activity.RESULT_OK, returnIntent);
}
finish();
So, in this case I was passing in an id in order to get more details from the user, and then I pass those back to the calling activity, in order to add it, as I didn't want to save this until the user chooses to later.
So, you need to do startActivityForResult in order to have it able to return the data.
You can add some data to the Intent with the methods putExtra(), and then retrieve the data in the new Activity with getExtras().getSomething().
i guess you have to use OnItemClickListener instead of the click event and you need intent to call the next activity.
private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id)
{
Intent intent = new Intent(this, MyInfoActivity.class);
intent.putExtra("selected_id", getIdFor(position));
startActivity(intent);
}
};
mHistoryView = (ListView)findViewById(R.id.history);
mHistoryView.setOnItemClickListener(mMessageClickedHandler);
Possible answer: How do I pass data between Activities in Android application?
http://thedevelopersinfo.wordpress.com/2009/10/15/passing-data-between-activities-in-android/
It really depends on what the data is in your listview. For example, if you're displaying a list of contacts in the listview, you could just pass the ID of the contact over to the other activity, and let that activity access the content provider for contacts to retrieve the data you want it to work with. You'd pass an ID within a URI in the data field of the intent, as opposed to in the extras bundle.
Related
I'm working on a simple notes app that contains a ListView and a plus button, I make the ListView item open a new activity when clicked
in this new activity also contains a ListView and a plus button
My problem is that every item opens the same new activity with the same data
So how to make each item in the ListView open a new empty activity with new data?
public void onClick(View v) {
Intent i = new Intent(v.getContext(), SecondActivity.class);
v.getContext().startActivity(i);
}
From first activity send some intent key say,"next" having value "new" if you want second activity to be empty,
else send intent value "edit",if you want second activity to have some previous data.
public void onClick(View v) {
Intent i = new Intent(v.getContext(), SecondActivity.class);
i.putStringExtra("next",new);
v.getContext().startActivity(i);
}
and receive this data in another activity,
and if received data in another activity if new, then clear all editText.
like..
Intent intent=getIntent();
String key=intent.getStringExtra("next");
if(key.equals("new"))
{
//clear all editText;
}else
{
//load previous data;
}
I am new to Android development and I am trying to start another activity when the user clicks on an item in a ListView. However, I am stuck trying to create an Intent to do it. I am trying to follow this guide, where they use the this keyword. However, it doesn't work in this case because this refers to the AdapterView.onItemClickListener.
My current code is this:
list.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter,
View item, int pos, long id) {
Intent intent = new Intent(this, OtherActivity.class);
// use the Intent to start another activity...
}
});
What should I be replacing this with?
use your activity name before this, example:
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
I am currently making an app that containing a widget.
When placing the app a WidgetConfigure Activity opens where you can select an object out of a recycleViewer.
The object (Kitten) implements the parcelable and the recyleviewer is filled by an adapter class.
When the eventListner of that recyleviewer is triggered, the selected object must be passed to the widget since I need properties from that object in my widget.
I'm currently completely stuck and confused how I can pass an object to the widget.
How can I pass an object to the widget?
Here is my code that will run in the eventListner in
LongEventConfigureActivity:
private void makeWidget(Event myObj){
// How I normally passing data to an activity/fragment
//Intent i = new Intent();
//Bundle b = new Bundle();
//b.putParcelable(CONST_PARCELKEY, myObj);
//i.putExtras(b);
//i.setClass(this, ObjectDetailActivity.class);
//startActivity(i);
// Example of how to pass data to the widget
final Context context = LongEventWidgetConfigureActivity.this;
// When the button is clicked, store the string locally
String widgetText = txtWidgetText.getText().toString();
saveTitlePref(context, mAppWidgetId, widgetText);
// It is the responsibility of the configuration activity to update the app widget
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
LongEventWidget.updateAppWidget(context, appWidgetManager, mAppWidgetId);
// Make sure we pass back the original appWidgetId
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
#Override
public void onItemClick(Event e) {
makeWidget(e);
}
Also I need to find a way when the user tapping this widget, a certain activity of my app will open that will receive that object.
Summery of the problem (I don't have the privilege to post an image yet)
Well, as I mentioned in heading, I can't get data from one activity, to another. I am making a really simple RSS reader, that loads data asynchroously, sends them to listview and opens the selected article in new activity's webview. And the problem is, that when i try to start an activity with webview, a can't pass data from the main activity.
So here is what I am doing:
1) In first activity (Main activity), i get data from my listview element, that user clicks on:
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
News clicked = (News) adapter.getItem(arg2);
String clickedTitle = clicked.title;
String clickedLink = clicked.link;
System.out.println("Title: " + clickedTitle + " / LINK: " + clickedLink); /*Here I can see my data in logs -> they are correct at this moment*/
Intent intent = new Intent(MainActivity.this, OpenFullArticle.class);
intent.putExtra(clickedTitle, "clickedTitle");
intent.putExtra(clickedLink, "clickedLink");
startActivity(intent);
}
});
So for now everything is okey, but than, when a new activity is started (OpenFullArticle activity), I am doing this:
public class OpenFullArticle extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open_full_article);
String title = getIntent().getExtras().getString("clickedTitle");
String link = getIntent().getStringExtra("clickedLink");
System.out.println(title); //Tried link too, simultaneous each one - both ar null
}
So I'm stuck for this moment and have no idea, what I'm doing wrong. Tried debuger, checked the Intent, and it hadn't extras there, that I added in first activity.
Maybe you can give me a clue, what could cause such a strange issue.
P.S. If it's necessary, I can provide a full code or a full project in github.
Use following code in first activity
Intent intent = new Intent(MainActivity.this, OpenFullArticle.class);
intent.putExtra( "clickedTitle",clickedTitle);
intent.putExtra("clickedLink", clickedLink, );
startActivity(intent);
Change to
intent.putExtra("clickedTitle", clickedTitle);
// the first pram is the key
// the second is the value
intent.putExtra("clickedLink", clickedLink);
http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String, android.os.Bundle)
Also use Log instead of System.out.println.
http://developer.android.com/reference/android/util/Log.html
The mistake is in this line:
intent.putExtra(clickedTitle, "clickedTitle");
intent.putExtra(clickedLink, "clickedLink");
You need to give key first and then the value:
intent.putExtra("clickedTitle", clickedTitle);
Replace
intent.putExtra(clickedTitle, "clickedTitle");
intent.putExtra(clickedLink, "clickedLink");
with
intent.putExtra("clickedTitle",clickedTitle);
intent.putExtra("clickedLink",clickedLink);
Use the following code in your first activity
Intent intent = new Intent(MainActivity.this, OpenFullArticle.class);
intent.putExtra( "clickedTitle",clickedTitle);
intent.putExtra("clickedLink", clickedLink, );
startActivity(intent);
Initially try to isolate the issue by passing some hardcoded values to extras
like
intent.putExtra("id",1);
intent.putExtra("name","abc");
and check whether OpenFullArticle activity has the extras.
I am relatively new to the Eclipse & ADT plugin world, so any answers can you please explain what it's doing? It would be very helpful.
Basically, I have a list in one activity that will be populated by a HTTP request from a database API, this I am still working on. However, what I wish to know, is am I able to take the string in the ListItem and give that to a TextView in the next activity?
So for example, I tap 'Record 1' and it takes 'Record 1' and puts it inside a variable, then sends the user to the next screen, and inserts the variable into a TextView. Is this possible?
Thank you.
Get what you want from the adapter, then put that into the Intent that starts the new activity:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String string = parent.getAdapter().getItem(position);
Intent intent = new Intent(this, nextActivity.class);
intent.putExtra("text", string);
startActivity(intent);
}
});
In the new Activity you then get the Intent that started the new Activity and get the String you put into it:
String data = getIntent().getStringExtra("text");
Implement the setOnItemClickListener() for the listItem where you will capture the listItem clicked and the String you require and then using intent.putExtra(String id, STRING_VALUE) you can pass your string to the next activity.
Yes, it is possible.
You can send information to another activity using Extra. See this code below:
Intent i = new Intent(this, NoteEdit.class);
i.putExtra(NotesDbAdapter.KEY_ROWID, id);
startActivityForResult(i, ACTIVITY_EDIT);
You use putExtra to put your data do you want to send to the activity. In the example NotesDbAdapter.KEY_ROWID is the name you give to access the data and id is the data itself.
And, this is how you can get the data from the activity:
Bundle extras = getIntent().getExtras();
mRowId = (extras == null) ? null : extras.getLong(NotesDbAdapter.KEY_ROWID);