Variable Stays Null - android

I'm trying to pass a String between activities, From YtAdapter to Favorites.
YtAdapter:
mHolder.mVideoFavorite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//////////////////////////////////////////////////////////////
// Need to send SearchResult result from here to favorites //
////////////////////////////////////////////////////////////
AppUtils.showToast(result.getSnippet().getTitle() + " Was added to favorites.");
Intent intent = new Intent(mActivity,Favorites.class);
String vidId,vidTitle,vidThumbnail;
vidId = result.getId().getVideoId(); //Video ID
vidTitle = result.getSnippet().getTitle(); //Video Title
vidThumbnail = result.getSnippet().getThumbnails().getMedium().getUrl(); //Video Thumbnail
intent.putExtra("id",vidId);
intent.putExtra("title",vidTitle);
intent.putExtra("thumbnail",vidThumbnail);
}
});
And try to get it in Favorites:
/*Getting video information from YtAdapter*/
vidID = getIntent().getStringExtra("id"); <--- Stays null
vidTitle = getIntent().getStringExtra("title"); <--- Stays null
vidThumbnail = getIntent().getStringExtra("thumbnail"); <--- Stays null
I'm accessing Favorties from 3rd activity, not straight from YtAdapter.

try this in your main class:
Intent i = new Intent(ListViewActivity.this, EditContact.class);
Bundle bundle = new Bundle();
bundle.putInt("index", itemId);
bundle.putBoolean("AddContact", false);
i.putExtras(bundle);
startActivity(i);
and you can get your data in the other class like this:
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
Int index = bundle.getInt("index");
Boolean bool = bundle.getBoolean("AddContact");
if (bool ) {
setTitle("Add Contact");
} else {
setTitle("Edit Contact");
}
I hope it will help.

you can force all of your parameter be a string value:
intent.putExtra("id",vidId +"");
intent.putExtra("title",vidTitle+"");
intent.putExtra("thumbnail",vidThumbnail+"");
probally they aren't and you are getting with "getStringExtra".
but the better options is you use te correct "getter" of extra.
---Edit---
you can check in your activity the keys and values this way:
for(String key : getIntent().getExtras().keySet()){
Log.d("TEST", "key: " + key);
Log.d("TEST", "value: " + getIntent().getExtras().get(key));
}

Related

how to use helper class to send and receive data using intent in android

enter image description here
I want to fetch the data from previous activity (using getIntent) in a .java class and same the data to next activity through intent. can anyone help me how.Thanks in advance.
public class AddToCartHelper {
public static void addToCart(Context context, Intent intent) {
String TAG = "AddToCart";
DecimalFormat decimalFormat;
boolean loginflagforuser = false, loginflagforguest = false;
String advId = "", num = "", uid = "", productName = "", emailCart = "",cartMessage = "";
Double price = 0.0;
Integer quantity = 1;
SharedPreferences preferences = context.getSharedPreferences("SECRETFILE", Context.MODE_PRIVATE);
loginflagforuser = preferences.getBoolean(Parameters.userEmail, false);
loginflagforguest = preferences.getBoolean(Parameters.guestEmail, false);
decimalFormat = new DecimalFormat("##.##");
if (loginflagforuser){
Intent fromCart = getIntent();
// imageId = fromCart.getStringExtra("image_url");
advId = fromCart.getStringExtra("Advid");
price = fromCart.getDoubleExtra("price", 0.0);
num = fromCart.getStringExtra("num");
uid = fromCart.getStringExtra("uid");
Log.d(TAG, "--- REGISTERD UID::::::::: " + uid);
quantity = fromCart.getIntExtra("quantity", 1);
productName = fromCart.getStringExtra("cart_product_name");
// total = fromCart.getDoubleExtra("total", 0.0);
emailCart = preferences.getString("email", null);
}else if (loginflagforguest){
}else{
}
}
}
you don't need to use this Intent fromCart = getIntent();,because in constructor you already pass intent ,then just use intent object
dvId = intent.getStringExtra("Advid");
price = intent.getDoubleExtra("price", 0.0);
num = intent.getStringExtra("num");
uid = intent.getStringExtra("uid");
Log.d(TAG, "--- REGISTERD UID::::::::: " + uid);
quantity = intent.getIntExtra("quantity", 1);
productName = intent.getStringExtra("cart_product_name");
// total = intent.getDoubleExtra("total", 0.0);
You have already pass parameter intent to addToCart function, so instead of advId = fromCart.getStringExtra("Advid");, you can use advId = intent.getStringExtra("Advid");
there is two ways to passing the data to the next activity you can either use intent or can use local broadcast receiver
if you want to fetch data from the previous activity then use
String a= getIntent().getStringExtra( "");// pass the name that you used in the previous activity

