Reputation: 225
I just made my first POJO Class, which takes two Strings and one String array and combines them into one (for lack of a better term) package. Everything seemed to be working well until the very last part. Here is my code:
class dataPOJO extends Phone_Save {
String fName;
String cName;
String[] cText;
public dataPOJO() {
fName = file_name;
cName = cname;
cText = ctext;
}
public String getName() {
return cName;
}
public void setName(String cName) {
this.cName = cName;
}
public String getFileName() {
return fName;
}
public void setFileName(String fName) {
this.fName = fName;
}
public String[] getRemainder() {
return cText;
}
public void getRemainder(String[] cText) {
this.cText = cText;
}
public String toString() {
String savePackage = "File Name: " + getFileName() + "\n";
savePackage += "CName: " + getName() + "\n";
savePackage += getRemainder() + "\n";
return savePackage;
}
public static void main(String[] args) {
dataPOJO combine = new dataPOJO();
combine.setName(cName);
combine.setFileName(fName);
combine.setRemainder(cText);
System.out.println(combine);
}
}
Unfortunately, I cannot setName as any of the strings I referenced above. To me, it seems like I should be able to. And I can't put a default name in there like:
combine.setName("default name");
because the name comes from user input and is different every time. My question is how can I set the value of the String array below to the strings like cName referenced above? Thanks! Added code
public class Phone_Save extends ActionBarActivity {
String[] ctext;
public int doc_id = 0;
String cname;
Button fileName;
Button loadText;
String file_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone_save);
Intent save_intent = getIntent();
Bundle bundle = save_intent.getExtras();
EditText file_name_edit = (EditText) findViewById(R.id.editText);
final TextView loaded_text = (TextView)findViewById(R.id.loaded_text);
file_name = file_name_edit.getText().toString();
fileName = (Button) findViewById(R.id.fName);
loadText = (Button) findViewById(R.id.loadText);
if (bundle != null) {
ctext = bundle.getStringArray("confirmed_text");
cname = bundle.getString("confirmed_name");
}
fileName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new dataPOJO();
}
});
}
Upvotes: 0
Views: 147
Reputation: 38605
First, dataPOJO
should not be extending Phone_Save
. There's nothing dataPOJO
needs to do that depends on it being an Activity
(at least based on what you've shown us). If you want to have that class to store those information in one place, that's fine; otherwise just store them in Phone_Save
itself. Right now you appear to be trying to do both at once, which is confusing.
Second, calling new dataPOJO()
in the OnClickListener allocates an object, then discards it since it is not being assigned to anything. It's also a completely different object and has no effect on this one.
Third, there's no use for that main
method on Android, it will never be called (unless you call it yourself, which would be pointless anyway).
I would make dataPOJO its own class that doesn't extend anything and get rid of the no-arg constructor (or leave it but remove the code that's in there). In your Phone_Save
's onCreate
method, you can just make a dataPOJO and hang onto it.
public class DataPojo {
String fName;
String cName;
String[] cText;
public dataPOJO() {}
/* all the other stuff */
}
public class Phone_Save extends ActionBarActivity {
private DataPojo dataPojo;
// other members
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dataPojo = new DataPojo();
Intent intent = getIntent();
dataPojo.setName(intent.getStringExtra("confirmed_name"));
dataPojo.setRemainder(intent.getStringExtra("confirmed_text"));
// other stuff...
}
}
Upvotes: 1