Android更改XML布局更改数据字段

时间:2013-04-19 20:36:11

标签: java android xml

这是一个令人头疼的问题......(至少对我而言) 我有一个联系人列表,显示我的Db中的联系人列表。当用户点击其中一个联系人时,会出现编辑活动。它完全按照目前的布局工作,但我需要让编辑活动显示名字前的姓氏。考虑到所有字段应该具有一对一关系,我继续将editText(XML)移动到编辑活动中第一个名称上方的姓氏,认为这应该由EditText的id引用。执行此操作后,程序现在在姓氏字段中显示第一个名称,反之亦然。我试过在模拟器上擦除用户数据没有区别。我已经意识到这可能是UH-DUH之一!键入问题,但如果有人可以为我指出明显的问题,我们将不胜感激。显示的所有代码都处于现在工作状态:

我删除了一些与我的问题无关的块。 感谢有人为我看看这个! 肯

XML:

     <EditText
        android:id="@+id/contact_edit_first_name"
        android:inputType="textPersonName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/contact_edit_first_name"
        android:imeOptions="actionNext"
        android:background="@color/warn" >
    </EditText>
     <EditText
        android:id="@+id/contact_edit_last_name"
        android:inputType="textPersonName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="top"
        android:hint="@string/contact_edit_last_name"
        android:imeOptions="actionNext"
        android:background="@color/warn" >
    </EditText>

这是显示listView行和调用的联系活动 createContact,用于发送添加,编辑或删除行的意图。

    public class ContactsActivity extends ListActivity implements
    LoaderManager.LoaderCallbacks<Cursor> {

  private SimpleCursorAdapter adapter; 

/** Called when the activity is first created. */
          @Override
  public void onCreate //DO THE ON CREATE STUFF -removed

    fillData();
    registerForContextMenu(getListView());

    Button add_contact = (Button) findViewById(R.id.add_contact_button);

    add_contact.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            createContact();
        }
      });
   }

  // Create the options menu to INSERT from the XML file
  // removed - not relevant
  // return true for the menu to be displayed
  }

  // When the insert menu item is selected, call CreateContact
  //Removed
      createContact();
      return true;
    }
    return super.onOptionsItemSelected(item);
  }

  private void createContact() {
        Intent i = new Intent(this, ContactEditActivity.class);
        startActivity(i);
      }

   //The onListItemClick sends a URI which flags the contactEditActivity
   //that this is an edit rather than a new insert.


  @Override
  protected void onResume() {
   super.onResume();
   //Starts a new or restarts an existing Loader in this manager
   getLoaderManager().restartLoader(0, null, this);
  }

   //The fillData method binds the simpleCursorAadapter to the listView.
  private void fillData() {

    //The desired columns to be bound:
    String[] from = new String[] { ContactsDB.COLUMN_LAST_NAME, ContactsDB.COLUMN_FIRST_NAME };

    //The XML views that the data will be bound to:
    int[] to = new int[] {R.id.label2, R.id.label};
    // The creation of a loader using the initLoader method call.

    getLoaderManager().initLoader(0, null, this);
    adapter = new SimpleCursorAdapter(this, R.layout.contact_row, null, from,
        to, 0);
    setListAdapter(adapter);
  }
  // Sort the names by last name, then by first name
  String orderBy = ContactsDB.COLUMN_LAST_NAME + " COLLATE NOCASE ASC"
  + "," + ContactsDB.COLUMN_FIRST_NAME + " COLLATE NOCASE ASC" ;

  // Creates a new loader after the initLoader () call
  @Override
  public Loader<Cursor> onCreateLoader(int id, Bundle args) {
//ETC
  }

  @Override
  public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    adapter.swapCursor(data); //Call requires Min API 11
  }

  @Override
  public void onLoaderReset(Loader<Cursor> loader) {
    // swap the cursor adapter

} 