passing intent value to another activity

Why the value did not appear in the second activity which is consultDoctorAnaemia? I think the code is already correct. But it display null.
resultAnaemia
if(symptom16.equals("Yes"))
{
weight=0.11;
newWeight = 0.0 * 0.15; //cf disease = 0.6, [min=0.15]
String cf = Double.toString(newWeight);
Intent intent = new Intent(resultAnemia.this, consultDoctorAnaemia.class);
intent.putExtra("cfDiseases", cf);
startActivity(intent);
}
consultDoctorAnaemia
TextView textView = (TextView) findViewById(R.id.textCF);
//get passed intent
Intent intent = getIntent();
if(null != intent)
{
//get cf value from intent
String cf = intent.getStringExtra("cfDiseases");
textView.setText("Certainty value : " + cf);
}
Bundle extras = getIntent().getExtras();
if (extras != null) {
String Diseases = extras.getString("cfDiseases");
}
You need to do in the consultDoctorAnaemia activity:
Bundle bundle = getIntent().getExtras();
String value2 = bundle.getString("cfDiseases");
Try this in first activity:
// do your intent setup somewhere and then setup bundle
Bundle info = new Bundle();
info.putString("cfDiseases", cf);
intent.putExtras(info);
startActivity(intent);
In new activity:
Bundle info = new Bundle();
info = getIntent().getExtras();
cf = info.getString("cfDiseases");
You can also pass value like this way
Declare your string global and static For example
Declare in variable in this class
Class A
public class A extends Activity{
static String cf = "abcde";
}
Access variable in this B class
class B
public class B extends Activity{
String Temp;
//you can get variable like this
Temp=A.cf ;
Toast.makeText(B.this, "Temp = "+Temp, Toast.LENGTH_SHORT).show();
}

How to add custom header while making a pjsip makecall()?

I need to add one custom header for before making a sip call. please help me.
Thanks in advance.
Finally i got the answer for my question. You should change your make makeSipCall() method.
private boolean makeSipCall(String phoneNumber)
{
if(!created)
return false;
Log.i("MtaAPIImpl", (new StringBuilder("makecall : ")).append(phoneNumber).toString());
phoneNumber = (new StringBuilder("<sip:")).append(phoneNumber).append("#").append(sipServer).append(">").toString();
byte userData[] = new byte[1];
int callId[] = new int[1];
pjsua_call_setting cs = new pjsua_call_setting();
pjsua.call_setting_default(cs);
cs.setVid_cnt(0L);
cs.setAud_cnt(1L);
cs.setFlag(0L);
pjsua_msg_data msgData = new pjsua_msg_data();
pjsua.msg_data_init(msgData);
pj_pool_t pool = pjsua.pool_create("call_tmp", 512L, 512L);
pjsua.csipsimple_init_acc_msg_data(pool, 1, msgData);
pj_str_t uri = pjsua.pj_str_copy(phoneNumber);
//Here adding headers adding through bundel.
Bundle extra_header = new Bundle();
final Bundle b = new Bundle();
extra_header.putString("header-Name", "Header-Value");
b.putBundle(SipCallSession.OPT_CALL_EXTRA_HEADERS, extra_header);
Bundle extraHeaders = b.getBundle(SipCallSession.OPT_CALL_EXTRA_HEADERS);
for (String key : extraHeaders.keySet()) {
try {
String value = extraHeaders.getString(key.toString());
if (!TextUtils.isEmpty(value)) {
int res = pjsua.csipsimple_msg_data_add_string_hdr(pool, msgData,pjsua.pj_str_copy(key), pjsua.pj_str_copy(value));
if (res == pjsuaConstants.PJ_SUCCESS) {
Log.e(THIS_FILE, "Failed to add Xtra hdr (" + key + " : "+ value + ") probably not X- header");
}
}
} catch (Exception e) {
Log.e(THIS_FILE, "Invalid header value for key : " + key);
}
}
int status = pjsua.call_make_call(1, uri, cs, userData, msgData, callId);
pjsua.pj_pool_release(pool);
return status == pjsuaConstants.PJ_SUCCESS;
}

