ImageView,更改DemoArrayAdapter以显示图像

时间:2013-03-02 20:42:41

标签: android listview youtube android-arrayadapter

的所有人。这是来自youtube api demo的代码。我只想自定义列表视图,而不是只显示文本,我想在文本之前为listview中的每个项添加一个特定的图像。为了澄清,出现“显示我的频道”...“我的播放列表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;
  }

 }

1 个答案:

答案 0 :(得分: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;
}

希望这有帮助!