在Android中加载更多按钮问题

时间:2016-02-29 10:58:18

标签: java android listview android-studio android-database

我创建了一个带有从数据库填充的列表适配器的报价应用程序。它有一个加载更多按钮,15个阈值阈值。它没有显示报价大小是否低于15,但它显示列表是否为15,当我们按下加载更多按钮时它会崩溃。

final Button btnLoadMore=new Button(this);
            btnLoadMore.setBackgroundColor(Color.parseColor("#351802"));
            btnLoadMore.setTextColor(Color.parseColor("#e8d8a7"));
            btnLoadMore.setTypeface(btnLoadMore.getTypeface(), Typeface.BOLD);
            btnLoadMore.setText("Load More Status");


            if(c.getCount()<15){

            }

            else{
                 list.addFooterView(btnLoadMore);}
            list.setAdapter(adapter);
            anifadein=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slidedown);
            list.startAnimation(anifadein);

            // Click event for single list row
            list.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    map = quotesList.get(position);

                    Intent intent = new Intent(QuotesActivity.this,
                            QuoteDialogActivity.class);
                    int itemPosition     = position;
                    if(startingPoint>=30){


                        intent.putExtra("Pos", itemPosition+1);
                        intent.putExtra("LstCount", list.getCount()-1);
                    }else{
                        intent.putExtra("Pos", itemPosition+1);
                        intent.putExtra("LstCount", list.getCount());}
                    intent.putExtra("QuoteId", map.get(KEY_ID));
                    intent.putExtra("quotesType", quType);
                    intent.putExtra("startFrom", getIntent().getStringExtra("startFrom"));
                    intent.putExtra("Quotes", quotesList);
            //  Log.i("COUNT",""+(itemPosition+1)+"-"+list.getCount());
                    intent.putExtra("Fav", map.get(KEY_FAVORITE));
                    startActivity(intent);
                    if (mInterstitial.isLoaded()) {
                        mInterstitial.show();
                    }

                }
            });

             btnLoadMore.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        if(btnLoadMore.getVisibility()==View.VISIBLE){
                        Cursor newC = null;

                        if (quType != 0) {

                            switch (quType) {
                            case 1:


                                    newC = db.getQuotes(""+startingPoint);


                                break;
                            case 2:
                                newC = db.getFavoriteQuotes(""+startingPoint);
                                //page.setVisibility(View.GONE);
                                break;
                            case 3:
                                newC = db.getAuthorQuotes(getIntent().getStringExtra("AuthorId"),""+startingPoint);
                            //  page.setVisibility(View.GONE);
                                break;
                            }
                        }
                        // Starting a new async task
                        if(newC.getCount()<15){
                            btnLoadMore.setVisibility(View.INVISIBLE);

                        }
                        startingPoint+=15;  
                        do{
                            map = new HashMap<String, String>();

                            // adding each child node to HashMap key =&gt; value
                            map.put(KEY_ID, newC.getString(newC.getColumnIndex(KEY_ID)));
                            map.put(KEY_TEXT, newC.getString(newC.getColumnIndex(KEY_TEXT)));
                            map.put(KEY_AUTHOR, newC.getString(newC.getColumnIndex(KEY_AUTHOR)));
                            map.put(KEY_PICTURE, newC.getString(newC.getColumnIndex(KEY_PICTURE)));
                            map.put(KEY_PICTURE_SDCARD, String.valueOf(newC.getInt(newC
                                    .getColumnIndex(KEY_PICTURE_SDCARD))));
                            map.put(KEY_WEB_ID,
                                    String.valueOf(newC.getInt(newC.getColumnIndex(KEY_WEB_ID))));
                            //Log.i("web_id",String.valueOf(newC.getInt(newC.getColumnIndex(KEY_ID))));
                            map.put(KEY_FAVORITE, newC.getString(newC.getColumnIndex(KEY_FAVORITE)));

                            // adding HashList to ArrayList
                            quotesList.add(map);
                            if (mInterstitial.isLoaded()) {
                                mInterstitial.show();
                            }

                        } while (newC.moveToNext());
                        adapter.notifyDataSetChanged();  }}
                });
        }
    }

Log Cat在这里

&#13;
&#13;
02-29 16:39:50.553: E/AndroidRuntime(22325): FATAL EXCEPTION: main
02-29 16:39:50.553: E/AndroidRuntime(22325): Process: com.karopass.hindi_suvichar, PID: 22325
02-29 16:39:50.553: E/AndroidRuntime(22325): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
02-29 16:39:50.553: E/AndroidRuntime(22325): 	at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
02-29 16:39:50.553: E/AndroidRuntime(22325): 	at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
02-29 16:39:50.553: E/AndroidRuntime(22325): 	at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
02-29 16:39:50.553: E/AndroidRuntime(22325): 	at com.karopass.hindi_suvichar.QuotesActivity$2.onClick(QuotesActivity.java:242)
02-29 16:39:50.553: E/AndroidRuntime(22325): 	at android.view.View.performClick(View.java:5076)
02-29 16:39:50.553: E/AndroidRuntime(22325): 	at android.view.View$PerformClick.run(View.java:20279)
02-29 16:39:50.553: E/AndroidRuntime(22325): 	at android.os.Handler.handleCallback(Handler.java:739)
02-29 16:39:50.553: E/AndroidRuntime(22325): 	at android.os.Handler.dispatchMessage(Handler.java:95)
02-29 16:39:50.553: E/AndroidRuntime(22325): 	at android.os.Looper.loop(Looper.java:135)
02-29 16:39:50.553: E/AndroidRuntime(22325): 	at android.app.ActivityThread.main(ActivityThread.java:5930)
02-29 16:39:50.553: E/AndroidRuntime(22325): 	at java.lang.reflect.Method.invoke(Native Method)
02-29 16:39:50.553: E/AndroidRuntime(22325): 	at java.lang.reflect.Method.invoke(Method.java:372)
02-29 16:39:50.553: E/AndroidRuntime(22325): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
02-29 16:39:50.553: E/AndroidRuntime(22325): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
&#13;
&#13;
&#13;

由于

1 个答案:

答案 0 :(得分:0)

<=表示小于或等于if(c.getCount()<=15){

或者你可以做(​​没有else子句)。

if(c.getCount() > 15( { 
    btnLoadMore.setVisibility(View.VISIBLE);
    ........ your code that is currently in the else clause.. 
} else {
    btnLoadMore.setVisibility(View.INVISIBLE)
}

请注意! View.GONE是View.INVISIBLE的替代品。不同之处在于,INVISIBLE只能使视图(按钮)无法看到,而GONE在视图(按钮)不存在的情况下使用它。