如何在Android中设置有限的适配器列表

时间:2018-01-02 12:09:22

标签: java android android-adapter

在我的应用程序中,我使用 ArrayAdapter ,我希望只显示 6项
我在adapter中写下代码:

@Override
public int getCount() {
    return 6;
}

但是当转到页面时,如果项目大小为0,则显示此错误:

FATAL EXCEPTION: main
                                                                   Process: com.app.test, PID: 7264
                                                                   java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
                                                                       at java.util.ArrayList.get(ArrayList.java:411)
                                                                       at com.app.test.Adapters.AuctionDetailBidListAdapter.getView(AuctionDetailBidListAdapter.java:46)
                                                                       at android.widget.AbsListView.obtainView(AbsListView.java:3170)
                                                                       at android.widget.ListView.makeAndAddView(ListView.java:2140)
                                                                       at android.widget.ListView.fillDown(ListView.java:762)
                                                                       at android.widget.ListView.fillFromTop(ListView.java:826)
                                                                       at android.widget.ListView.layoutChildren(ListView.java:1893)
                                                                       at android.widget.AbsListView.onLayout(AbsListView.java:2942)
                                                                       at android.view.View.layout(View.java:18878)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5953)
                                                                       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                       at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                       at android.view.View.layout(View.java:18878)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5953)
                                                                       at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1766)
                                                                       at android.view.View.layout(View.java:18878)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5953)
                                                                       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                       at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                       at android.view.View.layout(View.java:18878)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5953)
                                                                       at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1766)
                                                                       at android.view.View.layout(View.java:18878)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5953)
                                                                       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                       at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                       at android.view.View.layout(View.java:18878)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5953)
                                                                       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                       at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                       at android.view.View.layout(View.java:18878)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5953)
                                                                       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                       at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                       at android.view.View.layout(View.java:18878)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5953)
                                                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                       at android.view.View.layout(View.java:18878)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5953)
                                                                       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                       at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                       at android.view.View.layout(View.java:18878)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5953)
                                                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                       at android.view.View.layout(View.java:18878)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5953)
                                                                       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                       at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                       at android.view.View.layout(View.java:18878)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5953)
                                                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                       at com.android.internal.policy.DecorView.onLayout(DecorView.java:934)
                                                                       at android.view.View.layout(View.java:18878)
                                                                       at android.view.ViewGroup.layout(ViewGroup.java:5953)
01-02 15:37:05.687 7264-7264/com.app.test E/AndroidRuntime:     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2684)
                                                                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2400)
                                                                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1537)
                                                                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
                                                                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:959)
                                                                       at android.view.Choreographer.doCallbacks(Choreographer.java:734)
                                                                       at android.view.Choreographer.doFrame(Choreographer.java:670)
                                                                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:945)
                                                                       at android.os.Handler.handleCallback(Handler.java:751)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:154)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

我该如何解决?

适配器代码:

public class AuctionDetailBidListAdapter extends BaseAdapter {

    private TextView edtUserName, time, price;
    private ImageView User, line1;

    public AuctionDetailBidListAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull ArrayList<Bid> objects) {
        super(context, resource, objects);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View v = View.inflate(context, layout, null);
        Bid model = (Bid) datas.get(position);

        edtUserName = v.findViewById(R.id.edtUserName);
        line1 = v.findViewById(R.id.line1);
        time = v.findViewById(R.id.time);
        price = v.findViewById(R.id.price);
        edtUserName.setText(model.getName());
        time.setText(getDateCurrentTimeZone(model.getEnd()) + "");
        DecimalFormat df = new DecimalFormat(",###.##");
        price.setText(df.format(Integer.parseInt(model.getCurrentPrice())) + "تومان");
        User = v.findViewById(R.id.User);
        Glide.with(context)
                .load(Constants.SERVER + model.getAvatar())
                .placeholder(R.mipmap.ic_launcher)
                .signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
                .into(User);

        if (position == 0) {
            line1.setBackgroundColor(ContextCompat.getColor(context, R.color.winner_1));
        } else {
            line1.setBackgroundColor(ContextCompat.getColor(context, R.color.winner_2));
        }

        commonAction(v);
        return v;
    }

    @Override
    public int getCount() {
        return 6;
    }

3 个答案:

答案 0 :(得分:1)

更新:

首先,您应该将datas列表声明为成员变量:

private ArrayList<Bid> datas = null;

然后在构造函数中使用构造函数参数初始化datas数组,如下所示:

datas = objects;

然后在getCount方法中执行以下操作:

public int getCount() {
    return datas.size() <= 6 ? datas.size : 6;
}

ie:如果您的数组列表的大小小于或等于6,则返回大小,否则为6。试试这个

答案 1 :(得分:1)

当您的ArrayList中没有元素objects(或数据少于6个成员)或数据IndexOutOfBound异常将出现时

试一下

@Override
public int getCount() {
    if(objects.size() < 6){
        return objects.size();
    }
    else{
        return 6;
    }
}

更新

声明:

private ArrayList<Bid> objects = null;

在构造函数

this.objects = objects

答案 2 :(得分:0)

您的阵列java.lang.IndexOutOfBoundsException: Index: 0,Size: 0上的项目为零, 构建一个只有6个项目的数组。

如果您可以撰写有关代码的更多详细信息