小于或等于Android的条件

时间:2016-03-04 12:20:48

标签: java android listview conditional

我有一个引用应用程序,只有当引用列表大小为15时才会显示“加载更多”按钮。现在我想更改条件,这样只有当引用列表大小超过15时才必须显示该按钮。我目前的代码如下所示,我试图将其更改为:

if(c.getCount()<=15){
    // Not Showing Load More Button     
}

但它没有显示我的按钮。

该按钮的代码如下:

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 Quotes");


            if(c.getCount()<15){
                // Not Showing Load More Button

            }

            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();  }}
                });
        }
    }

2 个答案:

答案 0 :(得分:1)

我不确定Object&#34; c&#34;即将到来但我假设它列出了对象,而getCount则返回列表的当前计数。所以,如果你想展示&#34;显示更多&#34;当列表大小大于或等于15时按钮然后使用此条件。

if(c.getCount() >= 15){
    // Will show Load More Button     
}

答案 1 :(得分:0)

将Button放在布局XML文件中。膨胀它,然后:

if (c.getCount() > 15) {
    btnLoadMore.setVisibility(View.INVISIBLE);
}

如果您希望Button不可见。