gpt4 book ai didi

java - android listview loadmore按钮与xml解析

转载 作者:行者123 更新时间:2023-12-02 07:29:19 27 4
gpt4 key购买 nike

嗨,我必须在 Android 应用程序中使用 xml 解析来开发带有加载更多按钮的 ListView 。

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

我的 xml feed 为空意味着如何隐藏最后一页上的加载更多按钮。

我在这里使用了下面的代码。

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

}
}
}

编辑:

这里我必须运行应用程序意味着 ListView 显示在每页 4 个项目上。我的最后一页有 1 个项目。请引用此屏幕截图:lastpage在最后一页中,我必须单击“加载更多”按钮意味着必须进行下一个 Activity 并成功隐藏空白页面上的按钮..请引用此屏幕截图:empty page-so hide the button

我必须检查空页的情况:

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

}

我怎样才能写最后一页的条件??????请帮助我

这里我希望需要o/p隐藏最后一页上的按钮

请帮助我。我如何检查条件。以编程方式给我一些代码。

最佳答案

首先,为什么要在 AsyncTaks 中使用 runOnUiThread?这应该是没有必要的。必须在后台运行的代码,在 doInBackground 方法中就足够了。

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

为此,对按钮(在您的例子中为 btnLoadMore)的引用应该是一个实例变量,因此它应该在类中定义,而不是在 onCreate 方法中。否则将无法从此方法外部访问它。

我认为当提要为空时节点列表将为空?然后它看起来像这样:

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

关于java - android listview loadmore按钮与xml解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13133538/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com