Pull INT from Parse Database Android - java

I'm hoping this will be a fairly simple thing. In my parse database I have an column called seconds. I do not know the proper syntax to pull it as a Int, I can only pull it as a string. Is there a good way to do this? I've looked into conversion methods but none of them address actually taking an int from a position in an adapter. Here is my code. Alternatively if someone can tell me how to convert that string afterwards in a new activity that would be great.
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Send single item click data to SingleItemView Class
Intent i = new Intent(Welcome.this,
MainActivity.class);
// Pass data "name" followed by the position
i.putExtra("Name", ob.get(position).getString("Name")
.toString());
i.putExtra("Time", ob.get(position).getString("Time")
.toString());
i.putExtra("Seconds", ob.get(position).getInt("Seconds")
);
// Open SingleItemView.java Activity
startActivity(i);
}
});
Second Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = getIntent();
btnStart = (Button)findViewById(R.id.btnStart);
btnStop = (Button)findViewById(R.id.btnStop);
textViewTime = (TextView)findViewById(R.id.textViewTime);
Timer = i.getStringExtra("Timer");
textViewTime.setText(Timer);
Name = i.getStringExtra("Name");
Seconds = i.getIntExtra("Seconds");
final CounterClass timer = new CounterClass(180000,1000);
btnStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
timer.start();
}
});
btnStop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
timer.cancel();
}
});
}

You should use "getInt("Seconds")" without "toInt()".

Related

Not able to pass data between activities.Data is blank

I am trying to pass some data from one activity to another and I have made a toast in the other activity to see if data is being passed.When the toast shows up it is blank.I have done everything right and tried many different times with different methods I have also created another app but the problem still stays.It gives me null.
My java code in the first activity
public class titleandcuisine extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
public static final String TITLE_GET = "title";
public static final String CUISINE_GET = "cuisine";
ImageView imageView;
Button button;
Spinner spinner;
EditText dish;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_titleandcuisine);
dish = findViewById(R.id.editText);
spinner = findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.cuisines,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
button = findViewById(R.id.next);
imageView = findViewById(R.id.close);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), homepage.class));
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = dish.getText().toString();
String text2 = spinner.getSelectedItem().toString();
Intent data = new Intent(getBaseContext(),imagepost.class);
data.putExtra(TITLE_GET,text);
data.putExtra(CUISINE_GET,text2);
startActivity(data);
}
});
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Second activity java code
public class reviewactivity extends AppCompatActivity {
TextView cuisine,title;
ImageView displayimage,back;
Uri myUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reviewactivity);
Intent intent = getIntent();
String titleget = intent.getStringExtra(titleandcuisine.TITLE_GET);
String cuisineget = intent.getStringExtra(titleandcuisine.CUISINE_GET);
Toast.makeText(this, titleget + cuisineget, Toast.LENGTH_SHORT).show();
cuisine = findViewById(R.id.reviewcuisine);
title = findViewById(R.id.reviewtitle);
back = findViewById(R.id.backtopostvideo);
displayimage = findViewById(R.id.reviewdisplaimage);
// cuisine.setText(cuisineget);
// title.setText(titleget);
// myUri = Uri.parse(uri);
displayimage.setImageURI(myUri);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),videopost.class));
}
});
}
}
You need to read the text from the EditText when the button is clicked. Something like:
title = dish.getText().toString(); // Add this
Intent data = new Intent(getBaseContext(),imagepost.class);
data.putExtra(Intent.EXTRA_TEXT, title);
...
One more thing: You are trying to read the Intent.EXTRA_TEXT at the reviewactivity activity. However, nothing is starting that activity (at least within the piece of code that you shared).

I can't open a chat Intent by pressing a button

