与XML解析的Andr​​oid的ListView loadmore按钮 [英] android listview loadmore button with xml parsing

查看:360
本文介绍了与XML解析的Andr​​oid的ListView loadmore按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有发达的ListView负载更多的按钮,在Android应用程序使用的XML解析。

Hi i have to developed listview with load more button using xml parsing in android application.

在这里,我遇到了一些问题。

Here i have faced some problem.

我的XML数据源是空的手段如何隐藏最后一页上的负载更多的按钮。

我用下面code在这里。

i have used below code here.

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

        }
    }
}

编辑:

在这里,我需要运行的应用程序是指列表视图显示在有1 perpage 4 items.my最后一页item.please参考这个截图:的末页
在最后一页,我必须点击加载更多按钮意味着必须去下一个活动和成功隐藏空页面上的按钮 ..请参考这个截图:的空页面,以便隐藏按钮

Here i have to run the app means the listview is displayed on perpage 4 items.my last page having 1 item.please refer this screenshot:lastpage In last page i have to click the load more button means have to go next activity and successfully hide the button on empty page..please refer this screenshot:empty page-so hide the button

我要检查空页的条件:

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

                    }

我怎么能写conditon FOT最后一页?????认罪ehelp我

How can i write the conditon fot last page?????pleas ehelp me

下面的我想需要O / P是隐藏最后一页上的按钮

请帮忙me.how我可以检查condition.give我一些code编程。

Please help me.how can i check the condition.give me some code programmatically.

推荐答案

首先,你为什么用runOnUiThread一个AsyncTaks里面呢?这不应该是必要的。在code,它具有在后台运行,在doInBackground方法是不够的。

First off, why are you using runOnUiThread inside an AsyncTaks? This should not be necessary. The code that has to run in the background, in the doInBackground method is enough.

此外,您还可以使用的AsyncTask的第三个参数的类型从德doInBackground到onPostExecute返回结果。然后根据结果,您可以更改按钮出现的可见性。

Furthermore, you could use the third parameter type of the AsyncTask to return a result from te doInBackground to the onPostExecute. Then depending on the result, you can change the visibility of the button there.

要做到这一点,参照按钮(btnLoadMore在您的情况)应该是一个实例变量,所以应该在类中定义,而不是在onCreate方法。否则它不会从这个方法外访问的。

To do this, the reference to the Button (btnLoadMore in your case) should be a instance variable, so it should be defined in the class, not in the onCreate method. Otherwise it will not be accessible from outside this method.

我asume节点列表为空当原料是空的?然后,它会是这个样子:

I asume the nodelist will be empty when the feed is empty? Then it will look something like this:

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>
        }
    }
}
}

这篇关于与XML解析的Andr​​oid的ListView loadmore按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