in one activity I save data into the sharedpreferences via:
public void createIt() {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
Context context = getApplicationContext();
EditText editTextAge = (EditText) findViewById(R.id.editTextAge);
String s = editTextAge.getText().toString();
age = Integer.valueOf(s);
if (age < 16) {
setWeightValues();
setRepValues();
switch (workoutPref) {
case "fitness":
//mat exercises
editor.putString("sprints", "15" );
editor.putString("jogs", "10");
editor.putString("walks", "10" );
break;
}
}
editor.commit();
launchNextactivity();
}
--
and in this new activity, I display 3 specific items from the memory and display them in a textview with this code inside onCreate()
public class WorkoutActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_workout);
//pagetabview 3 days 3 workouts
TextView txt1 = (TextView) findViewById(R.id.txt1);
String t1 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("Star Jumps", "nothing");
txt1.setText(t1);
TextView txt2 = (TextView) findViewById(R.id.txt1);
String t2 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("Push Ups", "nothing");
txt1.setText(t2);
TextView txt3 = (TextView) findViewById(R.id.txt1);
String t3 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("Sit Ups", "nothing");
txt1.setText(t3);
TextView txt4 = (TextView) findViewById(R.id.txt1);
String t4 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("Plank", "nothing");
txt1.setText(t4);
TextView txt5 = (TextView) findViewById(R.id.txt1);
String t5 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("Rowing", "nothing");
txt1.setText(t5);
TextView txt6 = (TextView) findViewById(R.id.txt1);
String t6 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("Treadmill", "nothing");
txt1.setText(t6);
}
for some reason, it only puts "nothing" in the first textview, and then doesn't even change the other text views they stay as default. please can someone help, im not sure if its my sharedpreference or my way of setting textview
The key you have used for set and get values from shared preference should be the same.
You have not change textview value when finding textviews.
TextView txt1 = (TextView) findViewById(R.id.txt1);
TextView txt2 = (TextView) findViewById(R.id.txt2);
TextView txt3 = (TextView) findViewById(R.id.txt3);
TextView txt4 = (TextView) findViewById(R.id.txt4);
TextView txt5 = (TextView) findViewById(R.id.txt5);
TextView txt6 = (TextView) findViewById(R.id.txt6);
base from your code on putting data, this is how you get the data
getDefaultSharedPreferences(getApplicationContext()).getString("sprints", "nothing");
Related
Problem: My app crashed whenever the comment part is removed. But after I had tried to debug the problem seemed to be at line priceTextViews[i].setText(price[i]). Can anyone help to point out the problem? Thank you
TextView[] quantityTextViews;
TextView[] priceTextViews;
int[] unitPrice = {5, 15, 40, 20, 25, 15, 20, 25, 10, 20};
int[] price = new int[10];
int[] quantity = new int[10];
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.balance_page);
textView();
Intent intent = getIntent();
String[] quantityOfItemString = intent.getStringArrayExtra (ShoppingPageActivity.EXTRA_MESSAGE);
for (int i = 0; i < quantityTextViews.length; i++) {
quantityTextViews[i].setText(quantityOfItemString[i]);
quantity[i] = Integer.parseInt(quantityOfItemString[i]);
price[i]= unitPrice[i]* quantity[i];
}
/*for (int i = 0; i < priceTextViews.length; i++) {
price[i]= unitPrice[i]* quantity[i];
priceTextViews[i].setText(price[i]);
}*/
}
public void textView() {
quantityTextViews = new TextView[]{
(TextView) findViewById(R.id.textView49),
(TextView) findViewById(R.id.textView48),
(TextView) findViewById(R.id.textView47),
(TextView) findViewById(R.id.textView46),
(TextView) findViewById(R.id.textView45),
(TextView) findViewById(R.id.textView44),
(TextView) findViewById(R.id.textView43),
(TextView) findViewById(R.id.textView42),
(TextView) findViewById(R.id.textView41),
(TextView) findViewById(R.id.textView40),
};
priceTextViews = new TextView[]{
(TextView) findViewById(R.id.textView51),
(TextView) findViewById(R.id.textView52),
(TextView) findViewById(R.id.textView53),
(TextView) findViewById(R.id.textView54),
(TextView) findViewById(R.id.textView55),
(TextView) findViewById(R.id.textView56),
(TextView) findViewById(R.id.textView57),
(TextView) findViewById(R.id.textView58),
(TextView) findViewById(R.id.textView59),
(TextView) findViewById(R.id.textView50),
};
}
setText requires a string. The setText method that does accept an int needs to correspond to a text resource within your application.
priceTextViews[i].setText(Integer.toString(price[i]));
When you're using setText with int argument it automatically use setText(#StringRes int resid) instead of void setText(CharSequence text), so cast your argument to String or similar CharSequence type.
Easiest solution is
priceTextViews[i].setText("" + price[i]);
I have an Array List that i am getting Values from the EditText widget and placing them inside an Array List
I would like to do a Calculation and output the total to a Text View
The Program is Running fine it just crashes when i hit Calculate..
ArrayList<String> score = new ArrayList<String>();
public class MyActivity extends Activity {
EditText T1, T2, T3, T4, T5, T6, T7, T8, T9;
TextView total;
int sum = 0;
Button ButtonClear, ButtonScore;
ArrayList<String> score = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
T1 = (EditText) findViewById(R.id.editText2);
T2 = (EditText) findViewById(R.id.editText3);
T3 = (EditText) findViewById(R.id.editText4);
T4 = (EditText) findViewById(R.id.editText5);
T5 = (EditText) findViewById(R.id.editText6);
T6 = (EditText) findViewById(R.id.editText7);
T7 = (EditText) findViewById(R.id.editText8);
T8 = (EditText) findViewById(R.id.editText9);
T9 = (EditText) findViewById(R.id.editText10);
total = (TextView) findViewById(R.id.textView28);
ButtonScore = (Button) findViewById(R.id.buttonScore);
ButtonClear = (Button) findViewById(R.id.buttonClear);
score.add(T1.getText().toString());
score.add(T2.getText().toString());
score.add(T3.getText().toString());
score.add(T4.getText().toString());
score.add(T5.getText().toString());
score.add(T6.getText().toString());
score.add(T7.getText().toString());
score.add(T8.getText().toString());
score.add(T9.getText().toString());
public void Calculate(View v)
{
for (int i = 0; i < score.size(); i++)
{
sum = Integer.valueOf(score.get(i));
}
total.setText(sum);
}
You are setting integer value to textview therefore it crash,So change
total.setText(sum);
to
total.setText(String.valueOf(sum));
And to calculate sum do like
sum += Integer.valueOf(score.get(i));
In My App i have 10 Edittext and 10 TextView
I have created instance for the both EditText and TextView like
String[] messageText=new String[10];
String[] messageEdit=new String[10];
TextView oTextView1 = (TextView) findViewById(R.id.Tab_common1_EditText);
TextView oTextView2 = (TextView) findViewById(R.id.Tab_common2_EditText);
TextView oTextView3 = (TextView) findViewById(R.id.Tab_common3_EditText);
TextView oTextView4 = (TextView) findViewById(R.id.Tab_common4_EditText);
TextView oTextView5 = (TextView) findViewById(R.id.Tab_common5_EditText);
TextView oTextView6 = (TextView) findViewById(R.id.Tab_common6_EditText);
TextView oTextView7 = (TextView) findViewById(R.id.Tab_common7_EditText);
TextView oTextView8 = (TextView) findViewById(R.id.Tab_common8_EditText);
TextView oTextView9 = (TextView) findViewById(R.id.Tab_common9_EditText);
TextView oTextView10 = (TextView) findViewById(R.id.Tab_common10_EditText);
EditText oEditTextHiden1 = (EditText) findViewById(R.id.Tab_Hidencommon1_EditText);
EditText oEditTextHiden2 = (EditText) findViewById(R.id.Tab_Hidencommon2_EditText);
EditText oEditTextHiden3 = (EditText) findViewById(R.id.Tab_Hidencommon3_EditText);
EditText oEditTextHiden4 = (EditText) findViewById(R.id.Tab_Hidencommon4_EditText);
EditText oEditTextHiden5 = (EditText) findViewById(R.id.Tab_Hidencommon5_EditText);
EditText oEditTextHiden6 = (EditText) findViewById(R.id.Tab_Hidencommon6_EditText);
EditText oEditTextHiden7 = (EditText) findViewById(R.id.Tab_Hidencommon7_EditText);
EditText oEditTextHiden8 = (EditText) findViewById(R.id.Tab_Hidencommon8_EditText);
EditText oEditTextHiden9 = (EditText) findViewById(R.id.Tab_Hidencommon9_EditText);
EditText oEditTextHiden10 = (EditText) findViewById(R.id.Tab_Hidencommon10_EditText);
now i need to get all the TextView getText() and EditText getText()
I have try some method but it is not working
for (int i = 0; i < 10; i++) {
messageText[i] = (oTextView+i).getText().toString();//it says oTextView cannot be resolved to a variable
messageEdit[i] = (oEditTextHiden+i).getText().toString();
}
Log.i("message", "message :"+Arrays.deepToString(messageText)+" "+Arrays.deepToString(messageEdit));
Store the ids of the EditText and loop over them, since the View ids are integers, you can do that easily like so :
ArrayList<String> values = new ArrayList<String>();
int[] ids = new int[]{R.id.editText1,R.id.editText2,R.id.editText3};//and so on
for(int id : ids){
EditText t = (EditText) findViewById(id);
values.add(t.getText().toString());
}
Or if you only want the text and don't want to do anything else with the EditText view itself, you could make it in one line this:
for(int id : ids) values.add(((EditText)findViewById(id)).getText().toString());
Add all TextView and EdiText in Array Like this
TextView[] oTextView = { oTextView1, oTextView2, oTextView3,
oTextView4, oTextView5, oTextView6, oTextView7, oTextView8,
oTextView9, oTextView10 };
EditText[] oEditTextHiden = { oEditTextHiden1, oEditTextHiden2,
oEditTextHiden3, oEditTextHiden4, oEditTextHiden5,
oEditTextHiden6, oEditTextHiden7, oEditTextHiden8,
oEditTextHiden9, oEditTextHiden10 };
Then getText() Should look like this
for (int i = 0; i < 10; i++) {
messageText[i] = oTextView[i].getText().toString();
messageEdit[i] = oEditTextHiden[i].getText().toString();
}
String[] messageText=new String[10];
String[] messageEdit=new String[10];
TextView[] textViews = new TextView[10];
textViews[0] = (TextView) findViewById(R.id.Tab_common1_EditText);
textViews[1] = (TextView) findViewById(R.id.Tab_common2_EditText);
textViews[2] = (TextView) findViewById(R.id.Tab_common3_EditText);
textViews[3] = (TextView) findViewById(R.id.Tab_common4_EditText);
textViews[4] = (TextView) findViewById(R.id.Tab_common5_EditText);
textViews[5] = (TextView) findViewById(R.id.Tab_common6_EditText);
textViews[6] = (TextView) findViewById(R.id.Tab_common7_EditText);
textViews[7] = (TextView) findViewById(R.id.Tab_common8_EditText);
textViews[8] = (TextView) findViewById(R.id.Tab_common9_EditText);
textViews[9] = (TextView) findViewById(R.id.Tab_common10_EditText);
EditText[] editTexts = new EditText[10];
editTexts[0] = (EditText) findViewById(R.id.Tab_Hidencommon1_EditText);
editTexts[1] = (EditText) findViewById(R.id.Tab_Hidencommon2_EditText);
editTexts[2] = (EditText) findViewById(R.id.Tab_Hidencommon3_EditText);
editTexts[3] = (EditText) findViewById(R.id.Tab_Hidencommon4_EditText);
editTexts[4] = (EditText) findViewById(R.id.Tab_Hidencommon5_EditText);
editTexts[5] = (EditText) findViewById(R.id.Tab_Hidencommon6_EditText);
editTexts[6] = (EditText) findViewById(R.id.Tab_Hidencommon7_EditText);
editTexts[7] = (EditText) findViewById(R.id.Tab_Hidencommon8_EditText);
editTexts[8] = (EditText) findViewById(R.id.Tab_Hidencommon9_EditText);
editTexts[9] = (EditText) findViewById(R.id.Tab_Hidencommon10_EditText);
for (int i = 0; i < 10; i++) {
messageText[i] = textViews[i].getText().toString();
messageEdit[i] = editTexts[i].getText().toString();
}
Log.i("message", "message :"+Arrays.deepToString(messageText)+" "+Arrays.deepToString(messageEdit));
The best way to do this is to use the tag associated with the textview.
For example :
In Xml you need to create TextView and editText as following :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/llParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="tv1"
android:text="TextView1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="et1"
android:text="EditText1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="tv2"
android:text="TextView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="et2"
android:text="EditText2" />
===== Similarly 10 Textviews and EditTexts========
</LinearLayout>
In Code :
String[] messageText = new String[10];
String[] messageEdit = new String[10];
LinearLayout llParent = (LinearLayout) findViewById(R.id.llParent);
for (int i = 1; i <= 10; i++) {
messageText[i - 1] = ((TextView) llParent.findViewWithTag("tv" + i))
.getText().toString();
messageEdit[i - 1] = ((EditText) llParent.findViewWithTag("et" + i))
.getText().toString();
}
Happy Coding.... :-)
Try Like This :-
ArrayList<TextView> arrayListTextViews = new ArrayList<TextView>();
TextView oTextView1 = (TextView) findViewById(R.id.Tab_common1_EditText);
TextView oTextView2 = (TextView) findViewById(R.id.Tab_common2_EditText);
TextView oTextView3 = (TextView) findViewById(R.id.Tab_common3_EditText);
TextView oTextView4 = (TextView) findViewById(R.id.Tab_common4_EditText);
TextView oTextView5 = (TextView) findViewById(R.id.Tab_common5_EditText);
TextView oTextView6 = (TextView) findViewById(R.id.Tab_common6_EditText);
TextView oTextView7 = (TextView) findViewById(R.id.Tab_common7_EditText);
TextView oTextView8 = (TextView) findViewById(R.id.Tab_common8_EditText);
TextView oTextView9 = (TextView) findViewById(R.id.Tab_common9_EditText);
TextView oTextView10 = (TextView) findViewById(R.id.Tab_common10_EditText);
arrayListTextViews.add(oTextView1);
arrayListTextViews.add(oTextView2);
arrayListTextViews.add(oTextView3);
arrayListTextViews.add(oTextView4);
arrayListTextViews.add(oTextView5);
arrayListTextViews.add(oTextView6);
arrayListTextViews.add(oTextView7);
arrayListTextViews.add(oTextView8);
arrayListTextViews.add(oTextView9);
arrayListTextViews.add(oTextView10);
and then access textviews from arrayListTextViews in the loop.
Also do the same for Your Edittext.
the best way is to use the getIdentifier method in the Resources class
something like that
messageText[i] = (TextView) getActivity().findViewById(getResources().getIdentifier("Tab_common"+i+"_EditText", "id", context.getPackageName()));
messageEdit[i] = (EditText) getActivity().findViewById(getResources().getIdentifier("Tab_Hidencommon"+i+"_EditText", "id", context.getPackageName()));
I used the answer to this question to get the basic value of the TextView clicks into a single TextView .This code gives a per click value, I want to know how to add the existing value of TextView.
Note: m1aa and m1ab are number variables in a previous activity and received through intent on this one.
UPDATE This question should have been "how do you convert a string to a decimal."
//Original non working code.
private int productTotal1;
private int productTotal2;
private int overallTotalproduct;
private TextView m1a,
private TextView m1aa,
//and then way down the page
productTotal1 = 0;
productTotal2 = 0;
overallTotalproduct = 0;
final TextView textViewtotalproduct = (TextView) findViewById(R.id.total);
final TextView textView1 = (TextView) findViewById(R.id.m1aa);
final TextView textView2 = (TextView) findViewById(R.id.m1ab);
textView1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
productTotal1++;
overallTotalproduct = productTotal1 + productTotal2;
textView1.setText(String.valueOf(productTotal1));
textViewtotalproduct.setText(String.valueOf(overallTotalproduct));
}
});
textView2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
productTotal2++;
overallTotalproduct = productTotal1 + productTotal2;
textView2.setText(String.valueOf(productTotal2));
textViewtotalproduct.setText(String.valueOf(overallTotalproduct));
}
});
***UPDATE***WORKS FINE :)
AFTER GETTING HELP AND "THINKING" ABOUT WHAT I WAS WRITING I END UP WITH...
//Working version
private double overallTotalproduct;
final TextView textViewtotalproduct = (TextView) findViewById(R.id.total);
final TextView textView1 = (TextView) findViewById(R.id.m1aa);
final String stringm1aa = textView1.getText().toString();
final double intm1aa = Double.parseDouble(stringm1aa);
final TextView textView2 = (TextView) findViewById(R.id.m1ab);
final String stringm1ab = textView2.getText().toString();
final double intm1ab = Double.parseDouble(stringm1ab);
You can use following code to get a value of TextView and convert it to integer
String s = textView.getText().toString();
int n = Integer.parseInt(s);
Remember NumberFormatException will be thrown if the string does not contain a parsable integer.
I'm getting this error ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2585 when i start an intent
public class SingleMenuItemActivity extends Activity {
TextView lblName;
TextView lblyest;
TextView lbltoday;
TextView lblHigh;
TextView lblLow;
TextView lblChange;
TextView lblPcchange;
String name,yesterday,today,high,low,change,pcchange;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
lblName = (TextView) findViewById(R.id.name);
lblyest = (TextView) findViewById(R.id.yesterday);
lbltoday = (TextView) findViewById(R.id.today);
lblHigh = (TextView) findViewById(R.id.high);
lblLow = (TextView) findViewById(R.id.low);
lblChange = (TextView) findViewById(R.id.change);
lblPcchange = (TextView) findViewById(R.id.pcchange);
Intent in = getIntent();
name = in.getStringExtra("name");;
yesterday = in.getStringExtra("yesterday");
today =in.getStringExtra("today");
high =in.getStringExtra("high");
low = in.getStringExtra("low");
change = in.getStringExtra("change");
pcchange =in.getStringExtra("pcchange");
//Error doesnt happen when the lines below are commented
lblName.setText((CharSequence)name.toString());
lblyest.setText(yesterday.toString());
lbltoday.setText(today.toString());
lblHigh.setText(high.toString());
lblLow.setText(low.toString());
lblChange.setText(change.toString());
lblPcchange.setText(pcchange.toString());
}
}
I know the error is triggered when i do the settext() function but i don't know how to fix it
You're setting variables, lblName and all those, to contain all of the child View's ... but you're doing it at the class level. Those Views are not created until the layout is inflated, in the line that reads
setContentView(R.layout.single_list_item);
, so all those variables are getting set to null.
Set the variables after your setContentView() call and see how it goes.
Try this code.
public class SingleMenuItemActivity extends Activity {
TextView lblName;
TextView lblyest;
TextView lbltoday;
TextView lblHigh;
TextView lblLow;
TextView lblChange;
TextView lblPcchange;
String name, yesterday, today, high, low, change, pcchange;
#Override
protected void onCreate (Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate( savedInstanceState );
setContentView( R.layout.single_list_item );
Intent in = getIntent();
name = in.getStringExtra( "name" );
;
yesterday = in.getStringExtra( "yesterday" );
today = in.getStringExtra( "today" );
high = in.getStringExtra( "high" );
low = in.getStringExtra( "low" );
change = in.getStringExtra( "change" );
pcchange = in.getStringExtra( "pcchange" );
lblName = (TextView) findViewById( R.id.name );
lblyest = (TextView) findViewById( R.id.yesterday );
lbltoday = (TextView) findViewById( R.id.today );
lblHigh = (TextView) findViewById( R.id.high );
lblLow = (TextView) findViewById( R.id.low );
lblChange = (TextView) findViewById( R.id.change );
lblPcchange = (TextView) findViewById( R.id.pcchange );
lblName.setText( (CharSequence) name.toString() );
/* lblyest.setText(yesterday.toString());
lbltoday.setText(today.toString());
lblHigh.setText(high.toString());
lblLow.setText(low.toString());
lblChange.setText(change.toString());
lblPcchange.setText(pcchange.toString()); */
}
}
Actually, You can not initialize views before setContentView() call. That's why, you are getting this issue.
move these lines after setContentView(R.layout.single_list_item);
TextView lblName = (TextView) findViewById(R.id.name);
TextView lblyest = (TextView) findViewById(R.id.yesterday);
TextView lbltoday = (TextView) findViewById(R.id.today);
TextView lblHigh = (TextView) findViewById(R.id.high);
TextView lblLow = (TextView) findViewById(R.id.low);
TextView lblChange = (TextView) findViewById(R.id.change);
TextView lblPcchange = (TextView) findViewById(R.id.pcchange);