-1

みなさん、こんにちは。これは、YouTubeAPIデモのコードです。リストビューをクリックするためのテキストを表示するのではなく、リストビューをカスタマイズしたかったのですが、これをリストビュー内の各アイテムの特定の画像(描画可能なフォルダからの画像)に置き換えたいと思います。 "..."マイプレイリスト1....画像が表示されるはずです

package com.examples.youtubeapidemo;

import com.google.android.youtube.player.YouTubeIntents;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.examples.youtubeapidemo.adapter.DemoArrayAdapter;
import com.examples.youtubeapidemo.adapter.DemoListViewItem;

import java.util.ArrayList;
import java.util.List;

/**
* A sample activity which shows how to use the {@link YouTubeIntents} static methods     to create
* Intents that navigate the user to Activities within the main YouTube application.
*/
public final class IntentsDemoActivity extends Activity implements OnItemClickListener   {

 // This is the value of Intent.EXTRA_LOCAL_ONLY for API level 11 and above.
 private static final String EXTRA_LOCAL_ONLY = "android.intent.extra.LOCAL_ONLY";
 private static final String VIDEO_ID = "tKIISemrwJ8";
 private static final String PLAYLIST_ID = "PL75F25C99BA786497";
 private static final String PLAYLIST_ID2 = "PL75F25C99BA786497";
 private static final String USER_ID = "felipeneto";
 private static final int SELECT_VIDEO_REQUEST = 1000;

 private List<DemoListViewItem> intentItems;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.intents_demo);

intentItems = new ArrayList<DemoListViewItem>();
intentItems.add(new IntentItem("Show My Channel", IntentType.OPEN_USER));
intentItems.add(new IntentItem("My Playlist 1", IntentType.OPEN_PLAYLIST));
intentItems.add(new IntentItem("My Playlist 2", IntentType.OPEN_PLAYLIST2));
intentItems.add(new IntentItem("Featured", IntentType.PLAY_VIDEO));
intentItems.add(new IntentItem("Search", IntentType.OPEN_SEARCH));


ListView listView = (ListView) findViewById(R.id.intent_list);
DemoArrayAdapter adapter =
    new DemoArrayAdapter(this, R.layout.list_item, intentItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);

TextView youTubeVersionText = (TextView) findViewById(R.id.youtube_version_text);
String version = YouTubeIntents.getInstalledYouTubeVersionName(this);
if (version != null) {
  String text = String.format(getString(R.string.youtube_currently_installed), version);
  youTubeVersionText.setText(text);
} else {
  youTubeVersionText.setText(getString(R.string.youtube_not_installed));
}
 }

 public boolean isIntentTypeEnabled(IntentType type) {
  switch (type) {
  case PLAY_VIDEO:
    return YouTubeIntents.canResolvePlayVideoIntent(this);
  case OPEN_PLAYLIST:
    return YouTubeIntents.canResolveOpenPlaylistIntent(this);
  case OPEN_PLAYLIST2:
      return YouTubeIntents.canResolveOpenPlaylistIntent(this);
  case PLAY_PLAYLIST:
    return YouTubeIntents.canResolvePlayPlaylistIntent(this);
  case OPEN_SEARCH:
    return YouTubeIntents.canResolveSearchIntent(this);
  case OPEN_USER:
    return YouTubeIntents.canResolveUserIntent(this);

}

return false;
 }

  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  IntentItem clickedIntentItem = (IntentItem) intentItems.get(position);

