在我的 recyclerview
我有两种视图类型 native ad view type
和菜单项,在我下面的适配器我得到以下错误。
java.lang.classcastexception:无法将java.util.arraylist转换为adapter.mainlistadapter.onbindviewholder(mainlistadapter)处的model.videolistmodel。java:95)
那条线呢 95
是 final VideoListModel folderVideoModel = (VideoListModel) arrayList.get(position);
这是我的recyclerview适配器
public class MainListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int MENU_ITEM_VIEW_TYPE = 0;
private static final int UNIFIED_NATIVE_AD_VIEW_TYPE = 1;
private final Context mContext;
private final ArrayList<Object> arrayList;
public MainListAdapter(Context context, ArrayList<Object> arrayList) {
this.mContext = context;
this.arrayList = arrayList;
}
public static class MainAdapterViewHolder extends RecyclerView.ViewHolder {
TextView title;
TextView duration;
Chip size;
RelativeLayout path;
ImageView thumbnail;
public MainAdapterViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
duration = itemView.findViewById(R.id.duration);
size = itemView.findViewById(R.id.file_size);
path = itemView.findViewById(R.id.item_path);
thumbnail = itemView.findViewById(R.id.thumbnail);
}
}
@Override
public int getItemCount() {
return arrayList.size();
}
@Override
public int getItemViewType(int position) {
Object recyclerViewItem = arrayList.get(position);
if (recyclerViewItem instanceof UnifiedNativeAd) {
return UNIFIED_NATIVE_AD_VIEW_TYPE;
}
return MENU_ITEM_VIEW_TYPE;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
switch (viewType) {
case UNIFIED_NATIVE_AD_VIEW_TYPE:
View unifiedNativeLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.ad_unified, viewGroup, false);
return new UnifiedNativeAdViewHolder(unifiedNativeLayoutView);
case MENU_ITEM_VIEW_TYPE:
default:
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_video_list, viewGroup, false);
return new MainAdapterViewHolder(view);
}
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
int viewType = getItemViewType(position);
switch (viewType) {
case UNIFIED_NATIVE_AD_VIEW_TYPE:
UnifiedNativeAd nativeAd = (UnifiedNativeAd) arrayList.get(position);
populateNativeAdView(nativeAd, ((UnifiedNativeAdViewHolder) holder).getAdView());
break;
case MENU_ITEM_VIEW_TYPE:
default:
final MainAdapterViewHolder mainAdapterViewHolder = (MainAdapterViewHolder) holder;
final VideoListModel folderVideoModel = (VideoListModel) arrayList.get(position);
mainAdapterViewHolder.title.setText("" + folderVideoModel.getPath());
mainAdapterViewHolder.thumbnail.setImageResource(R.drawable.ic_folder);
break;
}
}
主要活动
public class MainActivity extends AppCompatActivity {
private final ArrayList<NormalFile> list = new ArrayList<>();
private final List<VideoListModel> models = new ArrayList<>();
private final Map<String, VideoListModel> files = new HashMap<>();
private DbHelper db;
MainListAdapter adapter;
private final FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
List<Object> arrayListVideos = new ArrayList<>();
List<UnifiedNativeAd> nativeAds = new ArrayList<>();
public static final int NUMBER_OF_ADS = 1;
AdLoader adLoader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = Constants.getDbHelper(this);
loadVideos();
RecyclerView recyclerView = findViewById(R.id.main_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new MainListAdapter(this, arrayListVideos);
recyclerView.setAdapter(adapter);
}
private void loadVideos() {
FileFilter.getFiles(this, new FilterResultCallback<NormalFile>() {
@Override
public void onResult(List<Directory<NormalFile>> directories) {
for (Directory<NormalFile> directory : directories) {
list.addAll(directory.getFiles());
for (int i = 0; i < directory.getFiles().size(); i++) {
File f = new File(directory.getFiles().get(i).getPath());
String parent = Objects.requireNonNull(f.getParent()).substring(f.getParent().lastIndexOf("/") + 1);
if (parent.equals("0")) {
VideoListModel vlm = new VideoListModel();
vlm.setFolder(false);
vlm.setFile(f);
vlm.setPath(f.getPath());
vlm.setDuration("");
models.add(vlm);
} else {
if (files.containsKey(parent)) {
VideoListModel vlm = files.get(parent);
assert vlm != null;
vlm.getFiles().add(f);
vlm.setPath(f.getParent());
} else {
VideoListModel vlm = new VideoListModel();
vlm.setFolder(true);
vlm.setFolderName(parent);
vlm.setPath(f.getParent());
List<File> fileList = new ArrayList<>();
fileList.add(f);
vlm.setFiles(fileList);
files.put(parent, vlm);
}
}
}
}
models.addAll(files.values());
arrayListVideos.add(models);
for (int i = 0; i < models.size(); i++) {
Log.d("TAG", "onResult: "+ models.get(i).getPath() + " 2 " + models.get(i).isFolder());
}
adapter.notifyDataSetChanged();
//sortByName();
new ThumbnailLoader().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new TimeAndSizeLoader().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
getSupportLoaderManager().destroyLoader(3);
}
}, new String[]{"mp4", "mov", "wmv", "avi", "avchd", "webm", "m4v", "mkv", "3gp", "3g2", "flv", "m3u8"});
}
@SuppressLint("StaticFieldLeak")
private class TimeAndSizeLoader extends AsyncTask<Void, Integer, Void> {
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
adapter.notifyItemChanged(values[0]);
}
@Override
protected Void doInBackground(Void... voids) {
for (int i = 0; i < models.size(); i++) {
if(models.get(i).getFile() != null){
FileStore fs = db.getFileByName(models.get(i).getFile().getName());
VideoListModel vlm = models.get(i);
if (fs != null) {
if(fs.getDuration() != null){ // for file
vlm.setDuration(fs.getDuration());
}else {
fs.setDuration(Constants.getDuration(vlm.getFile().getPath()));
vlm.setDuration(fs.getDuration());
db.insertOrUpdateFile(fs);
}
}else {
vlm.setDuration(Constants.getDuration(vlm.getFile().getPath()));
FileStore fss = new FileStore();
fss.setName(vlm.getFile().getName());
fss.setParent(vlm.getFile().getParent());
fss.setUpdatedAt(new Date());
db.insertOrUpdateFile(fss);
models.set(i, vlm);
}
publishProgress(i);
}
}
Log.e("size", db.getFiles().size() + "");
return null;
}
}
@SuppressLint("StaticFieldLeak")
private class ThumbnailLoader extends AsyncTask<Void, Integer, Void> {
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
adapter.notifyDataSetChanged();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
adapter.notifyDataSetChanged();
}
@Override
protected Void doInBackground(Void... voids) {
for (int i = 0; i < models.size(); i++) {
if(models.get(i).getFile() != null){
FileStore fs = db.getFileByName(models.get(i).getFile().getName());
VideoListModel vlm = models.get(i);
if (fs != null) {
if(fs.getBitmap() != null){
vlm.setBitmap(BitmapFactory.decodeByteArray(fs.getBitmap(), 0, fs.getBitmap().length));
vlm.setWidth(fs.getWidth());
vlm.setHeight(fs.getHeight());
}else {
mmr.setDataSource(models.get(i).getFile().getPath());
Bitmap bmp = mmr.getFrameAtTime((Long.parseLong(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION)) / 2) * 1000, OPTION_CLOSEST_SYNC);
bmp = Constants.resize(bmp, 512, 512);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
vlm.setBitmap(bmp);
fs.setBitmap(byteArray);
fs.setWidth(Integer.parseInt(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)));
fs.setHeight(Integer.parseInt(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)));
vlm.setWidth(fs.getWidth());
vlm.setHeight(fs.getHeight());
db.insertOrUpdateFile(fs);
}
}else {
try {
mmr.setDataSource(models.get(i).getFile().getPath());
Log.e("time", mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION));
//vlm.setDuration(Constants.getDurationByLong(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION)));
//models.set(i, vlm);
//publishProgress(i);
Bitmap bmp = mmr.getFrameAtTime((Long.parseLong(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION)) / 2) * 1000, OPTION_CLOSEST_SYNC);
bmp = Constants.resize(bmp, 512, 512);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
vlm.setBitmap(bmp);
FileStore fss = new FileStore();
fss.setName(vlm.getFile().getName());
fss.setParent(vlm.getFile().getParent());
fss.setBitmap(byteArray);
fss.setWidth(Integer.parseInt(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)));
fss.setHeight(Integer.parseInt(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)));
fss.setUpdatedAt(new Date());
vlm.setWidth(fss.getWidth());
vlm.setHeight(fss.getHeight());
db.insertOrUpdateFile(fss);
models.set(i, vlm);
}catch (Exception ignored){ }
}
publishProgress(i);
}
}
Log.e("db", db.getFiles().size() + "");
return null;
}
}
private void loadNativeAds() {
AdLoader.Builder builder = new AdLoader.Builder(this,getResources().getString(R.string.native_ad_id));
adLoader = builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
@Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
nativeAds.add(unifiedNativeAd);
if (!adLoader.isLoading()) {
insertAdsInMenuItem();
}
}
}).withAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
if (!adLoader.isLoading()) {
insertAdsInMenuItem();
}
}
}).build();
adLoader.loadAds(new AdRequest.Builder().build(),NUMBER_OF_ADS);
}
private void insertAdsInMenuItem() {
if (nativeAds.size() < 0 ){
return;
}
int offset = (models.size() / nativeAds.size() + 1 );
int index = 0;
for (UnifiedNativeAd ad:nativeAds){
arrayListVideos.add(index, ad);
index = index + offset;
}
}
}
}
视频列表模型
公共类videolistmodel{私有布尔文件夹;私有int文件计数;私有int宽度;私人内部高度;私有字符串foldername;私有字符串持续时间;私有字符串路径;私有文件;私有位图;私有列表文件;
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
public boolean isFolder() {
return folder;
}
public void setFolder(boolean folder) {
this.folder = folder;
}
public int getFileCount() {
return fileCount;
}
public void setFileCount(int fileCount) {
this.fileCount = fileCount;
}
public String getFolderName() {
return folderName;
}
public void setFolderName(String folderName) {
this.folderName = folderName;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public List<File> getFiles() {
return files;
}
public void setFiles(List<File> files) {
this.files = files;
}
}
我补充说 Model
但我还是犯了个错误。我犯了什么错?
1条答案
按热度按时间ecfsfe2w1#
创建类MyUnifiedNativeRead:
从mainlistsuperclass扩展videolistmodel:
适配器代码:
活动代码: