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();
}
});
}
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...
}
}
Related
Suddenly I decided that need to gain android dev experience. I want to create a simple us which will get data from lib and push it to segment with the table view. I'm following the guide https://developer.android.com/guide/fragments/communicate
The problem is that I don't have exceptions but the data in the table(R.id.sdkhash) is not updated. There should be some stupid mistake on my side, but I can't find it.
MainActivity.java
I'm getting (InfoViewModel extends ViewModel) here and push the data via selectItem method.
...
#Override
protected void onCreate(Bundle savedInstanceState) {
...
infoViewModel = new ViewModelProvider(this).get(InfoViewModel.class);
RuntimeInfo runtimeInfo = new RuntimeInfo("A", "B", "C", "D");
infoViewModel.selectItem(runtimeInfo);
InfoViewModel.java
class RuntimeInfo {
public String sdkHash;
public String deviceUid;
public String URL;
public String userAgent;
public RuntimeInfo(String hash, String deviceid, String url, String agent) {
sdkHash = hash;
deviceUid = deviceid;
URL = url;
userAgent = agent;
}
}
InfoViewModel.java I'm defining my ViewModel here.
class RuntimeInfo {
// Dummy class to hold the data.
public String sdkHash;
public String deviceUid;
public String URL;
public String userAgent;
public RuntimeInfo(String hash, String deviceid, String url, String agent) {
sdkHash = hash;
deviceUid = deviceid;
URL = url;
userAgent = agent;
}
}
public class InfoViewModel extends ViewModel {
private final MutableLiveData<RuntimeInfo> selectedItem = new MutableLiveData<RuntimeInfo>();
public void selectItem(RuntimeInfo item) {
selectedItem.setValue(item);
}
public MutableLiveData<RuntimeInfo> getSelectedItem() {
return selectedItem;
}
}
InfoFragment.java - Fragment which is responsible for the UI part. I want to be able to update its UI based on the data from MainActivity.
public class InfoFragment extends Fragment {
...
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
hashRowview = (TextView) view.findViewById(R.id.sdkhash);
infoViewModel = new ViewModelProvider(this).get(InfoViewModel.class);
infoViewModel.getSelectedItem().observe(getViewLifecycleOwner(), item -> {
hashRowview.setText((CharSequence) item.sdkHash);
});
infoViewModel.getSelectedItem();
RuntimeInfo runtimeInfo = infoViewModel.getSelectedItem().getValue();
System.out.println("*************************************** runtimeInfo:" + runtimeInfo);
if (runtimeInfo != null) {
hashRowview.setText((CharSequence) runtimeInfo.sdkHash);
System.out.println("*************************************** runtimeInfo Not NULL");
}
public class SecondActivity2 extends AppCompatActivity {
public static String main_html = "";
public static int second_action = 0;
WebView webView2;
public static taskResearch ndc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
webView2 = (WebView) findViewById(R.id.webView);
String userid = "12345";
try {
ndc = new TaskResearch(userid, "1", "keyword", 5, 4, "orderid", 6, "title", "text");
ndc.execute("");
} catch (Exception e) {
e.printStackTrace();
}
Log.v("htmlg", "taskResearch finished");
}
class TaskResearch extends AsyncTask <String, String, String> {
String deviceId, s_id, keyword, city, s_title, s_text;
int count1, size1, online;
public TaskResearch(String deviceId, String s_id, String keyword, int count1, int size1, String city, int online, String s_title, String s_text){
this.deviceId = deviceId;
this.s_id = s_id;
this.keyword = keyword;
this.city = city;
this.s_title = s_title;
this.s_text = s_text;
this.count1 = count1;
this.size1 = size1;
this.online = online;
}
protected String doInBackground(String... strings) {
String sonuc = "";
runOnUiThread(new Runnable() {
public void run() {
}
});
return "a";
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
class html_reader {
#SuppressWarnings("unused")
#JavascriptInterface
public void processHTML(final String html) {
// Log.d("HTMLG", "html: " + html);
// Log.d("HTMLG", "html: " + html);
// html_reader jssil = new html_reader(deviceId, sId, swAfter, swcity);
// webView2.addJavascriptInterface(jssil, "HTMLOUT");
main_html = html;
}
#SuppressWarnings("unused")
#JavascriptInterface
public void ok() {
// Log.d("HTMLG", "html: " + html);
// Log.d("HTMLG", "html: " + html);
// html_reader jssil = new html_reader(deviceId, sId, swAfter, swcity);
// webView2.addJavascriptInterface(jssil, "HTMLOUT");
second_action = 1;
}
}
Hello Friends.
My codes are above. I want to run this command.
ndc = new taskResearch(userid, "1", "keyword", 5, 4, "orderid", 6, "title", "text");
When this command finished. I want this command to run.
Log.v("htmlg", "taskResearch finished");
But 2 of them start together and second one finishes firstly. I want that first command should start and finish then second command should start. How can i do that ?
What is my mistake ?
By the way, If you are fecthing html code from a website, you can use different structures.
You can use onPostExecute to detect process end.
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
processFinished();
}
void processFinished(){
Log.v("htmlg", "task_research finished");
}
One way to do it is override the onPostExecute method, as mentioned in another post. Another way to do it is as follows:
After you call ndc.execute() you can wait for it to get a result like this:
String output = ndc.get(); // i believe this is the api
Once you are returned that output, you can do whatever you want to it and the second log will print after.
I'm new here, and also new to Android Studio.
My first project is to create a diary to save a users workouts.
I'm having trouble saving for this data..
Here is my front page for the app. With the button "Styrkeboken" (Strengthbook) I'll be entering my sets, reps, weight(vikt) and excercise(övning). I want to save this data in the second button "Historik" (History), but I can't manage it to work as I want to..
Code from my class "Pass" (Workout):
package com.example.mama0086.styrkeboken;
public class Pass {
private int _set;
private int _reps;
private float _vikt;
private String _övning;
public Pass() {
}
public Pass(int set, int reps, float vikt, String övning) {
this._set = set;
this._reps = reps;
this._vikt = vikt; //Vikt = weight
this._övning = övning; //Övning = excercise
}
public void set_set(int _set) {
this._set = _set;
}
public void set_reps(int _reps) {
this._reps = _reps;
}
public void set_vikt(float _vikt) {
this._vikt = _vikt;
}
public void set_övning(String _övning) {
this._övning = _övning;
}
public int get_set() {
return _set;
}
public int get_reps() {
return _reps;
}
public float get_vikt() {
return _vikt;
}
public String get_övning() {
return _övning;
}
}
Code from my class "Historik" (History):
public class Historik extends AppCompatActivity {
EditText editSet;
TextView textSet;
EditText editReps;
TextView textReps;
EditText editVikt;
TextView textVikt;
EditText editÖvning;
TextView textÖvning;
MyDBHandler dbHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_historik);
editSet = (EditText) findViewById(R.id.editSet);
textSet = (TextView) findViewById(R.id.textSet);
editReps = (EditText) findViewById(R.id.editReps);
textReps = (TextView) findViewById(R.id.textReps);
editVikt = (EditText) findViewById(R.id.editVikt);
textVikt = (TextView) findViewById(R.id.textVikt);
editÖvning = (EditText) findViewById(R.id.editÖvning);
textÖvning = (TextView) findViewById(R.id.textÖvning);
dbHandler = new MyDBHandler(this, null, null, 1);
printDatabase();
}
//Add excercise
public void addBtn(View view){
Pass pass = new Pass(textSet.getText().toString()); //Pass is swedish for work-out
dbHandler.addPass(pass);
printDatabase();
}
public void removeBtn(){
String inputText = textSet.getText().toString();
dbHandler.deletePass(inputText);
printDatabase();
}
public void printDatabase(){
String dbString = dbHandler.historikToString();
textSet.setText(dbString);
editSet.setText("");
textReps.setText(dbString);
editReps.setText("");
textVikt.setText(dbString);
editVikt.setText("");
textÖvning.setText(dbString);
editÖvning.setText("");
}
}
In the "Historik" class I get this error message:
"Cannot resolve constructor 'Pass(java.lang.String)'
please let me know if you need the code for my database.
I appreciate the help!
You defined 2 different constructors for your Pass class :
public Pass() {
}
public Pass(int set, int reps, float vikt, String övning) {
this._set = set;
this._reps = reps;
this._vikt = vikt; //Vikt = weight
this._övning = övning; //Övning = excercise
}
However in your Historik activity, you're trying to instantiate a new Pass with a String only as a parameter Pass pass = new Pass(textSet.getText().toString());
You will need to change this line to match one of the 2 constructors you have, or define a 3rd one taking only a String as argument
EDIT : Depending on your Pass object, you can either :
declare a 3rd constructor taking only a String (I assume it's övning)
public Pass(String övning) {
this._övning = övning;
}
if the textSet.getText().toString() is not relevant, simply use the default constructor Pass pass = new Pass();
I have 3 classes.
One of them have the attributes with setters and getters
On my main activity I have the class nameClass = new class
This My main Activity
coffe_cal coffecalo1 = new coffe_cal();
public void addf1(View view){
coffecalo1.setFraps01(true);
coffecalo1.setFraps1(65.00);
showfrappeup();
}
public void checkout(View view){
Intent checkout2 = new Intent (this, check_out.class);
startActivity(checkout2);
}
and I have an onClick event that set my boolean = true but in my third class I have the following
TextView stuff = (TextView) findViewById(R.id.StuffTotal);
String SoutTotal = "";
SoutTotal += coffecalo1.TotalTicket();
stuff.setText(SoutTotal);
}
On my TotalTicket method I have this :
public String TotalTicket(){
String message = "";
if (this.isFraps01()){
message += "Frappuccino moka .................." + this.getFraps1() + "\n\n";
}
return message;
}
This is the onClick event :
public void addf1(View view){
coffecalo1.setFraps01(true);
coffecalo1.setFraps1(65.00);
showfrappeup();
}
When I run my app the TextView doesn't show anything. There 2 different activities by the way.
define the variable as a public static variable shall to the trick.
public class MyActivity {
public static boolean myVar = false;
}
public class AnotherClass {
public void someMethod() {
if (MyActivity.myVar) { // do some work
}
}
Could anyone tell me what is wrong in my code?
I am trying to pass a string to function removeWords() and this function removes some information from the String.
For example if I pass:
"I Have a Headach"
the function should return:
"Headach"
However, my function is not working:
public class WordChosen extends Activity {
private TextView wordsList;
private String symptom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_word_chosen);
//Getting String from VoiceRecognition Activity and displaying it
Intent intent = getIntent();
String wordChosen = intent.getExtras().getString("wordChosen");
//casting the string with TextView to display the result
wordsList = (TextView) findViewById(R.id.wordChosen);
Log.v("Word List:", "+++++"+wordChosen);
//Setting text to be displayed in the textView
removeWords(wordChosen);
Log.v("removewords:", "------- message is displayed");
}
public void removeWords(String wordList)
{
ArrayList<String> stopList = null;
stopList.add("i");
stopList.add("have");
stopList.add("a");
ArrayList<String> result = new ArrayList<String>(Arrays.asList(wordList.split(" ")));
for(int i=0; i<result.size();i++)
{
for(int j=0; j<stopList.size();j++)
{
if (result.get(i).equals(stopList.get(j))) {
break;
}
else {
if(j==stopList.size()-1)
{
wordsList.setText(result.get(i));
}
}
}
}
}
}
public static void main(String[] args) {
String word = "I Have a Headach";
String remove = "I Have a ";
System.out.println(removeWords(word, remove));
}
public static String removeWords(String word ,String remove) {
return word.replace(remove,"");
}
output : Headach