Get String Value Dynamically - android

I am passing a value from one activity to another through putExtra() and getExtra() and getting value like:
Passing from
Activity 1:
intent = new Intent(this, blankScrollActivity.class);
intent.putExtra("alphabets", "a");
Recieving at
Activity 2: blankScrollActivity.class
Bundle extras = getIntent().getExtras();
String varName = extras.getString("alphabets");
TextView textView = (TextView) findViewByid(R.id.blankTextView);
String Resource MyString.xml:
<string name="a">My Example 1</string>
<string name="b">My Example 2</string>
I want to get the value of string dynamically to assign textView.

Bundle extras = getIntent().getExtras();
String varName = extras.getString("alphabets");
int resId = getResources().getIdentifier(varName , "string", getPackageName());
String data = getResources().getString(resId);
This way, you will get the String resource that you want based on the String value sent from the first Activity.

Related

How to textview2 getText from String resource by my textView1 is equal string name

"If the value in textView1 is equal string name I want string to show textView2"
// String Example
<string name="a">Apple</string>
<string name="b">Banana</string>
<string name="c">Car</string>
//Example
if textView1 = a
textView2 Will Show Apple
Here you have stringName as a text in your textView1.
You can fetch String value programatically from resourses by stringName.
Step1 : first fetch this both values, and store it in a variable;
String mTvTextStrName = textView1.getText().toString().trim();
String strValue = getStringResourceByName(mTvTextStrName);
use this method to fetch String value.
private String getStringResourceByName(String aString) {
String packageName = getPackageName();
int resId = getResources().getIdentifier(aString, "string", packageName);
return getString(resId);
}
You can also check this values via Log.e.
Step2 : Now set your strValue in your textView2.
// Do your code.. Show Apple
textView2.setText(strValue);
Try using getIdentifier() method of getResources() like below.
TextView textView1= findViewById(R.id.textView1);
TextView textView2= findViewById(R.id.textView2);
String textViewText=getResources().getString(getResources().getIdentifier(textView1.getText().toString(), "string", getPackageName()));
textView2.setText(textViewText);
Referenced from https://stackoverflow.com/a/17086690/9502601

Passing inputs through activities

i was developing an app and this question showed up:
EditText inputCorrect = (EditText) findViewById(R.id.inputCorrect);
EditText inputWrong = (EditText) findViewById(R.id.inputWrong);
EditText inputBlank = (EditText) findViewById(R.id.inputBlank);
EditText inputAll = (EditText)findViewById(R.id.inputAll);
String correctAmountText = inputCorrect.getText().toString();
String wrongAmountText = inputWrong.getText().toString();
String blankAmountText = inputBlank.getText().toString();
String allAmountText = inputAll.getText().toString();
myResultActivty.putExtra("c_a",correctAmountText);
myResultActivty.putExtra("w_a",wrongAmountText);
myResultActivty.putExtra("b_a",blankAmountText);
myResultActivty.putExtra("a_a",allAmountText);
startActivity(myResultActivty);
this is the code there are 4 edit texts with inputType of decimal number
i am getting string from them and send them to other activity.
in the second activity i take those string and turn them into ints with parseint method:
String correctNum = inputActivity.getString("c_a");
String wrongNum = inputActivity.getString("w_a");
String blankNum = inputActivity.getString("b_a");
String allNum = inputActivity.getString("a_a");
float res = 0;
int cN = Integer.parseInt(correctNum);
int wN = Integer.parseInt(wrongNum);
int bN = Integer.parseInt(blankNum);
int aN = Integer.parseInt(allNum);
but whenever i want to do mathematical operations it crashes or shows zero as result.
You could use intents to pass data between activities. In this case, you could create an intent like
Intent intent = Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("key",10);
startActivity(intent);
In SecondActivity,
Bundle extras = getIntent().getExtras();
int yourNum = extras.getInt("key"); // you should get 10

How to use the string from editText as display textview in another activity?

I want to use the string which is typed in edittext as textview in a new activity.
sub6 = (EditText)findViewById(R.id.editText16);
txt6 = (TextView)findViewById(R.id.textView25);
subject = (String) sub6.getText().toString();
txt6.setText(getResources().getString(R.id.editText16));
I have used this code. But is of no use. Can you please help me?
You can send the string using Bundle
Intent i=new Intent(A.this, B.class);
Bundle b = new Bundle();
b.putString("name", sub6.getText().toString());
i.putExtras(b);
startActivity(i);
and in the receiving activity:
Bundle extras = getIntent().getExtras();
String text = extras.getString("name");
Here's how I do it:
EditText sub6 = (EditText) findViewById(R.id.edittext16);
TextView txt6 = (TextView) findViewById(R.id.textview25);
String subject = sub6.getText().toString();
txt6.setText(subject);
You do a lot of unnecessary casts. Like your third line casts a String to a String. Which is useless. Also, declare the object types (like String, EditText, etc.) before declaring them.
In MainActivity,send the string using intent
sub6 = (EditText)findViewById(R.id.editText16);
txt6 = (TextView)findViewById(R.id.textView25);
subject = sub6.getText().toString();
Intent in=new Intent(FirstActivity.this,SecondActivity.class);
in.putExtra("key",subject);
startActivity(in);
In the second Activity recieve intent using getIntent() andthe string using key getStringExtra()
Intent in=getIntent();
String s=in.getStringExtra("key");
txt6.setText(s);

