2010-11-24 137 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); 
     } 
相關問題