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.
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.
My program is basically a list of items, and when you click the items it sends you to an expandable list. The problem is, my list of items is hundreds of items long, so it becomes tedious to have to make a different expandable list class for each one. Instead, I want to send different arrays to one subactivity depending on which list option was pressed.
Here's the part of my code in my main activity that is supposed to send the info:
if ((String) ((TextView) view).getText() == "Name of List Item") {
Intent myIntent = new Intent(view.getContext(),
SubActivity.class);
myIntent.putExtra("parents", new String[] { "bunch of parents" });
myIntent.putExtra("children", new String[][] {
{ "bunch" },
{ "of" },
{ "children"} });
startActivityForResult(myIntent, 0); }
Here's the part of my code that is supposed to receive it:
String[] parents; //declared outside onCreate() so they can be used by methods like createParentList()
String[][] children;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = this.getIntent().getExtras();
moves = b.getStringArray("parents");
parents = (String[][]) b.getSerializable("children");
I go on to try to use createParentList()/createChildList(), etc for my expandable list. It compiles perfectly fine. However, whenever I run it and try to click on the list item, it says that the process has stopped unexpectedly. Does anyone know what is wrong?
i think you should see http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/ ,or Transferring object data from one activity to another activity .you should do yourself Serializable
I have a question about populating arrays. In my Android app in one activity I enter the title and the description of my note and I want to add these titles and descriptions to the arrays in another activity respectively. Now it is done in a dummy way, statically. I want to do this dynamically. So, I guess there must be loops and I must be able to add as many notes as I want.
Use Intent mechanism:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("title", title);
intent.putExtra("description", desc);
In your SecondActivity:
Intent intent = getIntent();
array[i++] = new MyElement(intent.getExtra("title"), intent.getExtra("description"));
So you want to pass an entire array to the next activity, is that right? Rather than pass the individual strings, you can pass the entire array with putStringArrayListExtra(). Check here for an example: pass arraylist from one activity to other
Edit: Ok, then. Just extract the relevant strings from the intent, and add it to your existing array:
String newTitle = getIntent().getStringExtra("title");
mTitles.add(newTitle);
Edit2: I see you're using regular arrays, rather than lists. You can't resize arrays, so you need to allocate a new one, one string longer, and copy all the old items across. Something like this:
String[] newTitles = new String[mTitles.length + 1];
for (int i=0;i<mTitles.length;i++) {
newTitles[i]= mTitles[i];
}
mTitles = mNewTitles;
// add the new item
mTitles[mTitles.length-1] = "the string you got from the intent";