-1

大家好你们好。这是来自 youtube api 演示的代码。我只是想自定义列表视图,而不是显示要单击它的文本我想将其替换为列表视图中每个项目的特定图像(来自可绘制文件夹的图像)......为了澄清,出现“显示我的频道“......“我的播放列表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

您应该覆盖 DemoArrayAdapter 中的 getView() 方法。

于 2013-03-02T20:01:28.697 回答
0

您可以更改一行的 R.layout.list_item 布局,在您需要的地方放置一个 imageview。要连续指定您需要的图像,您必须将其写入自定义 DemoArrayAdapter 的 getView() 函数中。

有关自定义列表视图的更多信息,这里有一个很好的教程: http ://www.vogella.com/articles/AndroidListView/article.html

于 2013-03-02T20:00:38.570 回答