I have really no idea why it doesn’t show text.
If you look at the class 2, then then I save nimi2 as shared preference. But it doesn’t show up in class 1. I made small test. I replaces name 2 with “plaplapla” (note the “”) and it showed up as plaplapla. But I want it to show up whatever person types as his name. I am new to java. I am yet again really out of ideas :(. Can someone help?
package viimane.voimalus;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TextView;
public class MainStuff extends Activity {
TextView tere;
String nimi;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
tere = (TextView) findViewById(R.id.tvTere);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean nimiOlemas = preferences.getBoolean("nimionolemas", false);
nimi = preferences.getString("nimi2", "");
if (nimiOlemas == false){
startActivityForResult(new Intent(this, nimekysija.class), 0);
finish();
}
if (nimiOlemas == true){
tere.setText("Tere " + nimi);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// get data from shared preference here
super.onActivityResult(requestCode, resultCode, data);
}
}
package viimane.voimalus;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class nimekysija extends Activity {
EditText nimi;
SharedPreferences preferences;
String nimi2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.nimekysija);
preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
nimi = (EditText) findViewById(R.id.etNimekysija);
nimi2 = nimi.getText().toString();
Button kysOk = (Button) findViewById(R.id.bNimekysija);
kysOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences.Editor editor = preferences.edit();
editor.putString("nimi2", nimi2); // nime kirjutamine
editor.putBoolean("nimionolemas", true); // nimi on kirjutatud!
editor.commit();
startActivity(new Intent("viimane.voimalus.MAINSTUFF"));
finish();
}
});
}
}
and XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:text="" android:id="@+id/tvTere" android:layout_width="fill_parent" android:layout_height="wrap_content"></TextView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="Palun sisestage oma nimi!" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"></TextView>
<EditText android:id="@+id/etNimekysija" android:layout_height="wrap_content" android:layout_width="fill_parent">
</EditText>
<Button android:text="OK!" android:id="@+id/bNimekysija" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onAddClick"></Button>
</LinearLayout>
I found solution for my answer. I removed onActivityResult and in 2nd class just made small move. Moved
nimi2 = nimi.getText().toString();into button onClick :).Thank you ntc for help :). I couldn’t wait for your respond so I just searched googled a lot and so on :P.