在listview中显示图像而不是文本

时间:2013-03-02 19:54:39

标签: android listview youtube

嗨,大家好。这是来自youtube api demo的代码。我只是想自定义列表视图,而不是显示一个文本来点击它我想将它替换为listview中每个项目的特定图像(来自drawable文件夹的图像)....澄清一下,出现在哪里“显示我的频道“...”我的播放列表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;
  }

 }

2 个答案:

答案 0 :(得分:0)

您可以更改一行的R.layout.list_item布局,并在您需要的位置放置一个imageview。 要在一行中指定所需的图像,必须在自定义DemoArrayAdapter的getView()函数中编写它。

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

答案 1 :(得分:0)

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