0

每个人。这是来自 youtube api 演示的代码。我只是想自定义列表视图,而不仅仅是显示文本,我想在文本之前为列表视图中的每个项目添加一个特定的图像。为了澄清,“显示我的频道”出现在哪里......“我的播放列表1”......它应该在这些文本之前出现一个图像以点击它。

我知道我应该修改 DemoArrayAdapter 上的 getview()。但是怎么做?我是新手。这就是为什么我将 DemoArrayAdapter.Java 的代码放在下面

IntentDemoActivity.Java

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

1 回答 1

1

在下面的 getView 方法中,适配器在更新 View 时引用了一个图像数组。

mContext 在 ArrayAdapter 构造函数中初始化

mThumbs 是一个 id 数组,引用您希望显示的图像

public Integer[] mThumbs{/*R.drawable.img1, R.drawable.img2,....*/};
private Context mContext;


@Override
public View getView(int position, View convertView, ViewGroup parent){

    if (view == null) {
        view = inflater.inflate(R.layout.list_item, null);
    }

    ImageView imageView;
    if(convertView == null)// Recycled View
    {
        imageView = new ImageView(mContext); 
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    }
    else // Re-use the view
    {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbs[position]);
    return imageView;
}

希望这可以帮助!

于 2013-03-02T21:25:36.683 回答