Reputation: 996
In Activity A, I have a listView and an action bar icon used to intent to Activity B. If listView is pressed, it will send all the value from A to B and display on editText.
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view, final int position, long id) {
Expenses o = (Expenses) obj.getItem(position);
String day = o.getDate(); // 4 Dec 2016
String[] split = day.split("\\s+");
String date= split[1]; // 4
Intent intent = new Intent(QuickExpenses.this,AddExpenses.class);
intent.putExtra("date",date);
intent.putExtra("month", month); // Dec 2016
startActivity(intent);
}
});
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_task:
Intent intent = new Intent(QuickExpenses.this, AddExpenses.class);
intent.putExtra("month", month); // Dec 2016
startActivityForResult(intent, PROJECT_REQUEST_CODE);
return true;
}
}
Activity B
Activity B has an editText. When editText is pressed it will pop out datePickerDialog, for user select the date.
EditText date;
String month;
date = (EditText) findViewById(R.id.date);
month = getIntent().getStringExtra("month");
if(getIntent().getExtras()!=null) { //if has value pass from A (listView pressed)
String mDay = getIntent().getExtras().getString("date");
date.setText(mDay + "" + month);
}
So if the listview is pressed, it will display 5 Dec 2016 in EditText
date. But the problem is when I click the icon in Activity A, it display null Dec 2016
. I want to clear the editText
by using date.setText("")
and date.getText().clear();
but it still showing null Dec 2016
.
I want date
editText
display nothing if icon Activity A pressed but notnull Dec 2016
.
Upvotes: 0
Views: 805
Reputation: 503
You must check the intent from listview or icon.From your code can check from mDay is empty or not.If mDay is empty it's form icon pressed.
if(getIntent().getExtras()!=null) {
String month = getIntent().getStringExtra("month");
String mDay = getIntent().getExtras().getString("date");
if(!TextUtils.isEmpty(mDay)
date.setText(mDay + "" + month);
else // mDay will empty from icon click.
date.setText("");
}
Upvotes: 0
Reputation: 51
On click Activity A remove intent.putExtra("month", month);on Icon click and in Activity B try bellow code
date = (EditText) findViewById(R.id.date);
month = getIntent().getStringExtra("month");
mDay = getIntent().getStringExtra("date");
if(getIntent().getExtras()=null) { //if has value pass from A (listView pressed)
date.setText("");
}
Try this
Upvotes: 1
Reputation: 972
Check below code :
if(getIntent().getExtras()!=null) { //if has value pass from A (listView pressed)
String mDay = getIntent().getStringExtra("date");
if(!TextUtils.isEmpty(mDay) && !TextUtils.isEmpty(month)){
date.setText(mDay + "" + month);
}
}
Upvotes: 2
Reputation: 1801
The problem with your code is your passing intent in both cases so that is why it is going inside this block
if(getIntent().getExtras()!=null) { //if has value pass from A (listView pressed)
String mDay = getIntent().getExtras().getString("date");
date.setText(mDay + "" + month);
}
so just change your if condition with below code
if(getIntent().getExtras().getString("date")!=null) { //if has value pass from A (listView pressed)
String mDay = getIntent().getExtras().getString("date");
date.setText(mDay + "" + month);
}
else {
date.setText("");
}
Hope this will help
Upvotes: 4
Reputation: 91
Based on your Code , some value (Variable month,ie may be holding the value Dec 2016) is send using intent when you are clicking on the icon in Action bar
remove
intent.putExtra("month", month); // Dec 2016
from onOptionsItemSelected
Upvotes: 0
Reputation: 3887
Try out the following snippet!
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_task:
Intent intent = new Intent(QuickExpenses.this, AddExpenses.class);
intent.putExtra("date", ""); // Dec 2016
intent.putExtra("month", month); // Dec 2016
startActivityForResult(intent, PROJECT_REQUEST_CODE);
return true;
}
}
Hope this help you!
Upvotes: 0