当前位置: 动力学知识库 > 问答 > 编程问答 >

android - ArrayAdapter throws error on notifyDataSetChanged if the List<array> has less than 7 items

问题描述:

I'm new to android development. I have an arrayadapter that is updated on the click of a button. There are 4 array lists: adModelsList, adModelsList_SJ, adModelsList_SD and adModelsList_MJ. The view updates if the array list has more than 7 items. When the list has less than 7 items it crashes. What am I doing wrong??

Here is the 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;

}

}

Error log:

 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)

Edit Simplified the code

网友答案:

You have a wrong behaviour because of this:

 if (cor_count > 0) {
     convertView = null;
 }
 else {
    cor_count = adModelsList.size();
 }

after updating the adapter's adModelsList, the cor_count variable won't be updated correctly to the new size

网友答案:

So, since I was introducing a new array every time I had to clear the array, add the new list, notify the adapterarray and set the adapter. This did the trick.

Here is the code:

 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);
                }
            });
分享给朋友:
您可能感兴趣的文章:
随机阅读: