Intent not being received - android

I have 2 activities involved in this question. The first one the user inputs data on a simple form style layout. It then should save the 4 pieces of data to an intent (putextra) and startActivity(intent).
The second should retrieve the data and have them ready to be loaded in to arrays. When i run the debug, the intent from activity1 successfully holds the values; however, activity2 says the intent is null.. I'm unsure of what is happening in between.
ACTIVITY1
EditText editDebtName = (EditText) findViewById(R.id.dispDebtName);
debtName = editDebtName.getText().toString();
EditText editDebtAmount = (EditText) findViewById(R.id.dispDebtAmount);
String debtAmountStr = editDebtAmount.getText().toString();
EditText editDebtRate = (EditText) findViewById(R.id.dispDebtRate);
String debtRateStr = editDebtRate.getText().toString();
EditText editDebtPayment = (EditText) findViewById(R.id.dispDebtPayment);
String debtPaymentStr = editDebtPayment.getText().toString();
Intent i = new Intent(this, DebtList.class);
i.putExtra("newDebtName", debtName);
i.putExtra("newDebtAmount", debtAmountStr);
i.putExtra("newDebtRate", debtRateStr);
i.putExtra("newDebtPayment", debtPaymentStr);
startActivity(i);
ACTIVITY2
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.debtlist);
String[] debtName = new String[10];
String[] debtAmount = new String[10];
String[] debtRate = new String[10];
String[] debtPayment = new String[10];
int trigger = 5;
SharedPreferences sharedPref= getSharedPreferences("chaosdatasnowball", 0);
debtName = getFromSharedPreference("debtNames", this.getApplicationContext());
debtAmount = getFromSharedPreference("debtAmounts", this.getApplicationContext());
debtRate = getFromSharedPreference("debtRates", this.getApplicationContext());
debtPayment = getFromSharedPreference("debtPayments", this.getApplicationContext());
trigger = sharedPref.getInt("trigger", trigger);
Bundle extras = getIntent().getExtras();
debtName[trigger] = extras.getString("newDebtName");
debtAmount[trigger] = extras.getString("newDebtAmount");
debtRate[trigger] = extras.getString("newDebtRate");
debtPayment[trigger] = extras.getString("newDebtPayment");
There are no error messages being throw out either. What I'm trying to determine is.. why is my intent not being received..?

I think that problem cause by this code
SharedPreferences sharedPref= getSharedPreferences("chaosdatasnowball", 0);
debtName = getFromSharedPreference("debtNames", this.getApplicationContext());
debtAmount = getFromSharedPreference("debtAmounts", this.getApplicationContext());
debtRate = getFromSharedPreference("debtRates", this.getApplicationContext());
debtPayment = getFromSharedPreference("debtPayments", this.getApplicationContext());
Because i test without above code, activity2 still receive bundle value. If you try without above code and still have that problem, you should try to debug & show all values of string array to determine problem again.

Related

Passing inputs through activities

i was developing an app and this question showed up:
EditText inputCorrect = (EditText) findViewById(R.id.inputCorrect);
EditText inputWrong = (EditText) findViewById(R.id.inputWrong);
EditText inputBlank = (EditText) findViewById(R.id.inputBlank);
EditText inputAll = (EditText)findViewById(R.id.inputAll);
String correctAmountText = inputCorrect.getText().toString();
String wrongAmountText = inputWrong.getText().toString();
String blankAmountText = inputBlank.getText().toString();
String allAmountText = inputAll.getText().toString();
myResultActivty.putExtra("c_a",correctAmountText);
myResultActivty.putExtra("w_a",wrongAmountText);
myResultActivty.putExtra("b_a",blankAmountText);
myResultActivty.putExtra("a_a",allAmountText);
startActivity(myResultActivty);
this is the code there are 4 edit texts with inputType of decimal number
i am getting string from them and send them to other activity.
in the second activity i take those string and turn them into ints with parseint method:
String correctNum = inputActivity.getString("c_a");
String wrongNum = inputActivity.getString("w_a");
String blankNum = inputActivity.getString("b_a");
String allNum = inputActivity.getString("a_a");
float res = 0;
int cN = Integer.parseInt(correctNum);
int wN = Integer.parseInt(wrongNum);
int bN = Integer.parseInt(blankNum);
int aN = Integer.parseInt(allNum);
but whenever i want to do mathematical operations it crashes or shows zero as result.
You could use intents to pass data between activities. In this case, you could create an intent like
Intent intent = Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("key",10);
startActivity(intent);
In SecondActivity,
Bundle extras = getIntent().getExtras();
int yourNum = extras.getInt("key"); // you should get 10

