Android:帮助将值从一个类传递到另一个类

时间:2010-11-24 11:27:39

标签: android

这是第一个类,从sql显示listview并在longpress上弹出一个选项。我想传递要在另一个类上处理的当前选择行的ID号。

请帮忙。感谢。

/** Called when the activity is 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
 * 
 */

4 个答案:

答案 0 :(得分:1)

您可以使用intent.putExtra将原始类型从一个Activity传递到另一个Activity。在使用startActivity()启动Activity之前执行此操作。

答案 1 :(得分:1)

你必须实现onListItemClick方法 在那里

**Intent intent = new Intent(current.getContext(), desired.class);                        
intent.putExtra("id", id);                       
startActivityForResult(intent, 0);**

并在所需的类中放入此代码

**Long id = getIntent().getLongExtra("id", 0);**

你可以试试这个

答案 2 :(得分:1)

当你说将值传递给另一个“类”时,你的意思是另一个Activity,还是另一个标准的Java类?

如果要将该ID传递给另一个Android活动,则可以将其置于Intent中,并在目标活动上调用startActivity。看看this

Intent i = new Intent(this, ClassToSendTo.class);
i.putExtra("id", id);
startActivity(i);

此外,您可以(可能不是最佳选择)将其添加到应用程序上下文中,以便您可以在其他位置访问它。我不认为这是你最好的选择,但值得记住。我写了怎么做here

如果您只想将它​​传递给另一个不是Activity的类,那么您可以执行通常在Java中执行的操作,例如:

MyClass myClass = new MyClass();
myClass.doSomething(id);

答案 3 :(得分:1)

在你的情况下。添加代码

case R.id.edit:
            AdapterView.AdapterContextMenuInfo infoCode = (AdapterView.AdapterContextMenuInfo) item
            .getMenuInfo();

            addId(infoCode.id);
            return (true);

然后在当前活动中创建addId()方法

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();
            }

在新活动中,在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);
        }
相关问题