2013-06-13 63 views
2

我有兩個活動是朋友和細節,朋友的活動是一個列表視圖與列表數據從我已經創建的數據庫填充,當列表項被點擊時,細節活動應該啓動並列表項目數據進行的細節活動和投入在編輯文本框的細節類從一個活動獲取數據到另一個

package com.rich.myfinal; 

public class FriendsActivity extends Activity { 

    private CustomCursorAdapter customAdapter; 
    private PersonDataHelper databaseHelper; 
    private ListView listView; 

    private static final String TAG = FriendsActivity.class.getSimpleName(); 
    /** 
    * Called when the activity is first created. 
    */ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_friends); 

     databaseHelper = new PersonDataHelper(this); 

     listView = (ListView) findViewById(R.id.list_data); 
     listView.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Log.d(TAG, "clicked on item: " + position); 
       Intent i = new Intent(getApplicationContext(), DetailsActivity.class); 
       startActivity(i); 
      } 
     }); 

     // Database query can be a time consuming task .. 
     // so its safe to call database query in another thread 
     // Handler, will handle this stuff for you <img src="http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif?m=1129645325g" alt=":)" class="wp-smiley"> 

     new Handler().post(new Runnable() { 
      @Override 
      public void run() { 
       customAdapter = new CustomCursorAdapter(FriendsActivity.this, databaseHelper.getAllData()); 
       listView.setAdapter(customAdapter); 
      } 
     }); 
    } 
} 

細節性低於

package com.rich.myfinal; 

public class DetailsActivity extends Activity { 
    EditText details; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_details); 

     details = (EditText) findViewById (R.id.details); 
    } 
} 
+0

什麼是你面臨的問題? –

+0

我需要代碼的幫助才能將數據複製到詳細信息活動 – Richmahnn

+0

您可能已經搜索過。這個問題每天至少出現一次。這裏有一個例子,在這裏數百...讓我試試看它是一個例子,在這裏數百... http://stackoverflow.com/questions/4860811/how-to-transfer-data-from-one-activity-to-another?rq = 1 – Simon

回答

0

值傳遞你想從FriendsActivityDetailsActivity活動Intent就像下面一樣。從好友活動傳遞值時,您可以通過調用startActivity()

intent.putExtra("pos", v.getId()) ; 
intent.putExtra("TranObject",(Serializable) data.get(v.getId()).getTranse()) ; 

開始另一個活動,然後在DetailsActivity如下面onCreate()

try { 
     pos = getIntent().getExtras().getInt("pos"); 
     blog =(clsTranscation) getIntent().getExtras().getSerializable("TranObject"); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
1

您可以通過兩種方式做到這一點接受的價值觀,一種是採用包和另一個通過意圖。

使用捆綁,

派:

Intent passIntent = new Intent(CurrentActivity.this, SecondActivity.class); 
Bundle bundle = new Bundle(); 
bundle.putString("Key", "Value"); 
passIntent.putExtras(bundle); 
startActivity(passIntent); 

接收:

Bundle bundle = getIntent().getExtras(); 
String recText = bundle.getString("Key"); 

使用通過意圖:

派:

Intent passIntent = new Intent(CurrentActivity.this, SecondActivity.class); 
passIntent.putExtras("Key", "Value"); 
startActivity(passIntent); 

接收:

String recText = getIntent().getExtras().getString("Key"); 

爲您的代碼,在你的FirstActivity

listView.setOnItemClickListener(new OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
        Log.d(TAG, "clicked on item: " + position); 
        Intent passIntent = new Intent(CurrentActivity.this, SecondActivity.class); 
        passIntent.putExtras("Key", position); 
        startActivity(passIntent); 
       } 
      }); 



In SecondActivity, 


details.setText(getIntent().getExtras().getString("Key")); 
+0

讓我試試看 – Richmahnn

0

像使用詳細的活動,

@Override 
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
         Log.d(TAG, "clicked on item: " + position); 
         String value = listView .getItemAtPosition(position).toString(); 
         Intent i = new Intent(getApplicationContext(), DetailsActivity.class); 
         i.putExtra("data", value); 
         startActivity(i); 
        } 
       }); 

使用

Bundle bundle = getIntent().getExtras(); 
String value= bundle.getString("data"); 
details = (EditText) findViewById (R.id.details); 
details.setText(value) 
+0

sunil,在我的編輯文本框中,獲取「[email protected]」是什麼意思? – Richmahnn

+0

這是什麼?在你的詳細部分活動中,沒有任何東西與這段代碼相關。所以它不應該出現,你確定它詳細介紹活動部分? –

+0

這就是我得到的 – Richmahnn

0

就叫String value=YourList.get(position); 並採用intent.putExtra("Key","Value");

通過這在意圖和使用得到這個Bundle bundle=getIntent.getExtra();值,

String value=bundle.getExtra("Key"); 
相關問題