Is it possible to share multiple text by using share intent?

I intend to share 6 text information, however it always shows the last information only, i.e.
share.putExtra(Intent.EXTRA_TEXT, Contact);
I also tried to use a string array to store all 6 information, i.e:
share.putExtra(Intent.EXTRA_TEXT, stringArray);
However, it still doesn't work. Can anyone help ? Thank you.
My code:
public class SingleJobActivity extends Activity {
// JSON node keys
private static final String TAG_POSTNAME = "PostName";
private static final String TAG_LOCATION = "Location";
private static final String TAG_SALARY = "Salary";
private static final String TAG_RESPONSIBILITY = "Responsibility";
private static final String TAG_COMPANY = "Company";
private static final String TAG_CONTACT = "Contact";
String PostName;
String Location;
String Salary;
String Responsibility;
String Company;
String Contact;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_job_json_parsing);
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
PostName = in.getStringExtra(TAG_POSTNAME);
Location = in.getStringExtra(TAG_LOCATION);
Salary = in.getStringExtra(TAG_SALARY);
Responsibility = in.getStringExtra(TAG_RESPONSIBILITY);
Company = in.getStringExtra(TAG_COMPANY);
Contact = in.getStringExtra(TAG_CONTACT);
// Displaying all values on the screen
TextView lblPostName = (TextView) findViewById(R.id.PostName_label);
TextView lblLocation = (TextView) findViewById(R.id.Location_label);
TextView lblSalary = (TextView) findViewById(R.id.Salary_label);
TextView lblResponsibility = (TextView) findViewById(R.id.Responsibility_label);
TextView lblCompany = (TextView) findViewById(R.id.Company_label);
TextView lblContact = (TextView) findViewById(R.id.Contact_label);
lblPostName.setText(PostName);
lblLocation.setText(Location);
lblSalary.setText(Salary);
lblResponsibility.setText(Responsibility);
lblCompany.setText(Company);
lblContact.setText(Contact);
// listeners of our button
View.OnClickListener handler = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.share:
shareTextUrl();
break;
}
}
};
// our button
findViewById(R.id.share).setOnClickListener(handler);
}
// Method to share either text or URL.
private void shareTextUrl() {
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
// Add data to the intent, the receiving app will decide
// what to do with it.
share.putExtra(Intent.EXTRA_SUBJECT, "Job Information:");
share.putExtra(Intent.EXTRA_TEXT, PostName);
share.putExtra(Intent.EXTRA_TEXT, Location);
share.putExtra(Intent.EXTRA_TEXT, Salary);
share.putExtra(Intent.EXTRA_TEXT, Responsibility);
share.putExtra(Intent.EXTRA_TEXT, Company);
share.putExtra(Intent.EXTRA_TEXT, Contact);
startActivity(Intent.createChooser(share, "Share via"));
}
}
Could anyone help ?
Concatenate the six strings into one larger string, and share that larger string.
You can Concatenate the individual strings to larger string, and can get it
Intent iin = getIntent();
Bundle b = iin.getExtras();
if (b != null) {
String j = (String) b.get("name");
JSONObject name = new JSONObject(j);
Textv.setText(name.getString("postName"));
TextvDesc.setText(name.getString("location"));
}
What's wrong with your actual code
Let's first understand why you only get the last piece of data.
Your problem is by convention in Java (and this also applies to the android.content.Intent.html#putExtra(String, String[]) method you are using) the methods "putXXX" replace the actual value (if it exists) with the new one you are passing.
This is similar to java.util.Map.html#put(K, V) method.
First possible solution
For your current code to work, you would have needed to use a different key for your extra data each time, that is, something like that:
share.putExtra(SingleJobActivity.EXTRA_TEXT_NAME, PostName);
share.putExtra(SingleJobActivity.EXTRA_TEXT_LOCATION, Location);
share.putExtra(SingleJobActivity.EXTRA_TEXT_SALARY, Salary);
share.putExtra(SingleJobActivity.EXTRA_TEXT_RESPONSIBILITY, Responsibility);
share.putExtra(SingleJobActivity.EXTRA_TEXT_COMPANY, Company);
share.putExtra(SingleJobActivity.EXTRA_TEXT_CONTACT, Contact);
This would work fine (assuming you declare as public static final the keys used, and you respect the Android contract for extra data keys, such as using the full package name for the key (e.g. public static final EXTRA_TEXT_NAME = "com.yourpackage.EXTRA_DATA_NAME";).
Second possible solution
Another way of doing it is to pass one extra with a String[] (see method documentation).
String[] extraParams = new String[6];
extraParams[0] = PostName;
extraParams[1] = Location;
extraParams[2] = Salary;
extraParams[3] = Responsibility;
extraParams[4] = Company;
extraParams[5] = Contact;
share.putExtra(SingleJobActivity.EXTRA_TEXT, extraParams);
Then in your new activity you retrieve this array using android.content.Intent.html#getStringArrayExtra(String) method.
Intent intent = getIntent();
String[] extraParams = intent.getStringArrayExtra(SingleJobActivity.EXTRA_TEXT);

Saving String[] as a single String in Android

I am trying to take my 4 String arrays and convert them in to a single string separated by a ",". It seems to save the information easy enough when I debug; however, when I try and getString - it doesn't retrieve the data. I'm trying to determine why:
String[] debtName = new String[10];
String[] debtAmount = new String[10];
String[] debtRate = new String[10];
String[] debtPayment = new String[10];
//Load Data
SharedPreferences sharedPref= getSharedPreferences("chaosdatasnowball", 0);
debtName = sharedPref.getString("debtNames", null).split(",");
//End Load
Bundle extras = getIntent().getExtras();
int flipper = 0;
for(int i=0;i<debtName.length;i++)
{
if(debtName[i].equals(null) && !extras.equals(null) && flipper!=1)
{
debtName[i] = extras.getString("newDebtName");
debtAmount[i] = extras.getString("newDebtAmount");
debtRate[i] = extras.getString("newDebtRate");
debtPayment[i] = extras.getString("newDebtPayment");
flipper = 1;
}
}
..............
StringBuilder value = new StringBuilder("");
SharedPreferences.Editor editor= sharedPref.edit();
for (String i : debtName) {
value.append(i + ",");
}
editor.putString("debtNames", value.toString());
for (String i : debtAmount) {
value.append(i + ",");
}
editor.putString("debtAmounts", value.toString());
for (String i : debtRate) {
value.append(i + ",");
}
editor.putString("debtRates", value.toString());
for (String i : debtPayment) {
value.append(i + ",");
}
editor.putString("debtPayments", value.toString());
editor.commit();
Correction, when run outside of debug, this gives a nullpointer exception on:
SharedPreferences sharedPref= getSharedPreferences("chaosdatasnowball", 0);
debtName = sharedPref.getString("debtNames", null).split(","); <---this line
Here is where the activity is called:
EditText editDebtName = (EditText) findViewById(R.id.dispDebtName);
debtName = editDebtName.getText().toString();
EditText editDebtAmount = (EditText) findViewById(R.id.dispDebtAmount);
String debtAmountStr = editDebtAmount.getText().toString();
EditText editDebtRate = (EditText) findViewById(R.id.dispDebtRate);
String debtRateStr = editDebtRate.getText().toString();
EditText editDebtPayment = (EditText) findViewById(R.id.dispDebtPayment);
String debtPaymentStr = editDebtPayment.getText().toString();
Intent i = new Intent(this, DebtList.class);
i.putExtra("newDebtName", debtName);
i.putExtra("newDebtAmount", debtAmountStr);
i.putExtra("newDebtRate", debtRateStr);
i.putExtra("newDebtPayment", debtPaymentStr);
startActivity(i);
The problem is with this line:
debtName = sharedPref.getString("debtNames", null).split(",");
It is possible that the key debtNames does not exist. Therefor null is returned.
This will happen when you run the app for the first time and have not saved anything to the shared preference yet.
Add a check for null and handle this case. Something like, if null is returned, then show default values, else show values that are read from sharedpreference.
I have surrendered to the apparent fact that there is no valid way to do this so I have hard coded 40 variables in to the app now. If anyone comes up with anything useful, please feel free to post it and I'll check this every so often.

Problems with putExtra and putStringArrayList

I'm trying to send some data from one activity to another and it's sorta working but not like I want to work.
Problem 1-Things are getting mixed up. On the Next Activity part of the listitem is going to an incorrect textView and part to the correct textview.
Problem 2- I am only able to list 1 item on the new activity but I want to be able to send multiple listitems. I think the problem lies in combining different types of putExtra request to the same place like I do here.
.putExtra("inputPrice",(CharSequence)pick)
.putStringArrayListExtra("list", listItems)
Ant help would be appreciated.
Sending Data to next Activity
final TextView username =(TextView)findViewById(R.id.resultTextView);
String uname = username.getText().toString();
final TextView uplane =(TextView)findViewById(R.id.inputPrice);
String pick = uplane.getText().toString();
final TextView daplane =(TextView)findViewById(R.id.date);
String watch = daplane.getText().toString();
startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",(CharSequence)watch)
.putExtra("Card Number",(CharSequence)uname)
.putExtra("inputPrice",(CharSequence)pick)
.putStringArrayListExtra("list", listItems)
);
finish();
This is the Next Activity
Intent is = getIntent();
if (is.getCharSequenceExtra("Card Number") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleRccn);
setmsg.setText(is.getCharSequenceExtra("Card Number"));
}
Intent it = getIntent();
if (it.getCharSequenceExtra("date") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleTime);
setmsg.setText(it.getCharSequenceExtra("date"));
}
Intent id1 = getIntent();
if (id1.getCharSequenceExtra("inputPrice") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleName);
setmsg.setText(id1.getCharSequenceExtra("inputPrice"));
}
ArrayList<String> al= new ArrayList<String>();
al = getIntent().getExtras().getStringArrayList("list");
saleNotes= (TextView) findViewById(R.id.saleNotes);
saleNotes.setText(al.get(0));
Alright, a few things:
First of all you do not need to cast your strings as CharSequence.
Second thing,
Define intent, add your extras and only then call startActivity as below:
Intent intent = new Intent(MenuView1Activity.this,RecordCheckActivity.class);
intent.putExtra("date", watch);
startActivity(intent);
Third, when retrieving the intent create a bundle first as below:
Bundle extras = getIntent().getExtras();
String date = extras.getString("date");
EDIT:
Here is how you convert your entire array list to one single string and add it to your textview.
String listString = "";
for (String s : al)
{
listString += s + "\t"; // use " " for space, "\n" for new line instead of "\t"
}
System.out.println(listString);
saleNotes.setText(listString);
Hope this helps!
Try this, Don't use CharSequence just put string value
startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",watch)
.putExtra("Card Number",uname)
.putExtra("inputPrice",pick)
.putStringArrayListExtra("list", listItems)
);
And get like this
Intent is = getIntent();
if (is.getCharSequenceExtra("Card Number") != null) {
final TextView setmsg = (TextView)findViewById(R.id.saleRccn);
setmsg.setText(is.getStringExtra("Card Number"));
}

