I have made an application where the user enters text into a textbox which gets sent up to azure once a button is clicked. Once the user clicks the button they are taking to a separate java activity where the text from the textbox is added to a listbox. However once I close the app or change screen the text is not saved into the listbox permanently even though it is saved in the Azure. Can anyone help me to add the text from azure to my listbox permanently. The listbox is on my ListDeadlines activity.
My Code is as fallows
AddDeadline Activity
private MobileServiceClient mClient;
private EditText title;
public EditText editText;
public EditText editDate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_deadline);
try {
mClient = new MobileServiceClient(
"https://craigsapp.azure-mobile.net/",
"BTkcgnFQvevAdmmRteHCmhHPzdGydq84",
this
);
} catch (MalformedURLException e) {
e.printStackTrace();
}
editText = (EditText)findViewById(R.id.editText2);
editDate = (EditText)findViewById(R.id.editText3);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final ListDeadlines lst= new ListDeadlines();
lst.txtInput=(EditText)findViewById(R.id.txtinput);
ImageView btAdd=(ImageView)findViewById(R.id.btnAdd);
btAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
title= (EditText) findViewById(R.id.txtinput);
Item item = new Item();
item.Text = title.getText().toString();
Intent returnIntent = getIntent();
returnIntent.putExtra("result", item.Text = title.getText().toString());
setResult(RESULT_OK, returnIntent);
mClient.getTable(Item.class).insert(item, new TableOperationCallback<Item>() {
public void onCompleted(Item entity, Exception exception, ServiceFilterResponse response) {
if (exception == null) {
// Insert succeeded
} else {
// Insert failed
}
}
});
finish();
}
});
ListDeadline Activity
public ArrayList<String> arrayList;
public ArrayAdapter<String> adapter;
public ArrayAdapter<String> newad;
public EditText txtInput;
public EditText txtDate;
public int ADD_DEADLINE_REQUEST=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_deadlines);
final ListView listView = (ListView)findViewById(R.id.listv);
String[] items= {""};
arrayList=new ArrayList<>(Arrays.asList(items));
adapter=new ArrayAdapter<String>(this,R.layout.list_item,R.id.txtitem,arrayList);
listView.setAdapter(adapter);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Item newitem = new Item();
if (requestCode == ADD_DEADLINE_REQUEST){
if (resultCode == RESULT_OK) {
String item = data.getStringExtra("result");
arrayList.add(item);
adapter.notifyDataSetChanged();
Toast toast = Toast.makeText(getApplicationContext(),
"Deadline has been added", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 100, 0);
toast.show();
}
}
}
you'll have to query azure for the text and then populate your listbox. Or if you want to save the text locally then you can do it using shared preferences.
As #pooja said, you can try to get the text via query Azure Mobile Service or get from the local storage. However, for keeping consistent between local and cloud, you need to add Offline Data Sync to your Android Mobile Services app, please see the offical document https://azure.microsoft.com/en-us/documentation/articles/mobile-services-android-get-started-offline-data/.
There are two demo video below.
https://channel9.msdn.com/Shows/Cloud+Cover/Episode-155-Offline-Storage-with-Donna-Malayeri
http://azure.microsoft.com/documentation/videos/azure-mobile-services-offline-enabled-apps-with-donna-malayeri/ (For Windows, but feature discussion applies to all platforms.)
And an Android sample for offline data sync on GitHub you can refer to, please see https://github.com/Azure/mobile-services-samples/tree/master/TodoOffline/Android.
Related
Main Activity
`public class MainActivity extends AppCompatActivity {
private Object Context;
public static final String MSG="com.mp.exampleapp.ORDER";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void createBirthdayCard(View view) {
EditText text = (EditText)findViewById(R.id.nameInput);
String str = text.getText().toString();
Toast.makeText(this, "Button was clicked "+str,Toast.LENGTH_LONG).show();
getIntent().putExtra(MSG,str);
startActivity(new Intent(this,birthdaygreeting.class)) ;
ImageView image=(ImageView) findViewById(R.id.cake);
image.setImageResource(R.drawable.wish);
}
`Second activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_birthdaygreeting);
Intent intent = getIntent();
String msg = "HAPPY BIRTHDAY "+intent.getStringExtra(MainActivity.MSG);
// receive the value by getStringExtra() method
// and key must be same which is send by first activity
TextView textView= (TextView)findViewById(R.id.textView2);
textView.setText(msg);
}
After running this program - after clicking the button- the message that's getting displayed is "HAPPY BIRTHDAY null". I don't know what's going wrong.
What is wrong?
The place where you are passing the put extra, is in getIntent(). This means that you are giving an intent extra to the previous activity.In this case, when you go back, and then get that extra, you will get it.But not on the next activity. Now you get null because that extra does not exist for that activity. So, it comes null.
How to solve?
Instead of using this
public void createBirthdayCard(View view) {
EditText text = (EditText)findViewById(R.id.nameInput);
String str = text.getText().toString();
Toast.makeText(this, "Button was clicked "+str,Toast.LENGTH_LONG).show();
getIntent().putExtra(MSG,str);
startActivity(new Intent(this,birthdaygreeting.class)) ;
ImageView image=(ImageView) findViewById(R.id.cake);
image.setImageResource(R.drawable.wish);
}
use this
public void createBirthdayCard(View view) {
EditText text = (EditText)findViewById(R.id.nameInput);
String str = text.getText().toString();
Toast.makeText(this, "Button was clicked "+str,Toast.LENGTH_LONG).show();
startActivity(new Intent(this,birthdaygreeting.class).putExtra(MSG,str)) ;
ImageView image=(ImageView) findViewById(R.id.cake);
image.setImageResource(R.drawable.wish);
}
How do I know it will help me?
It will help you because here in the new intent which you are giving, there the extra is being given. So, that value is being sent.
Create an intent then add your data in it like the code below.
public void createBirthdayCard(View view) {
EditText text = (EditText)findViewById(R.id.nameInput);
String str = text.getText().toString();
Toast.makeText(this, "Button was clicked "+str,Toast.LENGTH_LONG).show();
Intent intent = new Intent(this,birthdaygreeting.class);
intent.putExtra(MSG,str);
startActivity(intent) ;
ImageView image=(ImageView) findViewById(R.id.cake);
image.setImageResource(R.drawable.wish);
}
After pressing the button I would like to go to the second activity, enter the data in the second activity and then return to the main activity, but having data in ListView. This is my code:
MainActivity:
public class MainActivity extends AppCompatActivity {
Button button;
ListView listView;
String name;
private static final int REQUEST_CODE = 1;
ArrayAdapter<String> adapter;
ArrayList<String> nameList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button2);
listView = findViewById(R.id.CarList);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(i, REQUEST_CODE);
}
});
nameList = new ArrayList<String>();
nameList.addAll(Arrays.asList(name));
adapter = new ArrayAdapter<String>(this, R.layout.element, nameList);
listView.setAdapter(adapter);
}
protected void onActivityResult(int requestCode, int resultCode, Intent i){
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
name = i.getStringExtra("name");
}
}
}
And this is my SecondActivity:
public class SecondActivity extends AppCompatActivity {
EditText editText;
Button button2;
String name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
editText = findViewById(R.id.editText);
button2 = findViewById(R.id.button);
}
public void finish() {
Intent i = new Intent();
name = editText.getText().toString();
i.putExtra("name", name);
setResult(RESULT_OK, i);
super.finish();
}
}
What could I change to make the application work? Because now I only get the message that: app has stopped, but I don't receive information about any error.
After pressing the button I would like to go to the second activity, enter the data in the second activity and then return to the main activity, but having data in ListView.
What could I change to make the application work? Because now I only get the message that: app has stopped, but I don't receive information about any error.
This is simply because you've incorrectly thinking that Activity.finish() is always called whenever you close the activity. But it never be called by the Android. Take a look for this Lifecyle picture from Activity-lifecycle concepts:
you can see that only onStop() then onDestroy() is called when activity is closed.
You need to call the finish() method manually to send your intent. Or, the better way, create a method that only build and set the intent for the result. Something like this:
private void prepareResult(String name) {
Intent i = new Intent();
i.putExtra("name", name);
setResult(RESULT_OK, i);
}
then call it whenever you want to close your activity:
String name = editText.getText().toString();
prepareResult(name);
finish();
Or you can override the onBackPressed() to also handing the back pressed, like the following:
#Override
public void onBackPressed() {
Intent i = new Intent();
name = editText.getText().toString();
i.putExtra("name", name);
setResult(RESULT_OK, i);
// The default implementation simply finishes the current activity
// see the documentation.
super.onBackPressed();
}
I have problem with my android coding. I want to make an update process. But first of all, I'm trying to collect all the data from edit text, spinner and image. But I have problem with spinner and image, could not get the previous data. How to get the previous data using spinner and image? Ex.. Image1
shows the complete retrieve process when user click on Submit Button, then it will go to this View Activity interface. While in Image2
shows the Edit Activity interface where the user can update all the data from there when the user click on Edit Button. The issue now is, I can not collect the previous data using spinner and image for editing/updating purposes as shown in the Image 2. Really hope someone can help me.. Thanks in advance...
Coding as follows:
1) Coding from View Activity:-
EditAdsButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent editAds = new Intent(ViewAdsActivity.this, EditAdsActivity.class);
startActivity(editAds);
String tn = ViewTuitionName.getText().toString();
String pn = ViewProviderName.getText().toString();
String pg = getIntent().getStringExtra("PG");
Intent i = new Intent(ViewAdsActivity.this, EditAdsActivity.class);
i.putExtra("TN", "" +tn); // Collected from EditText or any other source
i.putExtra("PN", "" +pn);
i.putExtra("PG", "" +pg);
startActivity(i);
}
});
2) Coding from Edit Activity (onCreate method):-
Intent i = getIntent();
String tn = i.getStringExtra("TN");
String pn = i.getStringExtra("PN");
String pg = getIntent().getStringExtra("PG");
EditTuitionName.setText(tn);
EditProviderName.setText(pn);
EditProviderGender.setSelection(pg);
Here is a solution:
Constant.java
public class Constant {
public static final String[] GENDER = {"Male", "Female", "Other"};
}
ViewAdsActivity.java
public class ViewAdsActivity extends AppCompatActivity {
private CircleImageView avatar;
private EditText name;
private Spinner gender;
private Button next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_ads);
avatar = findViewById(R.id.avatar);
name = findViewById(R.id.edit_text_name);
gender = findViewById(R.id.spinner_gender);
next = findViewById(R.id.button_next);
// Render avatar
String imageUrl = "https://vignette.wikia.nocookie.net/spiritedaway/images/6/69/Chihiro.jpg/revision/latest?cb=20170308090934";
avatar.setTag(imageUrl);
Picasso.get()
.load(imageUrl)
.into(avatar);
// Render spinner
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Constant.GENDER);
gender.setAdapter(adapter);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ViewAdsActivity.this, EditAdsActivity.class);
intent.putExtra("imageUrl", (String) avatar.getTag());
intent.putExtra("genderPosition", gender.getSelectedItemPosition());
intent.putExtra("name", name.getText().toString());
startActivity(intent);
}
});
}
}
EditAdsActivity.java
public class EditAdsActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_ads);
CircleImageView avatar = findViewById(R.id.avatar);
EditText nameEditText = findViewById(R.id.edit_text_name);
Spinner gender = findViewById(R.id.spinner_gender);
String imageUrl = getIntent().getStringExtra("imageUrl");
int genderPosition = getIntent().getIntExtra("genderPosition", 0);
String name = getIntent().getStringExtra("name");
// Render image view
Picasso.get()
.load(imageUrl)
.into(avatar);
// Render spinner
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Constant.GENDER);
gender.setAdapter(adapter);
gender.setSelection(genderPosition);
// Render edit text
nameEditText.setText(name);
}
}
For the spinner you can use
Spinner mySpinner = (Spinner) findViewById(R.id.your_spinner);
String text = mySpinner.getSelectedItem().toString();
And then place the text in the intent like the other objects.
An image is trickier because you don't want to serialize the bitmap for the intent since serialization is on the main thread and the app would probably stutter or hang a bit. Instead you could store the bitmap in a class that is accessible from both activities, or if the image is already stored on the device you can pass the URI and just reopen it in the next activity.
This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 5 years ago.
I have two Java classes. Where I want to retrieve TextView data from DrinkDetail.java into Cart.java in order to save it into Firebase database. The TextView is deliveryOption and wanted to save in Cart.java from Requests table.
How can I do it? Below are my Java codes.
DrinkDetail.java
public class DrinkDetail extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink_detail);
private void showAlertDialogChooseOptions() {
alertDialog = new AlertDialog.Builder(DrinkDetail.this);
alertDialog.setTitle("Drop by our kiosk or rider deliver?");
LayoutInflater inflater = this.getLayoutInflater();
View takeaway_deliver = inflater.inflate(R.layout.takeaway_delivery, null);
btnTakeAway = (FButton)takeaway_deliver.findViewById(R.id.btnTakeAway);
btnDelivery = (FButton)takeaway_deliver.findViewById(R.id.btnDelivery);
deliveryOption = (TextView)takeaway_deliver.findViewById(R.id.deliveryOptionChosen);
alertDialog.setView(takeaway_deliver);
alertDialog.create();
btnTakeAway.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
deliveryOption.setText("Take Away");
alertDialogTakeAway.show();
}
});
}
Cart.java:
public class Cart extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
private void showAlertDialog() {
alertDialog2 = new AlertDialog.Builder(Cart.this);
alertDialog2.setTitle("Cash on Delivery:");
alertDialog2.setMessage("Choose amount of money will be given to the rider upon order arriving: ");
LayoutInflater inflater2 = this.getLayoutInflater();
View cash_on_delivery = inflater2.inflate(R.layout.cash_on_delivery, null);
btnOrderNow = (FButton)cash_on_delivery.findViewById(R.id.btnOrderNow);
btnOrderNow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Submit to Firebase
Request request = new Request(
Common.currentUser.getPhone(),
Common.currentUser.getName(),
editAddress.getText().toString(),
txtTotalPrice.getText().toString(),
"0", //status
editComment.getText().toString(),
paymentMethod.getText().toString(),
moneySelected.getText().toString(),
//WANTED TO SAVE DELIVERY OPTION "TAKE AWAY" HERE,
cart
);
requests.child(String.valueOf(System.currentTimeMillis()))
.setValue(request);
//Order placed
alertDialogOrderPlaced.show();
alertDialog2.setCancelable(true);
}
});
alertDialog2.setView(cash_on_delivery);
alertDialog2.setIcon(R.drawable.icons8_cash_in_hand_filled_50);
}
Make a public method in the class you want to retrieve the data from. Like getData(), something similar to this:
public Data getData(){
return this.data;
}
If your using Intent to navigate from DrinkDetail to Cart then you can pass that string in Intent itself.
like this
Intent intent=new Intent(DrinkDetail.this, Cart.class);
intent.putExtra("Key","String value you want to pass");
startActivity(intent);
and can retrieve that string in Cart.java place below code in onCreate() method
String value;
if(getIntent()!=null)
value=getIntent().getStringExtra("Key");
Data can be stored using SharedPreferences.
In your DrinkDetail.java save the data.
SharedPreferences prefs = this.getSharedPreferences("File", MODE_PRIVATE);
prefs.edit().putString("deliveryOption", deliveryOption.getText()).apply();
In your Cart.java you can retrieve it back.
SharedPreferences prefs = this.getSharedPreferences("File", MODE_PRIVATE);
String deliveryOption = prefs.getString("deliveryOption", "default");
To send the data from one activity to another activity
DrinkDetail.java is first activity from where you want to send data
to another activity
DrinkDetail.java
TextView aText = (TextView )findViewById(R.id.textview);
String text = aText .getText().toString();
Intent myIntent = new Intent(view.getContext(),Cart.java);
myIntent.putExtra("mytext",text);
startActivity(myIntent);
Cart.java is second activity which receive the Intent data whatever
you pass from DrinkDetail.java
Cart.java
public class Cart extends Activity {
TextView mTextview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Cart);
aTextview = (TextView)findViewById(R.id.textView);
aTextview.setText(getIntent().getStringExtra("mytext"));
}
i am new to Andriod and programming. i am trying to make a to do list app, which will contain a listView, Button(add) in the main layout. when i click add i want it to go to another activity which will contain a editText and a add button. when i click the add button i want to update the list in my main activity. Now i was able to get the information from the second activity but when i try to add it in my list it over writes it.
How can i update my list as soon as my main activity appears again.
Here's what i have so far :
MainActivity class :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
add = (Button) findViewById(R.id.bAdd);
list = (ListView) findViewById(R.id.lvList);
addItems(); // i think this is the error but i dont know how to fix it.
alList = new ArrayList<String>();
aaList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, alList);
list.setAdapter(aaList);
}
public void clickAdd(View v) { //when clicking the add button(add)
Intent intent = new Intent(MainActivity.this, AddItem.class);
startActivity(intent);
}
private void addItems(){
String s = getIntent().getStringExtra("item");
aaList.add(s);
aaList.notifyDataSetChanged();
}
AddItem class :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_layout);
et = (EditText) findViewById(R.id.etAdd);
bt = (Button) findViewById(R.id.badd);
}
public void add(View v){ //when clicking the add button(bt)
edit = et.getText().toString();
Intent intent = new Intent(AddItem.this, MainActivity.class);
intent.putExtra("item", edit);
startActivity(intent);
}
Can you please tell me where and why i am going wrong? Thank You
First of all, you should use startACtivityForResult
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
Then in your second Activity
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();
And back in your fist one:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult
and then, your addItem should do the trick:
private void addItems(String s){
aaList.add(s);
aaList.notifyDataSetChanged();
}