i try to use encapsulation (setter getter) in my file java in my project android. but it not work, i don't know why. can you help me? i'll give my code
it's my setter and getter in Tahsin1_Jurus6.java
public class Tahsin1_Jurus6 extends Activity {
String judul;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.tahsin1_jurus6);
}
public String getJudul() {
return judul;
}
public void setJudul(String judul) {
this.judul = judul;
}
public void contoh_ba(View view) {
setJudul("Contoh huruf Ba");
Intent tah1_jurus6_contoh = new Intent(Tahsin1_Jurus6.this,
Tahsin1_Jurus6_Contoh.class);
startActivity(tah1_jurus6_contoh);
this.finish();
}
and in this class, i try to call value of "judul", and i want set my "textView1" with value of "judul"
public class Tahsin1_Jurus6_Contoh extends Activity {
TextView judul;
ImageView contoh;
String nama_judul = "dudud", nama_gambar = "";
Tahsin1_Jurus6 t1j6 = new Tahsin1_Jurus6();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tahsin1_jurus6_contoh);
judul = (TextView) findViewById(R.id.textView1);
contoh = (ImageView) findViewById(R.id.imageView1);
nama();
setNama_judul();
}
public void nama() {
nama_judul = t1j6.getJudul();
judul.setText(nama_judul);
judul.setTextSize(20);
}
you need to create New Class for getters/setters
public class JudulData
{
string judul;
public String getJudul() {
return judul;
}
public void setJudul(String judul) {
this.judul = judul;
}
}
Use it by creating instance of class
JudulData jd=new JudulData();
//getting
jd.getJudul();
//setting
jg.setJudul("stringvalue");
If you want to parse data beetween 2 activity in Android, you need to use Intent.
In your code just modify the Intent in Tahsin1_Jurus6 like this
Intent tah1_jurus6_contoh = new Intent(Tahsin1_Jurus6.this,
Tahsin1_Jurus6_Contoh.class);
tah1_jurus6_contoh.putExtra("name", ""+getJudul()); //the keyword is name
startActivity(tah1_jurus6_contoh);
And in the Tahsin1_Jurus6_Contoh you should get the value like this
@Override
protected void onCreate(Bundle savedInstanceState) {
...
String judul = getIntent().getStringExtra("name"); //get value by keyword
...
}
Hope this help
You need to create a separate class as rajan ks wrote. Your above code is written in an android activity . Create a new java class file (this does not come with an XML layout file) and do the above.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.