2

ここにヘッドスクラッチャーがあります...(少なくとも私にとっては)私のデータベースからの連絡先のリストを表示する連絡先リストがあります。ユーザーが連絡先の 1 つをクリックすると、編集アクティビティが表示されます。現在のレイアウトどおりにすべてが完全に機能しますが、編集アクティビティで名の前に姓のエントリを表示する必要があります。すべてのフィールドが 1 対 1 の関係を持つ必要があると考えて、先に進み、編集アクティビティで姓の editText(XML) を名の上に移動しました。これは EditText の ID で参照する必要があると考えました。その後、プログラムは姓フィールドに名を表示し、その逆も同様です。エミュレーターでユーザーデータを消去してみましたが、違いはありません。私はすでにこれがおそらくそれらの 1 つであることを理解しています。質問を入力しますが、誰かが私にとって明白なことを指摘できる場合は、それはありがたいです。表示されているすべてのコードは、現在動作中の状態です。

私の問題とは関係のないチャンクをいくつか削除しました。私のためにこれを見てくれた人に感謝します!ケン

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 を呼び出す contact アクティビティです。

    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 に答える