最后,这是联系人编辑代码,可能是我悲伤的根源......也许不是。可能是保存状态没有映射到id的?

  @Override
  protected void onCreate(Bundle bundle) {
    super.onCreate(bundle); 
    setContentView(R.layout.activity_contact_edit);

    Log.i(TAG, "INSIDE ONCREATE");

    mCategory = (Spinner) findViewById(R.id.category);
    mLastName = (EditText) findViewById(R.id.contact_edit_last_name);
    mFirstName = (EditText) findViewById(R.id.contact_edit_first_name);
    mHomePhone = (EditText) findViewById(R.id.contact_edit_home_phone);
    mCellPhone = (EditText) findViewById(R.id.contact_edit_cell_phone);
//****************ECT. ETC.

   //DECLARE THE BUTTONS AND SET THE DELETE ENABLED FALSE - REMOVED - NOT PERTINANT
    Bundle extras = getIntent().getExtras();

    // Check if the URI is from a new instance or a saved record
    }

    // Set the save button to check the required fields, save the contact and finish
    saveButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        if (TextUtils.isEmpty(mLastName.getText().toString()) ||
                TextUtils.isEmpty(mFirstName.getText().toString())) {
          makeToast();
        } else {
          setResult(RESULT_OK);
          finish();
        }
      }
    });

    // Set the delete button to delete the contact and finish - REMOVED - NOT PERTINANT

  private void fillData(Uri uri) {

    // QUERY PARAMETER projection - A list of which columns to return.
    // Passing null will return all columns, which is inefficient (but used now!)
    // null, null and null are: selection, selection args, and sort order for specific items

    Cursor cursor = getContentResolver().query(uri, null, null, null, null); 

    if (cursor != null) {
      cursor.moveToFirst();
      String category = cursor.getString(cursor
          .getColumnIndexOrThrow(ContactsDB.COLUMN_CATEGORY));
      for (int i = 0; i < mCategory.getCount(); i++) {
          String s = (String) mCategory.getItemAtPosition(i);
          Log.i("CATEGORY", s); ////////////////////////////////////////////
        if (s.equalsIgnoreCase(category)) {
          mCategory.setSelection(i);
        }
      };

      mLastName.setText(cursor.getString(cursor
          .getColumnIndexOrThrow(ContactsDB.COLUMN_LAST_NAME)));
      mFirstName.setText(cursor.getString(cursor
              .getColumnIndexOrThrow(ContactsDB.COLUMN_FIRST_NAME)));
      mHomePhone.setText(cursor.getString(cursor
              .getColumnIndexOrThrow(ContactsDB.COLUMN_PHONE_NUMBER)));
      mCellPhone.setText(cursor.getString(cursor
              .getColumnIndexOrThrow(ContactsDB.COLUMN_CELL_NUMBER)));
      mWorkPhone.setText(cursor.getString(cursor
              .getColumnIndexOrThrow(ContactsDB.COLUMN_WORK_NUMBER)));
      mFax.setText(cursor.getString(cursor
//****************ECT. ETC.  
      //close the cursor
    }
  }

  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    saveState();
    outState.putParcelable(whateverContentProvider.CONTENT_ITEM_TYPE, contactUri);
  }

  @Override
  protected void onPause() {
    super.onPause();
    saveState();
  }
  private void saveState() {
    String category = (String) mCategory.getSelectedItem();
    String someLAST = mLastName.getText().toString().valueOf(findViewById(R.id.contact_edit_last_name));
    String lastName = mLastName.getText().toString();
    String firstName = mFirstName.getText().toString();
    String someFIRST = mFirstName.getText().toString().valueOf(findViewById(R.id.contact_edit_first_name));
    String homePhone = mHomePhone.getText().toString();
    String somePhone = mHomePhone.getText().toString().valueOf(findViewById(R.id.contact_edit_home_phone));
    String cellPhone = mCellPhone.getText().toString();
    String workPhone = mWorkPhone.getText().toString();
//****************ECT. ETC.
    //Some logging I used to show that the first name field still came up first
    //after changing the order of the editTexts.
    Log.i("LAST NAME", lastName);
    Log.i("SOME LAST", someLAST);
    Log.i("FIRST NAME", firstName);
    Log.i("SOME FIRST", someFIRST);
    Log.i("Home Phone", homePhone);
    Log.i("SOME PHONE", somePhone);


    // Save if first name and last name are entered
    // The program will save only last name when a user presses back button with text in last name

    if (lastName.length() == 0 || firstName.length() == 0) {
      return;
    }
    // ContentValues class is used to store a set of values that the contentResolver can process.
    ContentValues values = new ContentValues();
    values.put(ContactsDB.COLUMN_CATEGORY, category);
    values.put(ContactsDB.COLUMN_LAST_NAME, lastName);//ANNIE
    values.put(ContactsDB.COLUMN_FIRST_NAME, firstName);
    values.put(ContactsDB.COLUMN_PHONE_NUMBER, homePhone); 
//****************ECT. ETC.

    if (contactUri == null) {
      // Create a new contact
      contactUri = getContentResolver().insert(whateverContentProvider.CONTENT_URI, values);
    } else {
      // Update an existing contact
      getContentResolver().update(contactUri, values, null, null);
    }
  }
//MAKE A TOAST DOWN HERE - REMOVED - NOT PERTINANT
}

2 个答案:

答案 0 :(得分:2)

您是否尝试过清洁项目(再生de R)。 另外,请尝试重新启动IDE。

这可能看起来很愚蠢,但实际上可以解决问题......

答案 1 :(得分:2)

尝试清理您的项目。 Eclipse中有时会发生奇怪的事情。