Sending ApplicationInfo from one activity to another

This is how I send it
Intent i = new Intent(v.getContext(), Permissions.class);
i.putExtra("AppSelected",installedApps.get((int) id));
startActivity(i);
THis is how I retrieve it:
Bundle extras = getIntent().getExtras();
ApplicationInfo a = extras.get[What do I put here?]("AppSelected");
I cannot seem to figure out get this to work. Any thoughts?
Call getParcelable() on the Bundle to retrieve your ApplicationInfo (which implements the Parcelable interface). You will need to cast the result to ApplicationInfo.
Assuming installedApps.get... returns an integer, you should change your code
Bundle extras = getIntent().getExtras();
for
int appId = getIntent().getIntExtra("AppSelected", 0);
to retrieve any extra just use the key that you use to save value plus a default in some case
example
Bundle extras = getIntent().getExtras();
int example = extras.getIntExtra("key", int defaultValue) ;//to retrieve an Integer
String example = extras.getStringExtra("key"); //to retrieve a String
.....son so forth
in your case
Bundle extras = getIntent().getExtras();
ApplicationInfo a = extras.getIntExtra("AppSelected", 0);

Problems with putExtra and putStringArrayList

I'm trying to send some data from one activity to another and it's sorta working but not like I want to work.
Problem 1-Things are getting mixed up. On the Next Activity part of the listitem is going to an incorrect textView and part to the correct textview.
Problem 2- I am only able to list 1 item on the new activity but I want to be able to send multiple listitems. I think the problem lies in combining different types of putExtra request to the same place like I do here.
.putExtra("inputPrice",(CharSequence)pick)
.putStringArrayListExtra("list", listItems)
Ant help would be appreciated.
Sending Data to next Activity
final TextView username =(TextView)findViewById(R.id.resultTextView);
String uname = username.getText().toString();
final TextView uplane =(TextView)findViewById(R.id.inputPrice);
String pick = uplane.getText().toString();
final TextView daplane =(TextView)findViewById(R.id.date);
String watch = daplane.getText().toString();
startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",(CharSequence)watch)
.putExtra("Card Number",(CharSequence)uname)
.putExtra("inputPrice",(CharSequence)pick)
.putStringArrayListExtra("list", listItems)
);
finish();
This is the Next Activity
Intent is = getIntent();
if (is.getCharSequenceExtra("Card Number") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleRccn);
setmsg.setText(is.getCharSequenceExtra("Card Number"));
}
Intent it = getIntent();
if (it.getCharSequenceExtra("date") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleTime);
setmsg.setText(it.getCharSequenceExtra("date"));
}
Intent id1 = getIntent();
if (id1.getCharSequenceExtra("inputPrice") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleName);
setmsg.setText(id1.getCharSequenceExtra("inputPrice"));
}
ArrayList<String> al= new ArrayList<String>();
al = getIntent().getExtras().getStringArrayList("list");
saleNotes= (TextView) findViewById(R.id.saleNotes);
saleNotes.setText(al.get(0));
Alright, a few things:
First of all you do not need to cast your strings as CharSequence.
Second thing,
Define intent, add your extras and only then call startActivity as below:
Intent intent = new Intent(MenuView1Activity.this,RecordCheckActivity.class);
intent.putExtra("date", watch);
startActivity(intent);
Third, when retrieving the intent create a bundle first as below:
Bundle extras = getIntent().getExtras();
String date = extras.getString("date");
EDIT:
Here is how you convert your entire array list to one single string and add it to your textview.
String listString = "";
for (String s : al)
{
listString += s + "\t"; // use " " for space, "\n" for new line instead of "\t"
}
System.out.println(listString);
saleNotes.setText(listString);
Hope this helps!
Try this, Don't use CharSequence just put string value
startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",watch)
.putExtra("Card Number",uname)
.putExtra("inputPrice",pick)
.putStringArrayListExtra("list", listItems)
);
And get like this
Intent is = getIntent();
if (is.getCharSequenceExtra("Card Number") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleRccn);
setmsg.setText(is.getStringExtra("Card Number"));
}

Categories

Resources