I can't open a new Intent by a button but only in listview I followed a guide but it solved it listview and I need a normal click where is the problem in my code
The code only works with the listview...
buttonadd.setOnClickListener(new AdapterView.OnClickListener(){
#Override
public void onClick(AdapterView<?> parent, View view, int postition, long id){
String curentgruopname = parent.getItemAtPosition(postition).toString();
Intent groupchatintent = new Intent(MainActivity.this , buttonaddactivity.class);
groupchatintent.putExtra("groupname", curentgruopname);
startActivity(groupchatintent);
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int postition, long id) {
String curentgruopname = parent.getItemAtPosition(postition).toString();
Intent groupchatintent = new Intent(MainActivity.this , buttonaddactivity.class);
groupchatintent.putExtra("groupname", curentgruopname);
startActivity(groupchatintent);
}
});
Maybe it works when you initialize the OnClicklistener in the OnCreate Method.
Here is an Example:
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
button = findViewById(R.id.button);
button.setOnClickListener(buttonClick);
}
View.OnClickListener buttonClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(testActivity.this, nextActivity.class);
startActivity(intent);
}
};

spinner value not passing from one activity to another

ViewToken.class:
spinnerGenre = (Spinner) findViewById(R.id.spinnerGenres);
spinnerGenre1 = (Spinner) findViewById(R.id.spinner);
docname = spinnerGenre1.getSelectedItem().toString();
session = spinnerGenre.getSelectedItem().toString();
next=(Button)findViewById(R.id.button4);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent next=new Intent(ViewToken.this,Tokens.class);
next.putExtra("docname", docname.toString());
next.putExtra("session", session.toString());
startActivity(next);
}
});
Tokens.class
Intent i2 = getIntent();
final String docname = i2.getStringExtra("docname");
final String session = i2.getStringExtra("session");
The spinner value from ViewToken.class is not passed to Tokens.class
Change this in ViewToken.class
next.putExtra("docname", docname);
next.putExtra("session", session);
You can do the following in your Tokens.class
Bundle extras = getIntent().getExtras();
String docname = extras.getString("docname");
String session = extras.getString("session");
The calls to getSelectedItem() should be made in the onClick listener so that it gets the most up-to-date values selected.
So instead the onClick() method will be:
#Override
public void onClick(View v) {
docname = spinnerGenre1.getSelectedItem().toString();
session = spinnerGenre.getSelectedItem().toString();
Intent next=new Intent(ViewToken.this,Tokens.class);
next.putExtra("docname", docname);
next.putExtra("session", session);
startActivity(next);
}
It is likely that the calls to getSelectedItem() where being made before anything was selected and therefore the values being put() inside the intent where incorrect.
get the values from the spinners just before send them.
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
docname = spinnerGenre1.getSelectedItem().toString();
session = spinnerGenre.getSelectedItem().toString();
Intent next=new Intent(ViewToken.this,Tokens.class);
next.putExtra("docname", docname.toString());
next.putExtra("session", session.toString());
startActivity(next);
}
});
int positionOfSelectedDataFromSpinner;
iv.putExtra("position", positionOfSelectedDataFromSpinner);
Create a New Method
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
positionOfSelectedDataFromSpinner= i;
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
And In Second Activity
int positionToShowToSpinner = iv.getIntExtra("position",0);
spinner.setSelection(positionToShowToSpinner);

Passing spinner selection between activities

I have 4 activity with spinner inside every activity and this spinner include 3
string data (drop-down selection), when I pass from activity to another one I must pass this selected data inside spinner like if I have chosen data x from the list in the spinner and click in button the selected data must be in second activity spinner as x too.
I read several solutions without any solving. I hope to solve it here and this is my code for spinner and where to put the intent code
public class Page1 extends AppCompatActivity {
Spinner spinner;
ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page1);
spinner = (Spinner)findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this,R.array.film_type,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_LONG).show();
switch (position)
{case 0:
btn[0] = (FloatingTextButton) findViewById(R.id.btn);
btn[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
double thick = Double.valueOf(editText1.getText().toString());
double width = Double.valueOf(editText2.getText().toString());
}
});
break;
case 1:
btn[0] = (FloatingTextButton) findViewById(R.id.btn);
btn[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
} catch (NumberFormatException e) {
//not a double
}
}
});
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
First save the position of selected data from spinner in a String variable,
int positionOfSelectedDataFromSpinner;
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
positionOfSelectedDataFromSpinner= position;
}
Then on button click send intent to Another activity with putExtra
Intent i = new Intent (this, activity2.class);
i.putExtra("position", positionOfSelectedDataFromSpinner);
startActivity(i);
get int from getIntent in another activity
Intent intent = getIntent();
int positionToShowToSpinner = intent.getStringExtra("position");
then set the position to spinner
spinner.setSelection(positionToShowToSpinner);
I think this my solve your problem.
this is the most painless way i can think of:
make a new class or a static member of an existing class but the second solution makes the code less understandable.
public class SpinnerPosHolder{
public static int poition;
}
then in all 4 of them:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
SpinnerPosHolder.position = spinner.getSelectedItemPosition();
For using it:
spinner.setSelection(SpinnerPosHolder.position)
You can do it in this way
String selectedItem=spinner1.getSelectedItem().toString();
And that String you can pass using Intent

