gpt4 book ai didi

android - 索引越界异常 : Inconsistency detected

转载 作者:行者123 更新时间:2023-11-30 00:35:39 39 4
gpt4 key购买 nike

我正在使用 recyclerView 并将数据加载为 arrayList。如果 arrayList 少于 7 个项目,则不会发生崩溃。

否则,我会遇到这个 fatal error :

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{3b1755be position=7 id=-1, oldPos=-1, pLpos:-1 no parent}

有人能帮忙吗?

诊断适配器.java

public class DiagnosticAdapter extends RecyclerView.Adapter<DiagnosticAdapter.ViewHolder> implements Filterable {
public static ArrayList<Diagnostic> diagnostics;
private ArrayList<Diagnostic> mFilteredDiagnosticArray;


public DiagnosticAdapter(ArrayList<Diagnostic> diagnostics) {
this.diagnostics = diagnostics;

}

@Override
public DiagnosticAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(DiagnosticAdapter.ViewHolder viewHolder, int i) {

viewHolder.parameter.setText(diagnostics.get(i).getParameter());
viewHolder.value.setText(diagnostics.get(i).getValue());
}

@Override
public Filter getFilter() {

return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {

String charString = charSequence.toString();

if (charString.isEmpty()) {

mFilteredDiagnosticArray = diagnostics;
} else {
ArrayList<Diagnostic> filteredList = new ArrayList<>();

for (Diagnostic diagnostic : diagnostics) {

if (diagnostic.getParameter().toLowerCase().contains(charString) || diagnostic.getValue().toLowerCase().contains(charString)) {

filteredList.add(diagnostic);
}
}

mFilteredDiagnosticArray = filteredList;

}

FilterResults filterResults = new FilterResults();
filterResults.values = mFilteredDiagnosticArray;
return filterResults;
}

@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
mFilteredDiagnosticArray = (ArrayList<Diagnostic>) filterResults.values;
notifyDataSetChanged();
}
};
}

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

}

ViewHolder.java

public class ViewHolder extends RecyclerView.ViewHolder{
private TextView parameter, value;
public ViewHolder(View view) {
super(view);
parameter = (TextView)view.findViewById(R.id.parameter);
value = (TextView)view.findViewById(R.id.value);
}
}

Fragment1.java

public class Fragment1 extends Fragment   {
private PtrClassicFrameLayout mPtrFrame;
private RecyclerView recyclerView;
private ArrayList<Diagnostic> data;
public static DiagnosticAdapter adapter;
private OkHttpClient client;
private Handler handler;
public static String constVar;
private String method;
public Fragment1() {

}
private Boolean mIsRefreshing = false;
private String ipAddress;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment_1, container, false);
}

@Override
public void onViewCreated(final View view, @Nullable final Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_viewDiag);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
Log.i("mIsRefreshing",mIsRefreshing.toString());
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
method = getArguments().getString("method");
Log.i("METHOD",method);

loadJSON();

}else{
}
}

private void loadJSON(){
// Log.i("Fragment1",ipAddress);
Retrofit retrofit = new Retrofit.Builder()
//.baseUrl("http://"+ipAddress+":8000")
.baseUrl("http://10.206.208.92"+":8000")
.addConverterFactory(GsonConverterFactory.create())
.build();


RequestInterface request = retrofit.create(RequestInterface.class);
Call<JSONResponse> call ;
switch (method){
case "identification":
call = request.getIdentification();
break;
case "memory":
call = request.getMemory();
break;
case "sysInfo":
call = request.getSysInfo();
break;
case "conditionalAccess":
call = request.getConditionalAccess();
break;
case "network":
call = request.getNetwork();
break;
case "software":
call = request.getSoftware();
break;
case "loader":
call = request.getLoader();
break;
default:
call = request.getJSON();
break;
}

call.enqueue(new Callback<JSONResponse>() {
@Override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
JSONResponse jsonResponse = response.body();
switch (method){
case "identification":
data = new ArrayList<>(Arrays.asList(jsonResponse.getIdentification()));
break;
case "memory":
data = new ArrayList<>(Arrays.asList(jsonResponse.getMemory()));
break;
case "sysInfo":
data = new ArrayList<>(Arrays.asList(jsonResponse.getSysInfo()));
break;
case "conditionalAccess":
data = new ArrayList<>(Arrays.asList(jsonResponse.getConditionalAccess()));
break;
case "network":
data = new ArrayList<>(Arrays.asList(jsonResponse.getNetwork()));
break;
case "software":
data = new ArrayList<>(Arrays.asList(jsonResponse.getSoftware()));
break;
case "loader":
data = new ArrayList<>(Arrays.asList(jsonResponse.getLoader()));
break;
default:
data = new ArrayList<>(Arrays.asList(jsonResponse.getDiagnostics()));
break;

}
//data = new ArrayList<>(Arrays.asList(jsonResponse.getDiagnostics()));
//Toast.makeText(getContext(), data.size(), Toast.LENGTH_LONG).show();
adapter = new DiagnosticAdapter(data);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
mIsRefreshing = true;
Log.i("mIsRefreshing",mIsRefreshing.toString());


// mPtrFrame.refreshComplete();

}
@Override
public void onFailure(Call<JSONResponse> call, Throwable t) {
// mPtrFrame.refreshComplete();
try {
adapter.diagnostics.clear();
adapter.notifyDataSetChanged();
} catch (Exception e) {
Log.e("ERROR", "showProgressDialog", e);
}
Snackbar.make(getView(), "Unable to fetch json: " + t.getMessage(), Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}

DemoActivity.java

public class DemoActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpagerDemo);
// final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
SmartTabLayout viewPagerTab = (SmartTabLayout)
findViewById(R.id.viewpagertab);


TabsAdapter adapter = new TabsAdapter(getSupportFragmentManager());
adapter.addFrag(new Fragment1(), "memory");
adapter.addFrag(new Fragment1(), "identification");
adapter.addFrag(new Fragment1(), "loader");
viewPager.setAdapter(adapter);
viewPagerTab.setViewPager(viewPager);
}
}

TabsAdapter.java

public class TabsAdapter extends FragmentPagerAdapter {
private final List<android.support.v4.app.Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public TabsAdapter(FragmentManager fm) {
super(fm);
}
public void addFrag(android.support.v4.app.Fragment fragment, String title){
Bundle bundle = new Bundle();
bundle.putString("method", title);
fragment.setArguments(bundle);

mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}

最佳答案

您可以尝试在您的适配器中使用数据的副本吗?

private ArrayList<Diagnostic> diagnostics = new ArrayList<>();
public DiagnosticAdapter(ArrayList<Diagnostic> diagnostics) {
this.diagnostics.addAll(diagnostics);
}

像这样清除数据:

public void clearData() {
this.diagnostics.clear();
notifyDataSetChanged();
}

像这样设置新数据:

public void setData(List<Diagnostics> data) {
clearData();
this.diagnostics.addAll(data);
notifyDataSetChanged();
}

除了使用 notifyDataSetChanged(),如果您想使用动画或获得更好的性能,您可以使用其他通知...函数。

希望这能奏效...

关于android - 索引越界异常 : Inconsistency detected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43461431/

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