2016-01-28 22 views
0

我有一个问题,从Mainacitivy获取数据到一个新的活动。 现在我只是在一个Maketoast中获取数据,它工作正常,但是当我开始一个意图时,我缺少概述。Android的SQLite数据到新的活动

//my buttonclicker which get the toast message, and starting the intent 
//at the top i have an edit text and the whole code is in MainActivity 
    final EditText et1=(EditText)findViewById(R.id.editText1); 
//and a button 
    final Button b1=(Button) findViewById(R.id.button1); 

//display contact 
     private void DisplayContact(Cursor c) 
     { 
      // TODO Auto-generated method stub 
      Toast.makeText(getBaseContext(),"id: " + c.getString(0) + "\n" +"Name: " + c.getString(1) + "\n" + 
      "Email: " + c.getString(2), 
        Toast.LENGTH_LONG).show(); 
     } 


    }); 


    b3.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      db.open(); 
      Bundle dataBundle = new Bundle(); 

     Intent intent = new Intent(MainActivity.this, DisplayCv.class); 

      Cursor c = db.getContact(Integer.parseInt 
        (et1.getText().toString())); 
      if (c.moveToFirst()) 

       DisplayContact(c); 

      startActivity(intent); 


      db.close(); 
      } 

//My display cv activity, im not quite sure what to do here, much of the code 
    isn´t used, just searching for a some simple example to get the name out. 

    public class DisplayCv extends Activity { 
private DBAdapter mydb; 

TextView navn; 
TextView telefon; 
TextView email; 
TextView addresse; 
TextView bynavn; 
TextView sendemail; 
int id_To_Update = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    //super referer til parent constructor 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.displaycv); 

    navn = (TextView) findViewById(R.id.editTextName); 
    telefon = (TextView) findViewById(R.id.editTextPhone); 
    email = (TextView) findViewById(R.id.editTextEmail); 
    addresse = (TextView) findViewById(R.id.editTextStreet); 
    bynavn = (TextView) findViewById(R.id.editTextCity); 
    final Button bdelete = (Button) findViewById(R.id.button2);//deletebutton 
    final TextView tx1 = (TextView) findViewById(R.id.textView1); 


    mydb = new DBAdapter(this); 

    Bundle extras = getIntent().getExtras(); 

    if (extras != null) { 
     int Value = extras.getInt("id"); 

     if (Value > 0) { 
      Cursor rs = mydb.getContact(Value); 
      id_To_Update = Value; 
      rs.moveToFirst(); 






      if (!rs.isClosed()) { 
       rs.close(); 
      } 


      navn.setText((CharSequence) nam); 
      navn.setFocusable(false); 
      navn.setClickable(false); 

      addresse.setText((CharSequence) stre); 
      addresse.setFocusable(false); 
      addresse.setClickable(false); 

      email.setText((CharSequence) ema); 
      email.setFocusable(false); 
      email.setClickable(false); 

      navn.setEnabled(true); 
      navn.setFocusableInTouchMode(true); 
      navn.setClickable(true); 

      email.setEnabled(true); 
      email.setFocusableInTouchMode(true); 
      email.setClickable(true); 

      addresse.setEnabled(true); 
      addresse.setFocusableInTouchMode(true); 
      addresse.setClickable(true); 


     } 
    } 

    } 
    } 

//and my displaycv.xml file 

    <?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/scrollView1" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
tools:context=".DisplayCv" > 
<!--lav en button2 delete i din displayCV--> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="370dp" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin"> 

    <TextView 
     android:id="@+id/editTextName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_marginTop="5dp" 
     android:layout_marginLeft="82dp" 
     android:ems="10" 
     android:text="@string/navn" 
     android:inputType="text" > 
    </TextView> 


    </RelativeLayout> 
    </ScrollView> 

回答

0

您需要将字符串放入意图。变化:

Intent intent = new Intent(MainActivity.this, DisplayCv.class); 

要:

Intent intent = new Intent(MainActivity.this, DisplayCv.class); 
String email = "Email: " + c.getString(2); 
intent.putExtra("email", email); 

检索条目:

Bundle extras = getIntent().getExtras(); 
email = extras.getString("email");