2013-06-13 37 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"); 
相关问题