public class AndroidXMLParsingActivity extends Activity {
public int currentPage = 1;
public ListView lisView1;
static final String KEY_ITEM = "docdetails";
static final String KEY_NAME = "heading";
public Button btnNext;
public Button btnPre;
public static String url = "http://dev.taxmann.com/TaxmannService/TaxmannService.asmx/GetNotificationList";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// listView1
lisView1 = (ListView) findViewById(R.id.listView1);
// Next
btnNext = (Button) findViewById(R.id.btnNext);
// Perform action on click
btnNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
currentPage = currentPage + 1;
ShowData();
}
});
// Previous
btnPre = (Button) findViewById(R.id.btnPre);
// Perform action on click
btnPre.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
currentPage = currentPage - 1;
ShowData();
}
});
ShowData();
}
public void ShowData() {
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(url); // getting XML
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
int displayPerPage = 5; // Per Page
int TotalRows = nl.getLength();
int indexRowStart = ((displayPerPage * currentPage) - displayPerPage);
int TotalPage = 0;
if (TotalRows <= displayPerPage) {
TotalPage = 1;
} else if ((TotalRows % displayPerPage) == 0) {
TotalPage = (TotalRows / displayPerPage);
} else {
TotalPage = (TotalRows / displayPerPage) + 1; // 7
TotalPage = (int) TotalPage; // 7
}
int indexRowEnd = displayPerPage * currentPage; // 5
if (indexRowEnd > TotalRows) {
indexRowEnd = TotalRows;
}
// Disabled Button Next
if (currentPage >= TotalPage) {
btnNext.setEnabled(false);
} else {
btnNext.setEnabled(true);
}
// Disabled Button Previos
if (currentPage <= 1) {
btnPre.setEnabled(false);
} else {
btnPre.setEnabled(true);
}
// Load Data from Index
int RowID = 1;
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
// RowID
if (currentPage > 1) {
RowID = (displayPerPage * (currentPage - 1)) + 1;
}
for (int i = indexRowStart; i < indexRowEnd; i++) {
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map = new HashMap<String, String>();
map.put("RowID", String.valueOf(RowID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
// adding HashList to ArrayList
menuItems.add(map);
RowID = RowID + 1;
}
SimpleAdapter sAdap;
sAdap = new SimpleAdapter(AndroidXMLParsingActivity.this, menuItems,
R.layout.list_item, new String[] { "RowID", KEY_NAME },
new int[] { R.id.ColRowID, R.id.ColName });
lisView1.setAdapter(sAdap);
}
}
这是我的源代码,我想设置ProgressBar这个活动是从另一个活动调用按钮点击我想要设置ProgressBar当数据加载ShowData()函数时我们用于解析打印,并且分页我试过很多但是我我无法使用ProgressBar加载数据请告诉我如何实现它。
答案 0 :(得分:0)
答案 1 :(得分:0)
试试这段代码,它会起作用..
public class AndroidXMLParsingActivity extends Activity {
public int currentPage = 1;
public ListView lisView1;
static final String KEY_ITEM = "docdetails";
static final String KEY_NAME = "heading";
public Button btnNext;
public Button btnPre;
public static String url = "http://dev.taxmann.com/TaxmannService/TaxmannService.asmx/GetNotificationList";
SimpleAdapter sAdap;
ProgressDialog progressDialog;
Activity mActivity;
ArrayList<HashMap<String, String>> menuItems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mActivity = this;
// listView1
lisView1 = (ListView) findViewById(R.id.listView1);
// Next
btnNext = (Button) findViewById(R.id.btnNext);
// Perform action on click
btnNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
currentPage = currentPage + 1;
new backProcess().execute();
}
});
// Previous
btnPre = (Button) findViewById(R.id.btnPre);
// Perform action on click
btnPre.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
currentPage = currentPage - 1;
new backProcess().execute();
}
});
new backProcess().execute();
}
public void ShowData() {
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(url); // getting XML
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
int displayPerPage = 5; // Per Page
int TotalRows = nl.getLength();
int indexRowStart = ((displayPerPage * currentPage) - displayPerPage);
int TotalPage = 0;
if (TotalRows <= displayPerPage) {
TotalPage = 1;
} else if ((TotalRows % displayPerPage) == 0) {
TotalPage = (TotalRows / displayPerPage);
} else {
TotalPage = (TotalRows / displayPerPage) + 1; // 7
TotalPage = (int) TotalPage; // 7
}
int indexRowEnd = displayPerPage * currentPage; // 5
if (indexRowEnd > TotalRows) {
indexRowEnd = TotalRows;
}
// Disabled Button Next
if (currentPage >= TotalPage) {
btnNext.setEnabled(false);
} else {
btnNext.setEnabled(true);
}
// Disabled Button Previos
if (currentPage <= 1) {
btnPre.setEnabled(false);
} else {
btnPre.setEnabled(true);
}
// Load Data from Index
int RowID = 1;
menuItems = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
// RowID
if (currentPage > 1) {
RowID = (displayPerPage * (currentPage - 1)) + 1;
}
for (int i = indexRowStart; i < indexRowEnd; i++) {
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map = new HashMap<String, String>();
map.put("RowID", String.valueOf(RowID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
// adding HashList to ArrayList
menuItems.add(map);
RowID = RowID + 1;
}
}
public class backProcess extends AsyncTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void result) {
if (progressDialog != null)
progressDialog.dismiss();
sAdap = new SimpleAdapter(AndroidXMLParsingActivity.this,
menuItems, R.layout.list_item, new String[] { "RowID",
KEY_NAME },
new int[] { R.id.ColRowID, R.id.ColName });
lisView1.setAdapter(sAdap);
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(mActivity, "Wait",
"Loading...");
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
ShowData();
return null;
}
}
}
答案 2 :(得分:-1)
使用异步任务加载数据
private class Data extends AsyncTask<Object, Integer, Object> {
@Override
protected void onPreExecute() {
ProgressDialog progress = ProgressDialog.show(Activity.this, "",
"Loading...");
super.onPreExecute();
}
@Override
protected Object doInBackground(Object... params) {
//do hard work here
ShowData();
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
progress.getProgress();
}
@Override
protected void onPostExecute(Object result) {
progressBar.dismiss();
super.onPostExecute(result);
}
}
和onclick方法
new Data().execute().
答案 3 :(得分:-1)
将异步类设为public static,然后您可以从任何地方访问它。
private class Data extends AsyncTask<Object, Integer, Object> {
@Override
protected void onPreExecute() {
ProgressDialog progress = ProgressDialog.show(Activity.this, "",
"Loading...");
super.onPreExecute();
}
@Override
protected Object doInBackground(Object... params) {
//do hard work here
ShowData();
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
progress.getProgress();
}
@Override
protected void onPostExecute(Object result) {
progressBar.dismiss();
super.onPostExecute(result);
}
}