Skip to content

Commit

Permalink
1.21 Clicking on list item opens EditorActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
Beginning Android committed Aug 23, 2016
1 parent e587771 commit c0d42fb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
</activity>
<activity
android:name=".EditorActivity"
android:label="@string/editor_activity_title_new_pet"
android:theme="@style/EditorTheme"
android:parentActivityName=".CatalogActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/example/android/pets/CatalogActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.example.android.pets;

import android.app.LoaderManager;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.content.Intent;
Expand All @@ -28,6 +29,7 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.android.pets.data.PetContract.PetEntry;
Expand Down Expand Up @@ -71,6 +73,28 @@ public void onClick(View view) {
mCursorAdapter = new PetCursorAdapter(this, null);
petListView.setAdapter(mCursorAdapter);

// Setup the item click listener
petListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
// Create new intent to go to {@link EditorActivity}
Intent intent = new Intent(CatalogActivity.this, EditorActivity.class);

// Form the content URI that represents the specific pet that was clicked on,
// by appending the "id" (passed as input to this method) onto the
// {@link PetEntry#CONTENT_URI}.
// For example, the URI would be "content://com.example.android.pets/pets/2"
// if the pet with ID 2 was clicked on.
Uri currentPetUri = ContentUris.withAppendedId(PetEntry.CONTENT_URI, id);

This comment has been minimized.

Copy link
@minaisaacfawzy

minaisaacfawzy Aug 6, 2018

this id is the id of the item in the list ....how can we guarantee that it is the same in the data base?


// Set the URI on the data field of the intent
intent.setData(currentPetUri);

// Launch the {@link EditorActivity} to display the data for the current pet.
startActivity(intent);
}
});

// Kick off the loader
getLoaderManager().initLoader(PET_LOADER, null, this);
}
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/com/example/android/pets/EditorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.example.android.pets;

import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
Expand Down Expand Up @@ -61,6 +62,21 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editor);

// Examine the intent that was used to launch this activity,
// in order to figure out if we're creating a new pet or editing an existing one.
Intent intent = getIntent();
Uri currentPetUri = intent.getData();

// If the intent DOES NOT contain a pet content URI, then we know that we are
// creating a new pet.
if (currentPetUri == null) {
// This is a new pet, so change the app bar to say "Add a Pet"
setTitle(getString(R.string.editor_activity_title_new_pet));
} else {
// Otherwise this is an existing pet, so change app bar to say "Edit Pet"
setTitle(getString(R.string.editor_activity_title_edit_pet));
}

// Find all relevant views that we will need to read user input from
mNameEditText = (EditText) findViewById(R.id.edit_pet_name);
mBreedEditText = (EditText) findViewById(R.id.edit_pet_breed);
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
<!-- Title for the activity to add a new pet [CHAR LIMIT=20] -->
<string name="editor_activity_title_new_pet">Add a Pet</string>

<!-- Title for the activity to edit an existing pet [CHAR LIMIT=20] -->
<string name="editor_activity_title_edit_pet">Edit Pet</string>

<!-- Label for editor menu option to save pet and leave editor [CHAR LIMIT=20] -->
<string name="action_save">Save</string>

Expand Down

1 comment on commit c0d42fb

@krunal2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • [ ]

Please sign in to comment.