Intent intent;
switch (clickedIntentItem.type) {
  case PLAY_VIDEO:
    intent = YouTubeIntents.createPlayVideoIntentWithOptions(this, VIDEO_ID, true, false);
    startActivity(intent);
    break;    
  case OPEN_PLAYLIST:
    intent = YouTubeIntents.createOpenPlaylistIntent(this, PLAYLIST_ID);
    startActivity(intent);
    break;
  case OPEN_PLAYLIST2:
      intent = YouTubeIntents.createOpenPlaylistIntent(this, PLAYLIST_ID2);
      startActivity(intent);
      break;
  case PLAY_PLAYLIST:
    intent = YouTubeIntents.createPlayPlaylistIntent(this, PLAYLIST_ID);
    startActivity(intent);
    break;
  case OPEN_SEARCH:
    intent = YouTubeIntents.createSearchIntent(this, USER_ID);
    startActivity(intent);
    break;
  case OPEN_USER:
    intent = YouTubeIntents.createUserIntent(this, USER_ID);
    startActivity(intent);
    break;

   }
   }

   @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent returnedIntent) {
if (resultCode == RESULT_OK) {
  switch (requestCode) {
    case SELECT_VIDEO_REQUEST:
      Intent intent = YouTubeIntents.createUploadIntent(this, returnedIntent.getData());
      startActivity(intent);
      break;
  }
}
super.onActivityResult(requestCode, resultCode, returnedIntent);
   }

  private enum IntentType {
PLAY_VIDEO,
OPEN_PLAYLIST,
OPEN_PLAYLIST2,
PLAY_PLAYLIST,
OPEN_USER,
OPEN_SEARCH,

  }

  private final class IntentItem implements DemoListViewItem {

   public final String title;
    public final IntentType type;

   public IntentItem(String title, IntentType type) {
     this.title = title;
     this.type = type;
    }

    public String getTitle() {
    return title;
    }

   public boolean isEnabled() {
     return isIntentTypeEnabled(type);
   }

    public String getDisabledText() {
     return getString(R.string.intent_disabled);
    }

    }

   }

DemoArrayAdapter.javaを変更する必要があります。しかし、どのように???? 私は初心者です

 package com.examples.youtubeapidemo.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
 import android.widget.TextView;

import com.examples.youtubeapidemo.R;

import java.util.List;

/**
* A convenience class to make ListViews easier to use in the demo activities.
*/
public final class DemoArrayAdapter extends ArrayAdapter<DemoListViewItem> {

  private final LayoutInflater inflater;

  public DemoArrayAdapter(Context context, int textViewResourceId, List<DemoListViewItem> objects) {
    super(context, textViewResourceId, objects);
   inflater = (LayoutInflater)    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }

  @Override
  public View getView(int position, View view, ViewGroup parent) {
   if (view == null) {
    view = inflater.inflate(R.layout.list_item, null);
    }

     TextView textView = (TextView) view.findViewById(R.id.list_item_text);
     textView.setText(getItem(position).getTitle());
     TextView disabledText = (TextView) view.findViewById(R.id.list_item_disabled_text);
    disabledText.setText(getItem(position).getDisabledText());

    if (isEnabled(position)) {
     disabledText.setVisibility(View.INVISIBLE);
     textView.setTextColor(Color.WHITE);
    } else {
     disabledText.setVisibility(View.VISIBLE);
     textView.setTextColor(Color.GRAY);
    }

    return view;
    }

   @Override
   public boolean areAllItemsEnabled() {
    // have to return true here otherwise disabled items won't show a divider in the list.
    return true;
   }

   @Override
  public boolean isEnabled(int position) {
   return getItem(position).isEnabled();
  }

   public boolean anyDisabled() {
    for (int i = 0; i < getCount(); i++) {
     if (!isEnabled(i)) {
       return true;
      }
   }
    return false;
  }

 }
4

2 に答える 2

0

1行のR.layout.list_itemレイアウトを変更し、必要な場所にimageviewを配置できます。必要な画像を連続して指定するには、カスタムDemoArrayAdapterのgetView()関数に画像を書き込む必要があります。

リストビューのカスタマイズの詳細については、次のチュートリアルを参照してください: http ://www.vogella.com/articles/AndroidListView/article.html

于 2013-03-02T20:00:38.570 に答える
0

DemoArrayAdapterのgetView()メソッドをオーバーライドする必要があります。

于 2013-03-02T20:01:28.697 に答える