Книга: Beginning Android

Pick ’Em

Pick ’Em

Sometimes you know your content Uri represents a collection of some type, such as content://contacts/people representing the list of contacts in the stock Android contacts list. In this case, you can let the user pick a contact that your activity can then use (e.g., tag it, dial it).

To do this, you need to create an Intent for the ACTION_PICK on the target Uri, then start a sub-activity (via startActivityForResult()) to allow the user to pick a piece of content of the specified type. If your onActivityResult() callback for this request gets a RESULT_OK result code, your data string can be parsed into a Uri representing the chosen piece of content.

For example, take a look at Introspection/Pick in the sample applications in the Source Code section of http://apress.com. This activity gives you a field for a collection Uri (with content://contacts/people pre-filled in for your convenience), plus a really big Gimme! button:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
>
 <EditText android:id="@+id/type"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:cursorVisible="true"
  android:editable="true"
  android:singleLine="true"
  android:text="content://contacts/people"
 />
 <Button
  android:id="@+id/pick"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:text="Gimme!"
  android:layout_weight="1"
 />
</LinearLayout>

Upon being clicked, the button creates the ACTION_PICK on the user-supplied collection Uri and starts the sub-activity. When that sub-activity completes with RESULT_OK, the ACTION_VIEW is invoked on the resulting content Uri.

public class PickDemo extends Activity {
 static final int PICK_REQUEST = 1337;
 private EditText type;
 @Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main);
  type = (EditText)findViewById(R.id.type);
  Button btn = (Button)findViewById(R.id.pick);
  btn.setOnClickListener(new View.OnClickListener() {
   public void onClick(View view) {
    Intent i = new Intent(Intent.ACTION_PICK,
     Uri.parse(type.getText().toString()));
    startActivityForResult(i, PICK_REQUEST);
   }
  });
 }
 @Override
 protected void onActivityResult(int requestCode, int resultCode,
  Intent data) {
  if (requestCode==PICK_REQUEST) { 
   if (resultCode==RESULT_OK) {
    startActivity(new Intent(Intent.ACTION_VIEW,
     data.getData()));
   }
  }
 }
}

The result: the user chooses a collection (Figure 25-1), picks a piece of content (Figure 25-2), and views it (Figure 25-3).


Figure 25-1. The PickDemo sample application, as initially launched


Figure 25-2. The same application, after the user has clicked the Gimme! button, showing the list of available people


Figure 25-3. A view of a contact, launched by PickDemo after the user has chosen one of the people from the pick list

Оглавление книги

Оглавление статьи/книги

Генерация: 1.467. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз