如何在onPreExecute和onProgressUpdate中显示进度条?
如果我有以下代码..感谢您的帮助
loading.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:minHeight="?android:attr/listPreferredItemHeight">
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
/>
<TextView
android:text="Loading Events…"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="@+id/progressbar"
android:layout_centerVertical="true"
android:gravity="center"
android:padding="10dip"
android:textColor="#FFFFFF" />
</RelativeLayout>
listplaceholder.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false"
/>
</LinearLayout>
EventsActivity.class
public class EventsActivity extends ListActivity {
syncEvent lastsyncEvent = null;
EventDataSet sitesList = null;
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
//refreshEvents call new syncEvent().execute();
refreshEvents();
private class syncEvent extends AsyncTask<String, Integer, String>{
private ProgressBar progressBar;
@Override
protected String doInBackground(String... arg0) {
.....
return null;
}
@Override
protected void onPostExecute(String result) {
ListAdapter adapter = new SimpleAdapter(EventsActivity.this, mylist , R.layout.eventitem,
new String[] { "name", "createat" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPreExecute() {
progressBar = new ProgressBar(EventsActivity.this, null, R.layout.loading );
progressBar.setVisibility(View.VISIBLE);
}
}
If i have the following code.. thanks you for your help
loading.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:minHeight="?android:attr/listPreferredItemHeight">
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
/>
<TextView
android:text="Loading Events…"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="@+id/progressbar"
android:layout_centerVertical="true"
android:gravity="center"
android:padding="10dip"
android:textColor="#FFFFFF" />
</RelativeLayout>
listplaceholder.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false"
/>
</LinearLayout>
EventsActivity.class
public class EventsActivity extends ListActivity {
syncEvent lastsyncEvent = null;
EventDataSet sitesList = null;
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
//refreshEvents call new syncEvent().execute();
refreshEvents();
private class syncEvent extends AsyncTask<String, Integer, String>{
private ProgressBar progressBar;
@Override
protected String doInBackground(String... arg0) {
.....
return null;
}
@Override
protected void onPostExecute(String result) {
ListAdapter adapter = new SimpleAdapter(EventsActivity.this, mylist , R.layout.eventitem,
new String[] { "name", "createat" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPreExecute() {
progressBar = new ProgressBar(EventsActivity.this, null, R.layout.loading );
progressBar.setVisibility(View.VISIBLE);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确,您想显示您在布局中定义的进度栏。我认为唯一的问题是您错误地引用了进度条。尝试:
If i understand correctly, you want to show the ProgressBar you have defined in your layout. I think the only problem is that you are referencing the progress bar incorrectly. Try:
顺便说一句,我找到了一些解决方案......它可能不是一个干净的代码......但是,我希望它对某些目的有用
---来自上面的代码
我删除
loading.xml
并将一些代码添加到listplaceholder.xml
中,而不是listplaceholder.xml
*---添加几行
AsyncTask
*By the way, i find some solutions... it might not be a clean code..however, i hope it useful for some purpose
---From the above code
I delete
loading.xml
and add some code intolistplaceholder.xml
insteadlistplaceholder.xml
*---Add a couple lines to
AsyncTask
*