How to pass hidden id using Intent in Android? - android

I have Edittext Activity & I have set Edittext Id in different getter setter class. Now I want to pass Edittext id using intent. I am trying to create a object of getter class but when I am calling getEdittext id it is passing 0 or new value. Can anybody tell me how it is possible.
my code here:
case R.id.setDate:
Intent passAddTask_Id = new Intent(getApplicationContext(), Task_Details.class);
db = new TodoTask_Database(getApplicationContext());
tasknote = taskNote.getText().toString();
db.addTaskNote(tasknote, null, completed);
AddTask addTask = new AddTask(); //Getter setter class object
int addtask_Id = addTask.getTaskID(); //getting 0 value
passAddTask_Id.putExtra("AddTask_Id", addtask_Id);
startActivity(passAddTask_Id);
break;
Getting Intent:
Intent passAddTask_ID = getIntent();
int AddtaskID = passAddTask_ID.getIntExtra("AddTask_Id", 0);

To get the value, you'll have to use putExtra() like so:
case R.id.setDate:
Intent passAddTask_Id = new Intent(getApplicationContext(), Task_Details.class);
passAddTask_Id.putExtra("AddTask_Id", 11);
db = new TodoTask_Database(getApplicationContext());
tasknote = taskNote.getText().toString();
db.addTaskNote(tasknote, null, completed);
AddTask addTask = new AddTask(); //Getter setter class object
int addtask_Id = addTask.getTaskID(); //getting 0 value
passAddTask_Id.putExtra("AddTask_Id", addtask_Id);
startActivity(passAddTask_Id);
break;
From the documentation here -
Returns
the value of an item that previously added with putExtra() or the default value if none >was found.
After that, when you use:
Intent passAddTask_ID = getIntent();
int AddtaskID = passAddTask_ID.getIntExtra("AddTask_Id", 0);
You'll get the result of 11, because you have put it with the putExtra() method.

You are passing the values through intent correctly. I think you are getting problems while getting the passed values. Try this and this works for me.
You have to use a bundle for getting the values.
Intent intent = new Intent();
Bundle b = intent.getExtras();
int addTaskID = b.getInt("AddTask_Id, 0);

Finally I got the Answer here... I need to create query in database, return long in insert method and then call that particular row id.
like this:
public long addTaskNote(String taskNote, String TaskList_Id, String taskCompleted) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TASKNOTE, taskNote); // Task Note
values.put(KEY_TASK_TASKLISTID, TaskList_Id); // TaskList ID
values.put(KEY_TASKCOMPLETED, taskCompleted); // Task Completed
// Inserting Row
long id = db.insert(TABLE_TODOTASK, null, values);
db.close(); // Closing database connection
return id; //It will give you the return value of id.
}
Changes in my code:
case R.id.setDate:
Intent passAddTask_Id = new Intent(getApplicationContext(), Task_Details.class);
db = new TodoTask_Database(getApplicationContext());
tasknote = taskNote.getText().toString();
long Addtask_id = db.addTaskNote(tasknote, null, completed);
System.out.println(Addtask_id);
passAddTask_Id.putExtra("AddTask_Id", Addtask_id);
startActivity(passAddTask_Id);
break;
Here you can get id at run time saved value.

Related

How start new activity with click on table rows and pass data to new activity in EditText

Following is my code and I have already set OnClickListener with each table row,it is working. Now I want to start a new activity when user click on any row and also I want to pass the data of row to new activity in a textedit.
Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+docno.getText()+"'", null);
int rows = c.getCount();
table_layout.removeAllViews();
c.moveToFirst();
for (int i = 0; i < rows; i++) {
String rollno = c.getString(0);
String name = c.getString(1);
String marks = c.getString(2);
TableRow row = new TableRow(this);
row.setClickable(true);
row.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
v.setBackgroundColor(Color.GRAY);
}
});
row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
TextView et = new TextView(this);
TextView name1 = new TextView(this);
TextView marks1 = new TextView(this);
name1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
et.setGravity(Gravity.CENTER);
et.setTextSize(10);
et.setText(rollno);
name1.setGravity(Gravity.CENTER);
name1.setTextSize(10);
name1.setText(name);
name1.setBackgroundColor(0xFF00FF00);
name1.setHeight(20);
name1.setWidth(180);
marks1.setGravity(Gravity.CENTER);
marks1.setTextSize(10);
marks1.setText(marks);
row.addView(et);
row.addView(name1);
row.addView(marks1);
table_layout.addView(row);
c.moveToNext();
}
There are a number of ways to do this, a few are described below.
You can use the putExtra method for your new intent. You can either do this by passing the data as text or you can expose the Cursor and simply pass the row number. To pass the data as text, in your onClick:
Intent intt = new Intent(this,NewActivity.class);
intt.putExtra("rollno",rollno);
intt.putExtra("name",name);
intt.putExtra("marks",marks);
startActivity(intt);
Then in your NewActivity
Bundle extras = getIntent().getExtras();
if(extras != null) {
// Use extras.getString("rollno") in your setText method in your new activity
// Use extras.getString("name") in your setText method in your new activity
// Use extras.getString("marks") in your setText method in your new activity
}
You could also expose your Cursor by declaring it inside your class, but outside/before your onCreate method.
public static Cursor c;
Then in your onCreate remove the 'Cursor' keyword at the start. You can then pass the data in as a row number in your onClick as such:
Intent intt = new Intent(this,NewActivity.class);
intt.putExtra("rownum",i);
startActivity(intt);
Then in your new activity:
Bundle extras = getIntent().getExtras();
int rownum;
if(extras != null) {
c.moveToPosition(extras.getInt("rownum"));
et.setText(MainActivity.c.getString(0));
name1.setText(MainActivity.c.getString(1));
marks1.setText(MainActivity.c.getString(2));
}
You could even simply skip passing the data through the intent, in the onClick:
c.moveToPosition(i);
Intent intt = new Intent(this,NewActivity.class);
startActivity(intt);
Then when you reference MainActivity.c (your cursor object) using getString() in your NewActivity, the correct row will have already been set for you. However, this requires that you always call moveToPosition
Id also like to suggest a few other tips. You should use a different naming convention for your layout objects. Using variables such as name, name1, marks, marks1 will lead to confusion. Instead use name, nameText or marksString, marksTextView.
Also for your db query, you should use query() method. Instead of:
Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+docno.getText()+"'", null);
Use:
c=db.query("student","rollno="+docno.getText.toString(),null,null,null,null,null);
This also assumes you have declared 'c' as a Cursor already as explained above. You also cannot guarantee that your columns will be exactly where you expect them to be, so instead of using
c.getString(0)
to retrieve column 0, instead you should instead use:
c.getString(c.getColumnIndex("columnName");
Where 'columnName' is the name of the column in the table.
http://developer.android.com/training/basics/firstapp/starting-activity.html
Particularly the "Start the second activity" section seems to be precisely what you are looking for. Grab the relevant data from the selected row and then use intent.putExtra(key, value) to send it to the next activity.

intent getString error

I have created two classes inside other class . inside these two classes i have used the Intent class.
Intent intent = new Intent(getApplicationContext(), DurationsActivity.class );
intent.putExtra("to",mydate);
in parent class i used this code to retrieve the intent value .
String to = getIntent().getExtras().getString("to");
String from = getIntent().getExtras().getString("from");
my logcat retriev this
java.lang.NullPointerException
mydate is like a key to identify the value in intent.
Declare mydate :-
String mydate,mydate1;
Intent intent = new Intent(ActivityName.this, DurationsActivity.class ); //Be Specific
intent.putExtra("to",mydate);
intent.putExtra("from",mydate1);
Than in DurationActivity.class
You can use your usual code in onCreate to get the contents from previous activity:-
String to = getIntent().getExtras().getString("to");
String from = getIntent().getExtras().getString("from");
Try, hope it will work
Intent intent = new Intent(YourCurrentActivity.this, DurationsActivity.class );
intent.putExtra("to",""+mydate);
String to = getIntent().getExtras().getString("to");
String from = getIntent().getExtras().getString("from");

how to use the last id SQLite

I am new to the android world and have a problem with an id. What i need is that when the user clicks on new match it will insert a new row into the db. This is working and i get the lastId but now i need this id in the next activities. How can i store that id so i can use it elsewhere?
This is how i insert the new match:
public void newMatch(WedstrijdenGeschiedenis wedstrijd){
// 1.
SQLiteDatabase db = this.getWritableDatabase();
// 2.
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = new Date();
ContentValues values = new ContentValues();
values.put(KEY_DATUM, dateFormat.format(date)); // get datum
// 3.
long lastId = db.insert(TABLE_WEDSTRIJD, // table
null, //nullColumnHack
values); // key/value -> keys = column names/ values = column values
Log.d("New Match","ID ="+lastId);
// 4. close
db.close();
}
so i see the lastId in LogCat but i don't know how to store it for further use. I tried void but offcourse that is not possible on void. Sorry for the dummy question
change void to long and add a return statement that returns the lastId
public long newMatch(WedstrijdenGeschiedenis wedstrijd){
// Your other code
return lastId;
}
Access it with:
long lastId= db.newMatch(new WedstrijdenGeschiedenis());

Am unable to get the Data from bundle in my application

In my app, i need to send a Two dimensional Array and two more integer values form on Activity to Another with the help of Intent method.
This is done as ..
Intent i = new Intent(getApplicationContext(), ViewActivity.class);
Bundle postbundle = new Bundle();
String[][] X={{"abc"},{"def"}};
postbundle.putSerializable("data", X);
i.putExtra("A", postbundle);
i.putExtra("albumid", position);
i.putExtra("Bigcard",bigcard);
here am using .putSerializable method to place an array into bundle.
So to access these data in the receiver Activity am using
Bundle bundle = getIntent().getBundleExtra("A");
String[][] ABC=(String[][]) bundle.getSerializable("data");
Log.e("Array is",""+ABC);
but I got java.lang.NullPointerException error message..
Whith out use of " Static " declaration how can i get these values from bundle here (in the receiver Activity..)
Let me out pls from this ..
step-1:Write a seperate bean class and save into another file
public class MyBean implements Serializable{
String[][] data = null;
public void set2DArray(String[][] data){
this.data = data;
}
public String[][] get2DArray(){
return data;
}
}
step-2:In the calling activity
Intent intent = new Intent(this, Second.class);
String data[][] = new String[][] {{"1","kumar"},{"2","sona"},{"3","kora"},{"1","pavan"},{"2","kumar"},{"3","kora333"}};
MyBean bean = new MyBean();
bean.set2DArray(data);
Bundle b = new Bundle();
b.putSerializable("mybean", bean);
intent.putExtra("obj", b);
startActivity(intent);
step-3:In the caller activity
Bundle b = getIntent().getBundleExtra("obj");
MyBean dData = (MyBean) b.getSerializable("mybean");
String[][] str =dData.get2DArray();
Not a real answer, but a try:
what happens if You try:
Intent intent = getIntent();
int a = intent.getIntExtra("albumid"); //if Your value is an int, otherwise use String
//getStringExtra or whatever Your value is

Passing Integer Between Activities and Intents in Android Is Always Resulting in Zero / Null

I'm attempting to pass two integers from my Main Page activity (a latitude and longitude) to a second activity that contains an instance of Google Maps that will place a marker at the lat and long provided. My conundrum is that when I retrieve the bundle in the Map_Page activity the integers I passed are always 0, which is the default when they are Null. Does anyone see anything glaringly wrong?
I have the following stored in a button click OnClick method.
Bundle dataBundle = new Bundle();
dataBundle.putInt("LatValue", 39485000);
dataBundle.putInt("LongValue", -80142777);
Intent myIntent = new Intent();
myIntent.setClassName("com.name.tlc", "com.name.tlc.map_page");
myIntent.putExtras(dataBundle);
startActivity(myIntent);
Then in my map_page activity I have the following in onCreate to pick up the data.
Bundle extras = getIntent().getExtras();
System.out.println("Get Intent done");
if(extras !=null)
{
System.out.println("Let's get the values");
int latValue = extras.getInt("latValue");
int longValue = extras.getInt("longValue");
System.out.println("latValue = " + latValue + " longValue = " + longValue);
}
Geeklat,
You don't need to use Bundle in this case.
Do your puts like this...
Intent myIntent = new Intent();
myIntent.setClassName("com.name.tlc", "com.name.tlc.map_page");
myIntent.putExtra("LatValue", (int)39485000);
myIntent.putExtra("LongValue", (int)-80142777);
startActivity(myIntent);
Then you can retrieve them with...
Bundle extras = getIntent().getExtras();
int latValue = extras.getInt("LatValue");
int longValue = extras.getInt("LongValue");
System.out.println("Let's get the values");
int latValue = extras.getInt("latValue");
int longValue = extras.getInt("longValue");
Not the same as
myIntent.putExtra("LatValue", (int)39485000);
myIntent.putExtra("LongValue", (int)-80142777);
Also it might be because you do not keep the name of the Int exactly the same throughout your code. Java and the Android SDK are Case-sensitive

Categories

Resources