2

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

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

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
}
4

2 回答 2

2

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

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

于 2013-04-19T20:48:13.273 回答
2

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

于 2013-04-19T20:58:52.877 回答