Passing entered values into arrays - android

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";

Related

How would I display edittext field dynamically

I have a string array and I have to display the string array in a edittext box showing one value at a time.
For Instance,
String str = new String{"abc", "def", "ghi"};
I have to show these values in a edittext box such that the value keep changes periodically as soon as a user opens this activity.
Please help me out, thanks in advance.
You can put this string array into parent activity of this activity. And wen you are starting intent of this activity(which contains edittext), put extra values i,e in your case array[i] and then start the activity.
starting intent from parent activity :
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
i.putExtra("STRING_you_NEED", string[i]);
and then get it on your activity that contains editText !
Bundle extras = getIntent().getExtras();
edittext.setText(extras.getString("STRING_you_NEED"))
There are lots of ways to store an index to the string array and retrieve it whenever you want. Depending on the use case, you can try the following,
Use SharedPreferences and use a key-value pair to store the index and retrieve it when you want to show the string in the Activity(Persistent)
Extend your Application(or create a static class) and use a static int variable to store the index(Non-Persistent)

ArrayAdapter wont load string from another activity

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");

Dynamically create pages/activity in Android?

Okay, so i just created an activity that queries post titles from a database and post it on a view using an adapter.
I would like to implement the following:
Once I click the title, I should be taken to a separate page/activity that displays the content section of that title. Similar to a blog app that goes to a blog post when we click on the title.
I'm going through a block right now. How does one get this done?
In Intent you can use add extra e.g id from each:
First: and in activity you can change it Dynamically setContentView(xxx);
xxx: change depend on extra you add previously
Second: you can create standard layout and depend on extra you can fill
e.g id==1 ===> fill textview to "zzzz"
id==2 ===> fill textview to "mmmm"
... and so on
Well actually you're not dynamically creating an activity. The content of your second (detail) activity just depends on the user's input on the first activity.
As soon as the user click's on a title on your first activity, you create a new intent and append the post's to the intent's extras.
When you startup your second activity using this intent, you will be able to get the post's id from the extras and load the content from your database.
FirstActivity
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("POST_ID", <post_id>);
startActivity(i);
SecondActivity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Bundle bundle = getIntent().getExtras();
String postId;
if(bundle != null) {
// process your id.
postId = bundle.getString("POST_ID");
}
}

display each element of an arraylist to setText

I want to display each of the element or value of arraylist to tv.setText.
The values of the arraylist are from the 1stactivity(1st screen) and I want to pass it to the tv.setText of the 2nd activity(2nd screen).
Here's the code of 1st activity
List<String> class_code = new ArrayList<String>();
class_code.add("test");
class_code.add("test2");
Intent intent = new Intent(1stscreen.this,2nd_screen.class);
intent.putStringArrayListExtra("code", (ArrayList<String>) class_code);
Here's the code of 2nd activity
tv.setText(getIntent().getExtras().getString("code"));
But it shows all the value of arraylist (test and test2) I only want to get the first value of the arraylist.
To display only one item from an ArrayList in a TextView, you need to pass only the first item:
List<String> list = getIntent().getExtras().getStringArrayListExtra("code");
tv.setText(list.get(0));
If you only plan to use this one String in the next Activity don't pass the entire ArrayList, only pass the String you want to use:
Intent intent = new Intent(1stscreen.this, 2nd_screen.class);
intent.putString("code", class_code.get(0));
if you want only one value use something like this class_code.get(0)
intent.putString("code", class_code.get(0));
and you can get it from the next activity using
getintent().getExtras().getString("code");
Use this:
List<String> list = getIntent().getStringArrayListExtra("code");
tv.setText(list.get(0));
If you only want to use the first value, then only pass the first value. Instead of
intent.putStringArrayListExtra("code", (ArrayList<String>) class_code);
try putting
intent.putStringExtra("code", class_code.get(0));
I need to forward the value from the 1st screen to the third screen so I solved it using
1st screen
intent.putStringArrayListExtra("class_code", (ArrayList<String>) class_code);
2nd screen
intent.putStringArrayListExtra("class_code", getIntent().getExtras().getStringArrayList("class_code"));
3rd screen
tv.setText(getIntent().getExtras().getStringArrayList("class_code").get(0));

"Tracking" ListView with Multiple choices

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.

Categories

Resources