getIntent() not capturing data properly

I am passing row id with the help of intent. The calling method being a custom adapter -
holder.text.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "TEXT CLICKED" + pos , Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context,ReminderModificationActivity.class);
long longPos = (long)pos;
intent.putExtra(TasksDBAdapter.KEY_ROWID, pos);
Log.i(TAG, "row clickd --> " + longPos);
((Activity) context).startActivityForResult(intent, ACTIVITY_EDIT);
}
});
OnCreate() method of ReminderModificationActivity try to retrieve the data.Code -
protected void onCreate(Bundle savedInstanceState){
dbHelper = new TasksDBAdapter(this);
//set current theme
settingsDBAdapter = new SettingsDBAdapter(this);
settingsDBAdapter.open();
setSettingsTheme();
setContentView(R.layout.reminder_modify);
mCalendar = Calendar.getInstance();
Log.i(getLocalClassName(), "VALUE of Button--->" + findViewById(R.id.SetDateButtonId));
mDateButton = (Button) findViewById(R.id.SetDateButtonId);
mTimeButton = (Button) findViewById(R.id.SetTimeButtonId);
mConfirmButton = (Button) findViewById(R.id.SaveButtonId);
mTitleText = (EditText) findViewById(R.id.TitleEditTextId);
mBodyText = (EditText) findViewById(R.id.DescriptionEditTextId);
mRowId = savedInstanceState != null ? savedInstanceState.getLong(TasksDBAdapter.KEY_ROWID) : null ;
registerButtonListenersAndSetDefaultText();
Log.i(TAG, "getIntent-->" + getIntent() + mRowId);
//code to check what row id have been passed
if(getIntent() != null) {
---->>>MAIN CODE
Log.i(TAG, "mRowId in getintent-->" + mRowId + "row id passed-->" + getIntent().getLongExtra(TasksDBAdapter.KEY_ROWID, -1));
mRowId = getIntent().getLongExtra(TasksDBAdapter.KEY_ROWID, -1);
// Do stuff with the row id here
if(mRowId != -1){
//code if RowId valid
}
}
super.onCreate(savedInstanceState);
Log.i(TAG, "mROwId in onCreate ReminderNotification again-->" + mRowId);
}
line marked as MAIN CODE captures the intent data but Log.i gives value of
getIntent().getLongExtra(TasksDBAdapter.KEY_ROWID, -1))
as -1.Can sumone please help me out on this.Thanks in advance,Ray
Seeing as you cast pos to longPos, you should be using longPos in your intent extra, but you use pos.
if you were to change your code to:
Intent intent = new Intent(context,ReminderModificationActivity.class);
long longPos = (long)pos;
intent.putExtra(TasksDBAdapter.KEY_ROWID, longPos); //<< changed to longPos
It should work

Android Bundle Concept to pass deta between activities

