display each element of an arraylist to setText - android

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

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)

Android: How to edit my ListView in another activity and return the edited result to the ListView

So I have a listview in my activity1 class, what i want to do is:
Click on an item in the listview, which will open activity2 class, with 2 edit texts with the values from the clicked item in the listview, like name and age strings, I want to edit those values/strings in my activity2 class [by changing the edit texts], and send the edited values back to my listview in activity1 class, and show the edited values in my listview [for example show the name] instead of showing the old value/string that was in the listview before the edit.
I have tried many different ways, and I couldn't accomplish the goal, I would love if any of you could help me.
Thank you,
You can transfer your selected items data to your second activity like this code below
Intent i = new Intent(MainActivity.this,ReportActivity.class);
i.putExtra("MainDate", MainDate.getText().toString());
and in your second activity you have to get this data's and then manipulate them
Intent intent = getIntent();
MainDate = intent.getExtras().getString("MainDate");
then send back your manipulated data's to your first Activity just like before then update your list adapter
you can have data of your selected item in your first activity list by this code which lies on yourList.setOnItemClickListener
Cursor Getdata = (Cursor)YourList.getItemAtPosition(position);
String Yourcolumnstring = Getdata.getString(Getdata.getColumnIndex("yourcolumnindex")) ;
you should send your strings to activity2 and when retun from activity2 to activity1 should send your new strings from activity2 to activity1.you should your strings send as following:
String str = "My Data"; //Data you want to send
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("name",str);
you should receive str in activity2 as following:
String name = this.getIntent().getStringExtra("name");

spinner with no select option

I'm trying to create spinner which should not have any select but instead of it, it should show Blank, after clicking that items can be selected.
Here is my code, please help.
urineGlucoseSpinner = (Spinner) view.findViewById(R.id.spnner_urine_glucose);
ArrayList<String> ugList = new ArrayList<String>();
ugList.add(0,"");
ugList.add("1.5");
ugList.add("5.5");
ugList.add("0.8");
ugList.add("9.5");
ugList.add("12.0");
//ArrayAdapter<String> urineGlucoseAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, ugList);
ArrayAdapter<String> urineGlucoseAdapter = new ArrayAdapter<String>(getActivity(),R.layout.custom_spinner_text, ugList);
urineGlucoseAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
urineGlucoseSpinner.setAdapter(urineGlucoseAdapter);
urineGlucoseSpinner.setSelection(0);
urineGlucoseSpinner.setOnItemSelectedListener(new OnUGItemSelected());
By default spinner takes array 0th element if u not selecting any one..u have to make object of ArrayList and for 0th element u have to put "" (null Sting) inside semicolon and make it as 0th element...i think this is the only solution for your question..
ArrayList<String> ugList = new ArrayList<String>();
ugList.add("");
I can see two ways to do this.
1) Add the blank line to your data at position 0, and then create a custom spinner adapter and override the getView method and in it use an if to set the 0 position view to GONE (thus getting rid of the blank line in the listing).
2) An alternative might be setting an empty EditText in your form, and when it gains focus pop a listview in a dialog with your possible choices.

passing data from a view to an activity

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

Passing entered values into arrays

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

Categories

Resources