gpt4 book ai didi

java - 应用程序停止是因为按下了按钮?

转载 作者:行者123 更新时间:2023-11-29 23:23:09 27 4
gpt4 key购买 nike

我有一个小应用程序,有时会停止并显示错误,因为我已经转换了按钮 buttonViewOption。有时,当您按下按钮时,应用程序会停止,这是编写好代码以避免错误的最佳方式。

尝试帮我提出更好的建议或编写替换代码。

查看代码

package com.sab99r.recyclerview_loadmore;

import android.content.Context;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

/**
* Created by sab99r
*/
public class MoviesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

public final int TYPE_MOVIE = 0;
public final int TYPE_LOAD = 1;

static Context context;
List<MovieModel> movies;
OnLoadMoreListener loadMoreListener;
boolean isLoading = false, isMoreDataAvailable = true;

/*
* isLoading - to set the remote loading and complete status to fix back to back load more call
* isMoreDataAvailable - to set whether more data from server available or not.
* It will prevent useless load more request even after all the server data loaded
**/


public MoviesAdapter(Context context, List<MovieModel> movies) {
this.context = context;
this.movies = movies;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
if(viewType==TYPE_MOVIE){
return new MovieHolder(inflater.inflate(R.layout.row_movie,parent,false));
}else{
return new LoadHolder(inflater.inflate(R.layout.row_load,parent,false));
}
}


@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

if(position>=getItemCount()-1 && isMoreDataAvailable && !isLoading && loadMoreListener!=null){
isLoading = true;
loadMoreListener.onLoadMore();
}
if(getItemViewType(position)==TYPE_MOVIE){
((MovieHolder)holder).bindData(movies.get(position));
}

// tvRating

// See Here
((MovieHolder)holder).buttonViewOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Toast.makeText(context, ""+ position, Toast.LENGTH_LONG).show();
}
});
}


@Override
public int getItemViewType(int position) {
if(movies.get(position).type.equals("movie")){
return TYPE_MOVIE;
}else{
return TYPE_LOAD;
}

}




@Override
public int getItemCount(){
return movies.size();
}

/* VIEW HOLDERS */

static class MovieHolder extends RecyclerView.ViewHolder{
TextView tvTitle;
TextView tvRating;
Button buttonViewOption;
public MovieHolder(View itemView) {
super(itemView);
tvTitle=(TextView)itemView.findViewById(R.id.title);
tvRating=(TextView)itemView.findViewById(R.id.rating);
buttonViewOption = (Button) itemView.findViewById(R.id.textViewOptions);
}

void bindData(MovieModel movieModel){
tvTitle.setText(movieModel.post_writer);
tvRating.setText(movieModel.post_content);
}
}

static class LoadHolder extends RecyclerView.ViewHolder{
public LoadHolder(View itemView) {
super(itemView);
}
}

public void setMoreDataAvailable(boolean moreDataAvailable) {
isMoreDataAvailable = moreDataAvailable;
}

/* notifyDataSetChanged is final method so we can't override it
call adapter.notifyDataChanged(); after update the list
*/
public void notifyDataChanged(){
notifyDataSetChanged();
isLoading = false;
}


interface OnLoadMoreListener{
void onLoadMore();
}

public void setLoadMoreListener(OnLoadMoreListener loadMoreListener) {
this.loadMoreListener = loadMoreListener;
}
}

最佳答案

您的 MoviesAdapter 能够显示两种类型的 ViewHolder:MovieHolderLoadHolder。没有看到你的堆栈跟踪,我的猜测是 onBindViewHolder 正在处理一个 LoadHolder,但是你在转换到一个 MovieHolder 之前没有检查它.让我们看一下 onBindViewHolder():

    @Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

if(position>=getItemCount()-1 && isMoreDataAvailable && !isLoading && loadMoreListener!=null){
isLoading = true;
loadMoreListener.onLoadMore();
}
if(getItemViewType(position)==TYPE_MOVIE){
((MovieHolder)holder).bindData(movies.get(position));
}

到目前为止,您正在检查您的观看类型是否是电影。因此,如果这是真的,则可以保证 holder 属于 MovieHolder 类。你的错误在下一行:

        // See Here 
((MovieHolder)holder).buttonViewOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Toast.makeText(context, ""+ position, Toast.LENGTH_LONG).show();
}
});
}

您现在位于 if 语句之外,因此 holder 现在可以是 MovieHolderLoadHolder ,如果您尝试转换 LoadHolder,这将导致 ClassCastException。我建议像这样重写你的方法:

    @Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

if(position>=getItemCount()-1 && isMoreDataAvailable && !isLoading && loadMoreListener!=null){
isLoading = true;
loadMoreListener.onLoadMore();
}
if(getItemViewType(position)==TYPE_MOVIE){
MovieHolder movieHolder = (MovieHolder) holder;
movieHolder.bindData(movies.get(position));

// tvRating
movieHolder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, ""+ position, Toast.LENGTH_LONG)
.show();
}
});
}
}

关于java - 应用程序停止是因为按下了按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53904440/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com