Android: help pass value from one class to another
this is the first class, displays listview from sql and pops up a option on longpress. I wanted to pass the ID number of the current select row to be processed on another class.
Please help. thanks.
/** Called when the activity is 开发者_JAVA百科first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select);
mListUsers = getUsers();
lvUsers = (ListView) findViewById(R.id.lv_user);
lvUsers.setAdapter(new ListAdapter(this, R.id.lv_user, mListUsers));
// needed for longpress thing
registerForContextMenu(lvUsers);
}
/**
* all for long press thingy
*/
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
menu.setHeaderTitle("Selection");
inflater.inflate(R.layout.list_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
Intent intent = new Intent(Select.this, Update.class);
startActivity(intent);
return true;
case R.id.delete:
return true;
default:
return super.onContextItemSelected(item);
}
}
/**
* end of long press thingy
*
*/
You can use intent.putExtra to pass primitive types from one Activity to another. Do this before starting the Activity with startActivity().
you got to implement onListItemClick method inside that
**Intent intent = new Intent(current.getContext(), desired.class);
intent.putExtra("id", id);
startActivityForResult(intent, 0);**
and in the desired class put this code
**Long id = getIntent().getLongExtra("id", 0);**
you can try out this
When you say passing the value to another "class" do you mean another Activity, or another standard Java class?
If you want to pass that ID into another android activity, then you can put it into an Intent, and call startActivity on the target activity. Have a look at this
Intent i = new Intent(this, ClassToSendTo.class);
i.putExtra("id", id);
startActivity(i);
Also, you could (might not be the best option) but add it to the application context so you can access it elsewhere. I don't think this is your best option, but its worth bearing in mind. I wrote up how to do that here.
If you just want to pass it to another class, which is not an Activity, then you can do what you normally would do in Java, such as :
MyClass myClass = new MyClass();
myClass.doSomething(id);
in your case . add the code
case R.id.edit:
AdapterView.AdapterContextMenuInfo infoCode = (AdapterView.AdapterContextMenuInfo) item
.getMenuInfo();
addId(infoCode.id);
return (true);
then create in your current activity, addId() method
private void addCode(final long rowId) {
if (rowId > 0) {
// load your sqlite
// your database handler
DatabaseHelper db = new DatabaseHelper(getApplicationContext());
SQLiteDatabase dbs = db.getReadableDatabase();
//query
Cursor cursor = dbs.rawQuery("SELECT * FROM your_table where _ID="
+ rowId + "", null);
if (cursor.moveToFirst()) {
do {
String numberId = cursor.getString(0); // Here you can get data
// from table and stored
// in string if it has
// only one string.
// if you add another field
//String fieldCode = cursor.getString(1);
//String fieldCode = cursor.getString(2);
Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
intent.putExtra("new_variable_name",numberId);
startActivity(intent);
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
if (db != null) {
db.close();
}
in the new activity, add the code in onCreate.
//if your send it to edittext (Edit)
TextView txt = (TextView) findViewById(R.id.youreditextid);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
txt.setText(value);
}
精彩评论