Advertisement
Guest User

Untitled

a guest
Jan 27th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. public class SampleChooserActivity extends Activity {
  2.  
  3. @Override
  4. public void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.sample_chooser_activity);
  7.  
  8. ListView sampleList = (ListView) findViewById(R.id.sample_list);
  9. final SampleAdapter sampleAdapter = new SampleAdapter(this);
  10.  
  11. sampleAdapter.add(new Header("Local Videos"));
  12. sampleAdapter.addAll((Object[]) Samples.LOCAL_VIDEOS);
  13.  
  14. sampleList.setAdapter(sampleAdapter);
  15. sampleList.setOnItemClickListener(new OnItemClickListener() {
  16. @Override
  17. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  18. Object item = sampleAdapter.getItem(position);
  19. if (item instanceof Samples.Sample) {
  20. onSampleSelected((Samples.Sample) item);
  21. }
  22. }
  23. });
  24. }
  25.  
  26. private void onSampleSelected(Samples.Sample sample) {
  27. Intent mpdIntent = new Intent(this, PlayerActivity.class)
  28. .setData(Uri.parse(sample.uri))
  29. .putExtra(PlayerActivity.CONTENT_ID_EXTRA, sample.contentId)
  30. .putExtra(PlayerActivity.CONTENT_TYPE_EXTRA, sample.type)
  31. .putExtra(PlayerActivity.PROVIDER_EXTRA, sample.provider);
  32. startActivity(mpdIntent);
  33. }
  34.  
  35. private static class SampleAdapter extends ArrayAdapter<Object> {
  36.  
  37. public SampleAdapter(Context context) {
  38. super(context, 0);
  39. }
  40.  
  41. @Override
  42. public View getView(int position, View convertView, ViewGroup parent) {
  43. View view = convertView;
  44. if (view == null) {
  45. int layoutId = getItemViewType(position) == 1 ? android.R.layout.simple_list_item_1
  46. : R.layout.sample_chooser_inline_header;
  47. view = LayoutInflater.from(getContext()).inflate(layoutId, null, false);
  48. }
  49. Object item = getItem(position);
  50. String name = null;
  51. if (item instanceof Samples.Sample) {
  52. name = ((Samples.Sample) item).name;
  53. } else if (item instanceof Header) {
  54. name = ((Header) item).name;
  55. }
  56. ((TextView) view).setText(name);
  57. return view;
  58. }
  59.  
  60. @Override
  61. public int getItemViewType(int position) {
  62. return (getItem(position) instanceof Samples.Sample) ? 1 : 0;
  63. }
  64.  
  65. @Override
  66. public int getViewTypeCount() {
  67. return 2;
  68. }
  69.  
  70. }
  71.  
  72. private static class Header {
  73.  
  74. public final String name;
  75.  
  76. public Header(String name) {
  77. this.name = name;
  78. }
  79.  
  80. }
  81.  
  82. }
  83.  
  84. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  85. android:layout_width="match_parent"
  86. android:layout_height="match_parent"
  87. android:orientation="vertical">
  88.  
  89. <ListView android:id="@+id/sample_list"
  90. android:layout_width="match_parent"
  91. android:layout_height="match_parent"/>
  92.  
  93. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement