2014-05-10 36 views
0

得到了一個空白的佈局,而不是要求佈局(add_new_contact) 1.MainActivity.java獲取一個留白的佈局設計(可能activity_main.xml中),而不是要求佈局(add_new_contact.xml)

package com.example.addressbookapp; 

import java.util.ArrayList; 
import java.util.HashMap; 

import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.ActionBar; 
import android.support.v4.app.Fragment; 
import android.app.ListActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 
import android.os.Build; 

public class MainActivity extends ListActivity { 

    // The Intent is used to perform an operation 
    Intent  intent; 
    TextView contactId; 

    DBTools dbTools = new DBTools(this); 

    // Method invoked first, when the Activity is first called 
    protected void onCreate(Bundle savedInstanceState) { 

     //Get saved data, if any 
     super.onCreate(savedInstanceState); 
     //Set the main view 
     setContentView(R.layout.fragment_main); 
     //Get all data from database and store it in an array 
     ArrayList<HashMap<String, String>> contactList = dbTools.getAllContacts(); 

     if (contactList.size() != 0) { 
      ListView listView = getListView(); 

      listView.setOnItemClickListener(new OnItemClickListener(){ 

       @Override 
       public void onItemClick(AdapterView<?> arg0, View view, 
         int arg2, long arg3) { 

        contactId = (TextView) view.findViewById(R.id.contactId); 
        String contactIdValue = contactId.getText().toString(); 

        // Intent to get application that owns this Activity 
        Intent theIntent = new Intent(getApplication(), edit_contact.class); 
        // Put additional key value pair to use in Edit Contact 
        theIntent.putExtra("contactId", contactIdValue); 
        startActivity(theIntent); 

       } 

      }); 


      // List Adapter is a bridge between ListView and ListView data 
      ListAdapter adapter = new SimpleAdapter(
        MainActivity.this, contactList, R.layout.contact_entry, 
        new String[] {"contactId", "lastName", "firstName"}, 
        new int[] {R.id.contactId, R.id.lastName, R.id.firstName}); 

      // setListAdapter provides cursor for the list view 
      // The cursor provides access to the data 
      setListAdapter(adapter); 
     } 

    } 

    // Call the below function showAddContact when New Contact is called 
    //   showAddContact 
    public void showAddContact(View view) { 
     Intent theIntent = new Intent(getApplication(), NewContact.class); 
     startActivity(theIntent); 
    } 
} 

2.NewContact的.java

package com.example.addressbookapp; 

import java.util.HashMap; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 

public class NewContact extends Activity { 

    EditText firstName; 
    EditText lastName; 
    EditText phoneNumber; 
    EditText emailAddress; 
    EditText homeAddress; 

    DBTools dbTools = new DBTools(this); 

    public void onCreated(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.add_new_contact); 

     firstName = (EditText) findViewById(R.id.firstName); 
     lastName = (EditText) findViewById(R.id.lastName); 
     phoneNumber = (EditText) findViewById(R.id.phoneNumber); 
     emailAddress = (EditText) findViewById(R.id.emailAddress); 
     homeAddress = (EditText) findViewById(R.id.homeAddress); 

    } 

    public void addNewContact(View view){ 
     HashMap<String, String> queryValuesMap = new HashMap<String, String>(); 

     queryValuesMap.put("firstName", firstName.getText().toString()); 
     queryValuesMap.put("lastName", lastName.getText().toString()); 
     queryValuesMap.put("phoneNumber", phoneNumber.getText().toString()); 
     queryValuesMap.put("emailAddress", emailAddress.getText().toString()); 
     queryValuesMap.put("homeAddress", homeAddress.getText().toString()); 

     dbTools.insertContact(queryValuesMap); 
     this.callMainActivity(view); 
    } 

    public void callMainActivity(View view){ 
     Intent theIntent = new Intent(getApplication(), MainActivity.class); 
     startActivity(theIntent); 
    } 
} 

3.add_new_contact.xml

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#000"> 

     <TextView 
      android:id="@+id/editContactTextView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="@dimen/padding_5dp" 
      android:text="@string/add_button" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textColor="#FFF" 
      /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/fistNameTextView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:padding="@dimen/padding_5dp" 
      android:text="@string/first_name" /> 

     <EditText 
      android:id="@+id/firstName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" 
      android:inputType="textPersonName" 
      android:padding="@dimen/padding_5dp" > 

      <requestFocus /> 
     </EditText> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/lastNameTextView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:padding="@dimen/padding_5dp" 
      android:text="@string/last_name" /> 

     <EditText 
      android:id="@+id/lastName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" 
      android:inputType="textPersonName" 
      android:padding="@dimen/padding_5dp" > 

     </EditText> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow4" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/phoneNumberTextView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:padding="@dimen/padding_5dp" 
      android:text="@string/phone_number" /> 

     <EditText 
      android:id="@+id/phoneNumber" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" 
      android:inputType="phone" 
      android:padding="@dimen/padding_5dp" > 

     </EditText> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow5" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/emailTextView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:padding="@dimen/padding_5dp" 
      android:text="@string/email_address" /> 

     <EditText 
      android:id="@+id/emailAddress" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" 
      android:inputType="textEmailAddress" 
      android:padding="@dimen/padding_5dp" > 
     </EditText> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow6" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/addressTextView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:padding="@dimen/padding_5dp" 
      android:text="@string/home_address" /> 

     <EditText 
      android:id="@+id/homeAddress" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" 
      android:inputType="textPostalAddress" 
      android:padding="@dimen/padding_5dp" > 
     </EditText> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <Button 
      android:id="@+id/addButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/save_data" 
      android:onClick="addNewContact" 
      android:layout_weight="1" 
      /> 
    </TableRow>  
</TableLayout> 

4. AndroidManifes t.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.addressbookapp" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="9" 
     android:targetSdkVersion="19" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.addressbookapp.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.example.addressbookapp.NewContact" 
      android:label="" > 
     </activity> 
     <activity 
      android:name="com.example.addressbookapp.edit_contact" 
      android:label="" > 
     </activity> 
    </application> 

</manifest> 

回答

0

你不重寫onCreate(Bundle savedInstanceState),你聲明的方法onCreated(Bundle savedInstanceState)

/* 
* yours 
*/ 
public void onCreated(Bundle savedInstanceState){ 

/* 
* should be 
*/ 
@Override  
public void onCreate(Bundle savedInstanceState){ 
+0

你什麼時候重寫和什麼時候不重複。沒有覆蓋editcontact並且正在工作 – user1509593

+0

您不一定需要'@ Override'註釋,但它可以讓編譯器警告您這些事情。在你的情況下,它會說「不覆蓋父方法」。 –