Passing correct Info to Dialog

I am looking at creating a Dialog with a bundle of data. This all work correctly.
I have a number of buttons when clicked opens the dialog with the data and can then be updated.
Trouble I am having is it does not matter which button I press the bundle is only the data for the last row. Any ideas on getting the correct data passed to the Dialog for each button.
I was thinking down the line of setting an id for each view as I pass through the loop but unsure how to call that back again
Code:
for (int i = 0; i < nameInfo.size(); i++) {
// creating the views
View viewItem = (View) inflater.inflate(R.layout.view_item, null);
nameView = (TextView) viewItem.findViewById(R.id.title);
nameView.setId(i);
value1View = (TextView) viewItem.findViewById(R.id.value1);
value1View.setId(i);
value2View = (TextView) viewItem.findViewById(R.id.value2);
value2View.setId(i);
updateButton = (Button) sightmarkView.findViewById(R.id.updatebutton);
updateButton.setId(i);
// Getting the values
nameValue = nameInfo.get(i).toString();
value1 = db.getvalue1('1', nameInfo.get(i).toString());
value2 = db.getvalue2('2', nameInfo.get(i).toString());
// update fields
nameView.setText(nameValue);
value1View.setText(String.valueOf(value1));
value2View.setText(String.valueOf(value2));
updateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int updateButtonId = updateButton.getId();
bundle = new Bundle();
bundle.putString("name", nameValue);
bundle.putFloat("value1", value1);
bundle.putFloat("value2", value2);
showDialog(SIGHTMARK_DIALOG_ID, bundle);
}
});
pMainlayout.addView(viewItem);
}
Thanks for your time
In each loop (from the for), you are redefining the values of nameValue, value1 and value2.
To fix the problem, just change your code to match this:
// Getting the values
final String nameValue = nameInfo.get(i).toString();
final String value1 = db.getvalue1('1', nameInfo.get(i).toString());
final String value2 = db.getvalue2('2', nameInfo.get(i).toString());

Categories

Resources