I am new to android here i am facing problem when i try to pass the retrived data from curser to bundle then i am not able to get the value of that variable.Below is my code please help me to come out from this situation.
Cursor cur3 = db3.rawQuery("SELECT * FROM " + TableName, null);
try {
db3 = this.openOrCreateDatabase("remoteid.db", MODE_PRIVATE, null);
if(cur3 != null )
{
if(cur3.moveToFirst())
{
do {
valueOfID = cur3.getString(cur3.getColumnIndex("PretestID"));
valuOfDate = cur3.getString(cur3.getColumnIndex("Date"));
textType = cur3.getString(cur3.getColumnIndex("txtVCT"));
valueOfDDLTS = cur3.getString(cur3.getColumnIndex("ddlTestingSession"));
valueOfReason = cur3.getString(cur3.getColumnIndex("txtReason"));
valueOfHowmany = cur3.getString(cur3.getColumnIndex("txthowmany"));
valueOftxtques1 = cur3.getString(cur3.getColumnIndex("txtques1"));
valueOfrblques2a = cur3.getString(cur3.getColumnIndex("rblques2a"));
valueOfrblques2b = cur3.getString(cur3.getColumnIndex("rblques2b"));
valueOfrblques3 = cur3.getString(cur3.getColumnIndex("rblques3"));
valueOftxtques4 = cur3.getString(cur3.getColumnIndex("txtques4"));
valueOfrblques5 = cur3.getString(cur3.getColumnIndex("rblques5"));
valueOfrblques6 = cur3.getString(cur3.getColumnIndex("rblques6"));
valueOfrblques7 = cur3.getString(cur3.getColumnIndex("rblques7"));
valueOfrblques8 = cur3.getString(cur3.getColumnIndex("rblques8"));
valueOfrblques9 = cur3.getString(cur3.getColumnIndex("rblques9"));
valueOfddlsick = cur3.getString(cur3.getColumnIndex("ddlsick"));
valueOftxtques11 = cur3.getString(cur3.getColumnIndex("txtques11"));
valueOfrblques12 = cur3.getString(cur3.getColumnIndex("rblques12"));
valueOftxtques13 = cur3.getString(cur3.getColumnIndex("txtques13"));
valueOftxtques14 = cur3.getString(cur3.getColumnIndex("txtques14"));
valueOfrblques15 = cur3.getString(cur3.getColumnIndex("rblques15"));
valueOfrblques16 = cur3.getString(cur3.getColumnIndex("rblques16"));
valueOfrblques17 = cur3.getString(cur3.getColumnIndex("rblques17"));
valueOftxtques18 = cur3.getString(cur3.getColumnIndex("txtques18"));
//Toast.makeText(getApplicationContext(), valueOftxtques18, Toast.LENGTH_SHORT).show();
}while (cur3.moveToNext());
}
}
}
catch(Exception e) {
Log.e("Error", "Error", e);
} finally {
if (db3 != null)
db3.close();
}
cur3.close();
arrayadapter11 = new simpleefficientadapter(Screening.this,prtestData);
arrayadapter22 = new simpleefficientadapter(Screening.this,screeningData);
arrayadapter33 = new simpleefficientadapter(Screening.this,postData);
mylist1.setAdapter(arrayadapter11);
mylist1.setOnItemClickListener(this);
mylist2.setAdapter(arrayadapter22);
mylist2.setOnItemClickListener(this);
mylist3.setAdapter(arrayadapter33);
mylist3.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent intent;
switch (arg0.getId()) {
case R.id.prescreenlist:
intent = new Intent(getApplicationContext(), NewScreening.class);
Bundle bundle = new Bundle();
bundle.putString("DateValue", valuOfDate);
bundle.putString("TT", textType);
bundle.putString("idValue", valueOfID);
bundle.putString("ddltsValue", valueOfDDLTS);
bundle.putString("reasonValue", valueOfReason);
bundle.putString("howmanyValue", valueOfHowmany);
bundle.putString("textqus1Value", valueOftxtques1);
bundle.putString("textqus2aValue", valueOfrblques2a);
bundle.putString("textqus2bValue", valueOfrblques2b);
bundle.putString("rbqs3Value", valueOfrblques3);
bundle.putString("rbqs4Value", valueOftxtques4);
bundle.putString("rbqs5Value", valueOfrblques5);
bundle.putString("rbqs6Value", valueOfrblques6);
bundle.putString("rbqs7Value", valueOfrblques7);
bundle.putString("rbqs8Value", valueOfrblques8);
bundle.putString("rbqs9Value", valueOfrblques9);
bundle.putString("ddlsValue", valueOfddlsick);
bundle.putString("tq11Value", valueOftxtques11);
bundle.putString("tq12Value", valueOfrblques12);
bundle.putString("tq13Value", valueOftxtques13);
bundle.putString("tq14Value", valueOftxtques14);
bundle.putString("rbqs15Value", valueOfrblques15);
bundle.putString("rbqs16Value", valueOfrblques16);
bundle.putString("rbqs17Value", valueOfrblques17);
bundle.putString("rbqs18Value", valueOftxtques18);
intent.putExtras(bundle);
startActivityForResult(intent, 1);
setResult(1,intent);
break;
A wiser move, might be passing the id of that database row to the next Activity.
And your code is incomplete, but it looks like only the last row's data will ever be passed to the next activity.
Also, it might be mildly more efficient and clear if you just putExtra directly into the intent instead of the intermediary Bundle.
Good practices said to include the package name as a prefix in the extra names.

Categories

Resources