How to modify a string array and save it to use for a listview?

public class MainActivity extends AppCompatActivity {
private Button addcar;
String[]foods={"carone","cartwo","carthree"};
#Override
protected void onCreate(Bundle savedInstanceState) {
if(savedInstanceState!=null){ <----- Here is where I thought I could get the updated array to be added to the listview.
String [] foods = savedInstanceState.getStringArray("foods");
ListAdapter myadapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,foods);
ListView mylistview = (ListView)findViewById(R.id.list);
mylistview.setAdapter(myadapter);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addcar = (Button) findViewById(R.id.button);
addcar.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myintent = new Intent("com.example.husse.profilesalgortihm.Main2Activity");
startActivity(myintent);
}
}
);
ListAdapter myadapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,foods);
ListView mylistview = (ListView)findViewById(R.id.list);
mylistview.setAdapter(myadapter);
mylistview.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (id == 0) {
Toast.makeText(MainActivity.this, foods[0], Toast.LENGTH_SHORT).show();
}
}
}
);
}
}
My Second activity
public class Main2Activity extends MainActivity {
public int number = 0;
public EditText Model;
public EditText Body;
public EditText color;
public String M;
public String B;
public String C;
private final String tag = "Main2activity";
private Button add;
Car_information[] cars = new Car_information[10];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Model = (EditText)findViewById(R.id.editText);
Body = (EditText)findViewById(R.id.editText2);
color = (EditText)findViewById(R.id.editText3);
add = (Button)findViewById(R.id.button2);
M = Model.getText().toString();
B = Body.getText().toString();
C = color.getText().toString();
// may run into issues with the final
add.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) { <--- Here is where I tried to save the information about the car but for the listview purpose I only wanted the name to be added to the Listview in the previous activity.
cars[number] = new Car_information();
cars[number].Model = M;
cars[number].Body = B;
cars[number].Color = C;
foods[number]= M;<----- Updating the array with the name the of the vehicle the user put in
number ++;
Intent intent = new
Intent(Main2Activity.this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
);
}
#Override
public void onSaveInstanceState(Bundle outState) { <----- where I tried to save the new updated array to be used in the listview, so the user could see the new listview when the user goes back to the firstactivity.
super.onSaveInstanceState(outState);
outState.putStringArray("foods",foods);
}
What I am trying to do is when the user goes to the second activity he/she will enter in the information about his/her vehicle, but when they click the add button it will take them back to the previous activity and it will save the name of the car that they entered, and display it on the listview. But the list isn't being updated when they go back to the firstactivity.
You can use startActivityForResult() function to implement your task.
Example: You are in Activity1 and want to get data from Activity2.
In Activity1, call intent to start Activity2 with specific request
static final int PICK_CONTACT_REQUEST = 1; // The request code
...
private void pickContact() {
Intent pickContactIntent = new Intent(this, Activity2.class);
pickContactIntent.putExtra(...) <-- put some data to send to Activity2
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
then override onActivityResult like
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// get data sent from Activity2 via data parameter
}
}
}
In Activity2, when you're done with process, send data back to Activity1 by flow
setResult (int resultCode, Intent data) > finish()
this data will send to onActivityResult above
Hope it help !

Categories

Resources