在我的应用程序中我有3个单选按钮,如果我点击“直接拨号”选项,则相应于卡片视图中填充的数据,以及其他选项的相同数据。但问题是,当我点击另一个选项时,它会填充我想要的数据,但应用程序会因IndexOutOfBound异常而崩溃。 我的代码是:
DI.java(我正在填充回收者视图)
gdone.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
mDatasetTypes.clear();
source.clear();
mDatasetTypes.clear();
example.clear();
RadioButton radioButton = (RadioButton)radioGroup.findViewById(i);
if(radioButton.getText().toString().equals("Budget\nDialing")) {
/* Intent intent = getActivity().getIntent();
getActivity().finish();
startActivity(intent);*/
cquery = sql.rawQuery("select BD_Mobile,BD_landline,BD_Mobile_Example,BD_landline_Example from DialingInstruction where SimId='" + substr + "'", null);
for (cquery.moveToFirst(); !cquery.isAfterLast(); cquery.moveToNext()) {
mDataset.add(cquery.getString(0).toString());
mDataset.add(cquery.getString(1).toString());
source.add("Mobile");
source.add("Landline");
mDatasetTypes.add(INSTRUCTION);
mDatasetTypes.add(INSTRUCTION);
example.add(cquery.getString(2).toString());
example.add(cquery.getString(3).toString());
}
// mAdapter = new InstructionAdapter(source,mDataset,example, mDatasetTypes);
try{
mAdapter.notifyDataSetChanged();
}
catch(Exception e){ Log.e("tagag","caught you");}
}
else if(radioButton.getText().toString().equals("Prefix\nDialing")) {
cquery = sql.rawQuery("select PD_Mobile_International,PD_landline_International,PD_Mobile_International_Example,PD_landline_International_Example from DialingInstruction where SimId='" + substr + "'", null);
for (cquery.moveToFirst(); !cquery.isAfterLast(); cquery.moveToNext()) {
mDataset.add(cquery.getString(0).toString());
mDataset.add(cquery.getString(1).toString());
source.add("Mobile");
source.add("Landline");
mDatasetTypes.add(INSTRUCTION);
mDatasetTypes.add(INSTRUCTION);
example.add(cquery.getString(2).toString());
example.add(cquery.getString(3).toString());
}
}
else if(radioButton.getText().toString().equals("Direct\nDialing"))
{
cquery = sql.rawQuery("select DD_Mobile_Local,DD_landline_Local,DD_Mobile_International,DD_landline_International,DD_Mobile_Local_Example,DD_landline_Local_Example,DD_Mobile_International_Example,DD_landline_International_Example from DialingInstruction where SimId='"+substr +"'", null);
for(cquery.moveToFirst();!cquery.isAfterLast();cquery.moveToNext())
{
mDataset.add(cquery.getString(0).toString());
mDataset.add(cquery.getString(1).toString());
mDataset.add(cquery.getString(2).toString());
mDataset.add(cquery.getString(3).toString());
source.add("Mobile(Local)");
source.add("Landline(Local)");
source.add("Mobile(International)");
source.add("Landline(International)");
mDatasetTypes.add(INSTRUCTION);
mDatasetTypes.add(INSTRUCTION);
mDatasetTypes.add(INSTRUCTION);
mDatasetTypes.add(INSTRUCTION);
example.add(cquery.getString(4).toString());
example.add(cquery.getString(5).toString());
example.add(cquery.getString(6).toString());
example.add(cquery.getString(7).toString());
}
}
}
});
Log.e("TAGkhj", "onCreateView: "+mDataset.size() );
mAdapter = new InstructionAdapter(source,mDataset,example, mDatasetTypes);
mRecyclerView.setAdapter(mAdapter );
return view;
}
适配器类:
public class InstructionAdapter extends RecyclerView.Adapter<InstructionAdapter.ViewHolder> {
private List<String> mSource;
private List<String> mExample;
private List<String> mDataSet;
private List<Integer> mDataSetTypes;
public static final int INSTRUCTION = 0;
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View v) {
super(v);
}
}
public class InstructionViewHolder extends ViewHolder{
TextView Ins, Detail;
RadioButton Instruction, Exm;
RadioGroup GroupI;
public InstructionViewHolder(View v) {
super(v);
this.Ins = (TextView) v.findViewById(R.id.ins);
this.Detail=(TextView)v.findViewById(R.id.detail);
this.Instruction=(RadioButton)v.findViewById(R.id.instruction);
this.Exm=(RadioButton)v.findViewById(R.id.exm);
this.GroupI=(RadioGroup)v.findViewById(R.id.groupI);
}
}
public InstructionAdapter(List<String> source, List<String> dataSet, List<String> example, List<Integer> dataSetTypes) {
mSource=null;
mExample=null;
mDataSet=null;
mDataSetTypes=null;
mDataSet = dataSet;
mDataSetTypes = dataSetTypes;
mSource=source;
mExample=example;
try {
notifyDataSetChanged();
}
catch (Exception e)
{
Log.e("lfkjdlg", "InstructionAdapter: ");
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
if (viewType == INSTRUCTION) {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.instructionxml, parent, false);
return new InstructionViewHolder(v);
}
else
return null;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
if (holder.getItemViewType() == INSTRUCTION) {
final InstructionViewHolder instructionViewHolder = (InstructionViewHolder) holder;
instructionViewHolder.Ins.setText(mSource.get(position));
int SelId=instructionViewHolder.GroupI.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) instructionViewHolder.GroupI.findViewById(SelId);
if(radioButton.getText().toString().equals("Instruction"))
{
instructionViewHolder.Detail.setText(mDataSet.get(position));
}
instructionViewHolder.GroupI.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton radioButton = (RadioButton) radioGroup.findViewById(i);
if(radioButton.getText().toString().equals("Example"))
{
instructionViewHolder.Detail.setText(mExample.get(position));
}
else {
instructionViewHolder.Detail.setText(mDataSet.get(position));
}
}
});
// holder.temp.setText(mDataSet[position]);
}
/* mDataSet.clear();
mDataSetTypes.clear();
mExample.clear();
mSource.clear();*/
}
@Override
public int getItemCount() {
return mDataSet.size();
}
@Override
public int getItemViewType(int position) {
return mDataSetTypes.get(position);
}
}
我的Logcat:
Process: com.roam1.roam1_selfcare, PID: 27679
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.roam1.roam1_selfcare.InstructionAdapter.getItemViewType(InstructionAdapter.java:117)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4960)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4913)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1193)
at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1043)
at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1552)
at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:2649)
at android.view.View.dispatchTouchEvent(View.java:9297)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2549)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2240)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
at android.app.Activity.dispatchTouchEvent(Activity.java:2769)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63)
at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
at android.view.View.dispatchPointerEvent(View.java:9517)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4242)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4108)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3654)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3707)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3673)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3799)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3681)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3856)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3654)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3707)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3673)
at android