我是Android开发的新手。我有一个arrayadapter,只需点击一下按钮即可更新。有4个数组列表:adModelsList,adModelsList_SJ,adModelsList_SD和adModelsList_MJ。如果数组列表包含7个以上的项目,则视图会更新。当列表少于7个项目时,它会崩溃。我做错了什么?
这是arrayadapter:
public class AdAdapter extends ArrayAdapter{
private List<AdModels> adModelsList;
private int resource;
private LayoutInflater inflater;
public AdAdapter(Context context, int resource, List<AdModels> objects) {
super(context, resource, objects);
this.resource = resource;
adModelsList = objects;
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
sjBtn = (Button) findViewById(R.id.sjBtn);
sdBtn = (Button) findViewById(R.id.sdBtn);
mjBtn = (Button) findViewById(R.id.mjBtn);
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(resource, null);
holder.overskrift = (TextView) convertView.findViewById(R.id.adoverskrift);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.overskrift.setText(adModelsList.get(position).getOverskrift());
sjBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adModelsList = adModelsList_SJ;
((AdAdapter) lvAds.getAdapter()).notifyDataSetChanged();
}
});
sdBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adModelsList = adModelsList_SD;
((AdAdapter) lvAds.getAdapter()).notifyDataSetChanged();
}
});
mjBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adModelsList = adModelsList_MJ;
((AdAdapter) lvAds.getAdapter()).notifyDataSetChanged();
}
});
lvAds.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AdModels admodel = adModelsList.get(position);
Intent intent = new Intent(MainActivity.this, MapsActivity.class);
intent.putExtra("adInfo", new Gson().toJson(admodel));
startActivity(intent);
}
});
return convertView;
}
class ViewHolder{
private TextView overskrift;
}
}
错误日志:
Process: dk.forsoegsperson.fp, PID: 12603
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 dk.forsoegsperson.fp.MainActivity$AdAdapter.getView(MainActivity.java:261)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.makeAndAddView(ListView.java:1875)
at android.widget.ListView.fillDown(ListView.java:702)
at android.widget.ListView.fillSpecific(ListView.java:1366)
at android.widget.ListView.layoutChildren(ListView.java:1662)
at android.widget.AbsListView.onLayout(AbsListView.java:2148)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
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 :(得分:0)
由于这个原因,你的行为有误:
if (cor_count > 0) {
convertView = null;
}
else {
cor_count = adModelsList.size();
}
更新适配器的adModelsList后,cor_count变量将无法正确更新为新大小
答案 1 :(得分:0)
所以,因为每次必须清除数组时我都会引入一个新数组,所以添加新列表,通知adapterarray并设置适配器。这就行了。
以下是代码:
xxBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adModelsList = adModelsList_SD;
((AdAdapter) lvAds.getAdapter()).clear();
((AdAdapter) lvAds.getAdapter()).addAll(adModelsList);
((AdAdapter) lvAds.getAdapter()).notifyDataSetChanged();
lvAds.setAdapter(AdAdapter.this);
}
});