I have two activities. The first activity has one TextView, and the second activity has one ListView. I couldn't transfer between the two via an intent. I must transfer data momentarily. Because I have a timer in the first activity, when the timer goes to 0, then the second activity will start. I must show results in second activity ListView.
You need an arraylist for this. First declare an arraylist in your first activity.
ArrayList<String> clickeddata=new ArrayList<String>();
On button click add the text from textview to arraylist.
clickeddata.add(your_textview.getText().toString());
And when counter is 0 send the arraylist to the second activity.
Intent intent = new Intent(Your_First_Activity.this, Your_second_Activity.class);
intent.putStringArrayListExtra("sentdata", clickeddata);
startActivity(intent);
On second Activity receive it by following code
Intent i = getIntent();
ArrayList<String> mylist=new ArrayList<String>();
mylist = i.getStringArrayListExtra("sentdata");
Now you can create listview by simple adapter like.
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mylist);
your_list_view.setAdapter(adapter);
You can do it by Intent
Intent intent=new Intent(Context,SecondActivity);
intent.putExtra("key",textView.getText().toString());
startActivity(intent);
In Second Activity
String data=getIntent().getStringExtra("key");
and use it.
Related
So, I have done exactly what's on this video: http://android-er.blogspot.com.br/2013/05/add-and-remove-view-dynamically.html
Now, i'd like to get these names I added and send to another activity when I click the "Next" button (it's not on the video, btw). These names should be displayed like in a list view.
To exemplify it better: think of it as a Bowling software. The add/remove view on the video would be the add/remove players from the game. Once you add everyone you want to, you would click the "Next" button and then it would start another activity with the names you have just added and their scores, etc. I think I could use SQLite to store those names, but since it lasts for one game only, I thought using putExtra or whatever, would be the best way. But I have never tried this before, so I don't know how to send this data to a ListView.
Create an arraylist in your first activity and add your data to it
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("data");
arrayList.add("anotherdata");
Send data by Intent putExtra
Intent intent = new Intent(this,SecondActivity.class);
intent.putStringArrayListExtra("arrayList", (ArrayList<String>) arrayList);
startActivity(intent);
In your secondActivity get the data like
ArrayList<String> mylist=getIntent().getExtras().getStringArrayList("arrayList");
Set your arraylist to your listview like following using adapter
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mylist);
listview.setAdapter(arrayAdapter);
You can pass data to a new Activity by using a Bundle. It works like this:
First create a new Bundle and put the data you want to send into that...
Bundle bundle = new Bundle();
bundle.putString("name1", "John");
bundle.putString("name2", "Larry");
bundle.putString("name3", "Cindy");
Then create a new Intent and attach the Bundle to it. Then start your new Activity using the Intent you just created...
Intent intent = new Intent(this, YourSecondActivity.class);
intent.putExtras(bundle);
startActivity(intent);
Then when the new Activity starts up, in the onCreate() method you can extract the data from the Bundle like this...
Bundle bundle = getIntent().getExtras();
String name1 = bundle.getString("name1");
String name2 = bundle.getString("name2");
String name3 = bundle.getString("name3");
Then if you wanted to put those names in a ListView you could do something like this:
Put the names in an ArrayList...
ArrayList<String> nameList = new ArrayList<>();
nameList.add(name1);
nameList.add(name2);
nameList.add(name3);
Then inflate your view and populate your ListView with the ArrayList...
View view = inflater.inflate(R.layout.your_activity_layout, container, false);
ListView listView = (ListView) view.findViewById(R.id.your_listview);
ArrayAdapter arrayAdapter = new ArrayAdapter<>(view.getContext(), R.layout.your_list_item_layout, nameList);
listView.setAdapter(arrayAdapter);
I am trying to send data of a List Array to an Array Adapter in another activity. This is based arround a smiple counter app that when i open the second activity it shows which buttons have been pressed and how many times.
So far i have created my array list the first activity
String player1data = lifepointsP1.getText().toString();
ArrayList<String> myList1 = new ArrayList<String>();
myList1.add(player1data);
and this updates everytime the lifepointsP1 text is changed. Then on the button pushed to open the new activity i do this:
Intent mi = new Intent(v.getContext(), MatchHistory.class);
mi.putExtra("p1L", myList1);
startActivity(mi);
which sends the data across to the second activity and i retrieve it like this:
String p1List = (getIntent().getStringExtra("p1L"));
i then create my array adapter and insert that string as the value:
lvP1 = (ListView) findViewById(R.id.lvP1R);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MatchHistory.this, android.R.layout.simple_list_item_1, p1List);
lvP1.setAdapter(arrayAdapter);
although, this then gives me an error and tells me to take out the 'p1List' string out of the arrayadapter parameters. i dont know why it is doing this.
As other mentioned you are using wrong data type to get the extra from intent.
Replace this line:
String p1List = (getIntent().getStringExtra("p1L"));
With:
ArrayList<String> p1List = getIntent().getStringArrayListExtra("p1L");
basically my problem is this. I am trying to display a list view within a fragment set within the xml file, this list view gets its data from an ArrayList that gets its data from a form I created in another activity using Edit Texts which are put into an ArrayList and passed as a Parcelable Array List to the fragment class.
At the start of the fragment activity there of course are no values stored so I bring up the form activity for data input upon checking that there is no intent stored with the id I specify the passed Parcelable ArrayList as. The problem I have is that whenever I try populate the list view with more than one item (having the first item showing up, returning to the form to add another item in), it seems to only display the one item I add each time and not the items I added previously.
public class MasterFragment extends ListFragment {
private DetailFragment ingredDetails;
private ArrayList<Ingrediants> ingredList = new ArrayList<Ingrediants>();
private ArrayList<String> test2 = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getActivity().getIntent();
Bundle extras = intent.getExtras();
if (extras != null) { //If extras are present
boolean data = extras.getBoolean("ingrediant", true);
if (data) {//Data present from form, retrieve from ArrayList<Ingrediants> and populate the ArrayList<String>
ingredList = getActivity().getIntent().getParcelableArrayListExtra("ingrediant");
test2.add(ingredList.get(i).ingrediantName);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, test2));
}//END OF IF DATA
}//END OF IF EXTRAS
else {//No data found, bring up the FormActivity
Intent intent3 = new Intent(getActivity(), FormActivity.class);
startActivity(intent3);
}//END OF ELSE
}//END OF onCreate()
I think it has something to do with how I am creating the list view and populating it with the ArrayList with the data got from the Parcelable ArrayList intent as it could be recreating the list view each time and only displaying the data supplied to it which in this case will be one item at a time. I've been stuck on this for a while now and was wondering if anyone has any ideas ? Thanks much.
The ArrayList you use (test2) to keep the data does not persist. You can either save the ingredients in sqlite db and call all items onCreate in your fragment, or move the test2 to the main activity and populate items before passing it with Intent bundle.
I have a list view set to multiple choices, my question is is there a way to track what was "checked" by its name?
Intent formed = new Intent(this, formedlist.class);
SparseBooleanArray sp=getListView().getCheckedItemPositions();
String str="";
for(int i=0;i<sp.size();i++) {
str+=names[sp.keyAt(i)]+",";
formed.putExtra("strings", str);
if(names[sp.keyAt(i)].toString().equals(<String>))
startActivity(formed);
}
is this how I should be iterating through the string? and is the code for the next class that supposed to show the newly formed list is
if(names[sp.keyAt(i)].toString().equals(<String>))
//get the Listview from the previous class (this is in a new class seperate from other methods
Intent formed = getIntent();
String [] needed = formed.getStringArrayExtra("strings");
//ArrayAdapter will makes strings appear in the ListView
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_2, needed));`
the way to see what is inside of the list of checked items?
I don't see a problem using this approach. Are you having a problem doing it this way?
You are not creating the intent correctly. To create an Intent for navigating to a separate Activity, use this format
Intent formed = new Intent(this, OtherActivity.class);
If you are never executing inside the if statement, try logging what values you are comparing, or stepping through with the debugger to see where things go awry.
I am making an android app in which i have an activity X that displays a list and a button. Activity X calls a listview to display that list. Each list item has a number(textview) and a checkbox. I used a setonclicklistener on the checkbox, so whenever the checkbox is checked i am storing the number associated with it in a string. Now i want that whenever i click the button the msg activity should start and the numbers to be sent are the ones that are checked.
I am using the following code to start the msg activity in my X activity.
Intent msgIntent = new Intent(Intent.ACTION_VIEW, Uri
.fromParts("sms", msgnumbers, null));
msgIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(msgIntent);
Now "msgsnumbers" variable is present in my listview. How do I pass it to this activity X??
I found the same question here but with no appropriate solution.
-Thanks in advance
Intent in = new Intent(Quote.this, Purchase Ysn.class);
in.putExtra("price", salesprc);
public static String price = "price";
if (getIntent().getExtras().containsKey(price)) {
purces_nbcpy = getIntent().getExtras().getDouble(price);
}
onItemClickListener for ListView has a param position that tells you what position has been clicked.
so if you are using an ArrayList (for eg) to provide values for listItems in adapter you can use this inside onItemClickListener
MyBeanObject object=arraList.get(position);
//use getters of object to retrieve values and pass it as intent
//where arrayList may be your list of objects MyBeanObject