2010-11-24 139 views
2

这是第一个类,从sql显示listview并在longpress上弹出一个选项。我想传递当前选择行的ID号码以在另一个类上处理。Android:帮助将值从一个类传递到另一个类

请帮忙。谢谢。

/** 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 
* 
*/ 

回答

1

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

1

你一定要实现onListItemClick内部的

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

,并把这段代码

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

你可以尝试一下这个

1

当你说传递值所需的类方法 到另一个“类”你的意思是另一个活动,或另一个标准的Java类?

如果您想将该ID传递给另一个android活动,那么您可以将其放入一个Intent中,并在目标活动上调用startActivity。看看this

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

此外,您可以(可能不是最好的选择),但它添加到应用程序上下文,这样你可以在其他地方访问它。我不认为这是你最好的选择,但它值得牢记。我写了如何做到这一点here

如果你只是想将它传递给另一个类,这是不是一个活动,那么你可以做你通常会在Java中做什么,如:

MyClass myClass = new MyClass(); 
myClass.doSomething(id); 
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); 
     } 
相关问题