我正在制作具有RecyclerView
的android应用程序,并且每个项目包含关闭按钮共享按钮和最大计数和计数按钮,当您单击计数按钮时间等于应删除该项目的最大次数时。
经过一段时间后它会崩溃
这是代码
package com.example.ali.azkarv10;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Vibrator;
import android.provider.ContactsContract;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Collections;
import java.util.List;
/**
* Created by Ali on 03/06/2016.
*/
public class Recycleradapter extends RecyclerView.Adapter<Recycleradapter.myViewHolder> {
Context context;
List<Zekr> list= Collections.emptyList();
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
public Recycleradapter(Context context,List<Zekr> list){
this.context=context;
this.list=list;
sharedPreferences=context.getSharedPreferences("data",Context.MODE_PRIVATE);
editor=sharedPreferences.edit();
}
@Override
public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater=LayoutInflater.from(context);
View view=inflater.inflate(R.layout.list_azkar_ayout,parent,false);
myViewHolder holder=new myViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final myViewHolder holder, final int position) {
Zekr current=list.get(position);
holder.zekr.setText(current.zekr);
holder.zekrcount.setText(Integer.toString(current.zekrcount));
holder.zekrmax.setText(Integer.toString(current.zekrmax));
holder.zekr.setTypeface(Typeface.createFromAsset(context.getAssets(),sharedPreferences.getString("fontType","fonts/blacksugar.ttf")));
holder.zekr.setTextSize(sharedPreferences.getInt("fontSize",10));
}
public void close(int position){
list.remove(position);
notifyItemRemoved(position);
if(list.isEmpty())
((Activity)context) .finish(); }
@Override
public int getItemCount() {
return list.size();
}
class myViewHolder extends RecyclerView.ViewHolder{
TextView zekr;
TextView zekrcount;
TextView zekrmax;
ImageButton close;
ImageButton share;
Button sab;
public myViewHolder(View itemView) {
super(itemView);
zekr=(TextView)itemView.findViewById(R.id.textzekr);
zekrcount=(TextView)itemView.findViewById(R.id.zekrcount);
zekrmax=(TextView)itemView.findViewById(R.id.zekrmax);
close=(ImageButton) itemView.findViewById(R.id.close);
share=(ImageButton)itemView.findViewById(R.id.share);
sab=(Button)itemView.findViewById(R.id.zekrButton);
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
close(getAdapterPosition());
}
});
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, zekr.getText().toString());
sendIntent.setType("text/plain");
context.startActivity(Intent.createChooser(sendIntent, context.getResources().getText(R.string.send_to)));
}
});
sab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.get(getAdapterPosition()).zekrcount+=1;
zekrcount.setText(Integer.toString( list.get(getAdapterPosition()).zekrcount));
if( list.get(getAdapterPosition()).zekrcount== list.get(getAdapterPosition()).zekrmax){
close(getAdapterPosition());
////buzzer code
if(sharedPreferences.getBoolean("buzzer",true)){
Vibrator vibrator=(Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);
}
}
}
});
}}
}
我的logcat
06-06 12:36:45.335 16747-16747/com.example.ali.azkarv10 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ali.azkarv10, PID: 16747
java.lang.ArrayIndexOutOfBoundsException: length=40; index=-1
at java.util.ArrayList.get(ArrayList.java:310)
at com.example.ali.azkarv10.Recycleradapter$myViewHolder$3.onClick(Recycleradapter.java:108)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:1)
你的getAdapterPosition()返回
`按照Android Doc
可能是因为
注意,如果你已经调用了notifyDataSetChanged(),那么直到下一个 布局传递,此方法的返回值为NO_POSITION。
<强>更新强>
而不是实现点击进入viewHolder,你应该在
中完成onBindViewHolder()
这样您就可以轻松获得位置,而无需调用getAdapterPosition()。