簡體   English   中英

android listview loadmore按鈕與xml解析

[英]android listview loadmore button with xml parsing

嗨我必須使用Android應用程序中的xml解析來加載更多按鈕來開發listview。

在這里,我遇到了一些問題。

我的xml提要為空意味着如何隱藏最后一頁上的加載更多按鈕。

我在這里使用了以下代碼。

public class CustomizedListView extends Activity {
// All static variables
private String URL = "http://dev.mmm.com/xctesting/xcart444pro/retrieve.php?page=1";
// XML node keys
static final String KEY_SONG = "Order"; 
    static final String KEY_TITLE = "orderid";
static final String KEY_DATE = "date";
static final String KEY_ARTIST = "payment_method";
    int current_page = 1;
ListView lv;
LazyAdapter adapter;
ProgressDialog pDialog; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    lv = (ListView) findViewById(R.id.list);

    ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML from URL
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_SONG);
    // looping through all song nodes <song>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
        map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
              songsList.add(map);
    }

    Button btnLoadMore = new Button(this);
    btnLoadMore.setText("Load More");

    btnLoadMore.setBackgroundResource(R.drawable.lgnbttn);
    // Adding Load More button to lisview at bottom
    lv.addFooterView(btnLoadMore);

    // Getting adapter
    adapter = new LazyAdapter(this, songsList);
    lv.setAdapter(adapter);
                btnLoadMore.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // Starting a new async task
            new loadMoreListView().execute();
        }
    });
}
    private class loadMoreListView extends AsyncTask<Void, Void, Void> {

        @Override 
        protected void onPreExecute() {
            // Showing progress dialog before sending http request
            pDialog = new ProgressDialog(
                    CustomizedListView.this);
            pDialog.setMessage("Please wait..");
            //pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.my_progress_indeterminate));
            pDialog.setIndeterminate(true);
            pDialog.setCancelable(false);
            pDialog.show(); 

            pDialog.setContentView(R.layout.custom_dialog);


        }
        protected Void doInBackground(Void... unused) {

                    current_page += 1;

                    // Next page request
                    URL = "http://dev.mmm.com/xctesting/xcart444pro/retrieve.php?page=" + current_page;

                    ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

                    XMLParser parser = new XMLParser();
                    String xml = parser.getXmlFromUrl(URL); // getting XML from URL
                    Document doc = parser.getDomElement(xml); // getting DOM element

                    NodeList nl = doc.getElementsByTagName(KEY_SONG);


                    NodeList nl = doc.getElementsByTagName(KEY_SONG);
                    if (nl.getLength() == 0)
                      {
                          btnLoadMore.setVisibility(View.GONE);
                          pDialog.dismiss();

                      }
                      else

                    // looping through all item nodes <item>
                    for (int i = 0; i < nl.getLength(); i++) {
                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();
                        Element e = (Element) nl.item(i);

                        // adding each child node to HashMap key => value
                        map.put(KEY_ID, parser.getValue(e, KEY_ID));
                        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
                        map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
                 songsList.add(map);
                    }

                    // get listview current position - used to maintain scroll position
                    int currentPosition = lv.getFirstVisiblePosition();

                    // Appending new data to menuItems ArrayList
                    adapter = new LazyAdapter(
                            CustomizedListView.this,
                            songsList);
                    lv.setAdapter(adapter);
             lv.setSelectionFromTop(currentPosition + 1, 0);


            }
            });

            return (null);
        }


        protected void onPostExecute(Void unused) {
            // closing progress dialog
            pDialog.dismiss();

        }
    }
}

編輯:

在這里我必須運行應用程序意味着列表視圖顯示在perpage 4 items.my最后一頁有1項。請參閱此截圖: lastpage在最后一頁我必須點擊加載更多按鈕意味着必須去下一個活動並成功隱藏空頁面上的按鈕 ..請參考此截圖: 空頁面 - 所以隱藏按鈕

我必須檢查空頁的條件:

    if (nl.getLength() == 0)
                    {
                        btnLoadMore.setVisibility(View.GONE);
                        pDialog.dismiss();

                    }

我怎么能寫最后一頁的條件?????請幫助我

在這里我希望需要o / p隱藏最后一頁上的按鈕

請幫助我。我可以檢查條件。以編程方式給我一些代碼。

我用這種方式實現了更多的負載我不使用頁腳視圖

public boolean mHasLoadMore = false;

    class MyAdapter extends ArrayAdapter implements OnItemClickListener{

        public MyAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
            // TODO Auto-generated constructor stub
        }

        public int getTotal(){
            return super.getCount();
        }
        @Override
        public int getCount() {

            if(super.getCount()>0)
                return super.getCount()+1;
            else
                return 0;

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if( getTotal() == (position+1) && mHasLoadMore ){

                TextView loadMoreView = new TextView(getContext());
                loadMoreView.setText("Load More");
                return loadMoreView;
            }              
            else {
                View theView = new View(getContext());
                // inflate & add what you need for item view here you can modify it to reuse convertView also
                return theView;
            }   

        }

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long id) {

            if( getTotal() == (position+1) && mHasLoadMore ){

                // load more button clicked do handling for load more launch async 
                //task to do load more after that if found 0 result change mHasLoadMore to false 
                //& call notifydatadetchanged               
            }
            else
            {
                // handle item click here  
            }

        }



    } 

在加載數據和調用notifydatasetchanged時首次使用此類適配器作為列表視圖並設置mHasLoadMore的值

首先,為什么在AsyncTaks中使用runOnUiThread? 這不應該是必要的。 必須在后台運行的代碼,在doInBackground方法中就足夠了。

此外,您可以使用AsyncTask的第三個參數類型將te doInBackground的結果返回到onPostExecute。 然后根據結果,您可以更改按鈕的可見性。

為此,對Button的引用(在您的情況下為btnLoadMore)應該是一個實例變量,因此應該在類中定義,而不是在onCreate方法中定義。 否則,無法從此方法外部訪問。

我認為當Feed為空時,nodelist將為空? 然后它看起來像這樣:

public class CustomizedListView extends Activity {

    private Button btnLoadMore;

    public void onCreate(){
        btnLoadMore = new Button(this);
        <do stuff>
        btnLoadMore.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View arg0) {
               // Starting a new async task
               new LoadMoreListView().execute();
           }
        });

        new LoadMoreListView().execute();// execute the AsyncTask once in the onCreate, so you don't have to duplicate the code here to load the listview.

    }

    private class LoadMoreListView extends AsyncTask<Void, Void, Boolean> {

        @Override 
        protected void onPreExecute() {
            < Show dialog>;
        }

        @Override
        protected Void doInBackground(Void... unused) {
           Boolean listIsEmpty;

           < Page request>;
           < Retrieve element list>;
           < If element list size == 0, listIsEmpty = true; return listIsEmpty>;

           < Process elements, fill list etc.>;
           < End of doInBackground, listIsEmpty = false; return listIsEmpty>;

        }

        @Override
        protected void onPostExecute(Boolean listIsEmpty) {
            < If listIsEmpty == true -> btnLoadMore.setVisibility(View.GONE);>
            < Close dialog>
        }
    }
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM