问最简单的问题,我搜索了很多与我的问题相关但没有找到任何解决方案。我在Android应用程序中有一个布局,我使用listview
和progressbar
。在 AsyncTask 类中执行的所有任务。我想在加载列表之前在活动的中心显示进度条。谁能告诉我哪里出错或者这个解决方案需要哪些更改。在此先感谢。
我是怎么做的
activity_main:
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
android:visibility="invisible" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ListView>
MainActivity:
public class GettingAlerts extends ListActivity {
// Initialization
IPAddress ipAddressObject = new IPAddress();
public static String GETTING_ALERTS_URL = "http://"
+ IPAddress.IP_Address.toString()
+ "/MyServices/Alerts/AlertService.svc/gettingAllAlerts";
// TAGS
public static String TAG_NAME = "GetAllAlertsResult";
public static String TAG_ALERT_TITLE = "alertTitle";
public static String TAG_ALERT_DESCRIPTION = "alertDescription";
public static String TAG_ALERT_OWNER = "alertOwner";
public static String TAG_ALERT_DEADLINE = "alertDeadline";
static String Serv_Response = "";
ArrayList<Alerts> alertsList;
ProgressBar pg;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.alert_activity_list);
pg = (ProgressBar) findViewById(R.id.progressBar1);
// requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
// setProgressBarIndeterminateVisibility(true);
alertsList = new ArrayList<Alerts>();
ListView lv = getListView();
// setProgressBarIndeterminateVisibility(false);
new GettingAlertsList().execute();
}
private class GettingAlertsList extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
// LinearLayout linlaHeaderProgress = (LinearLayout)
// findViewById(R.id.linlaHeaderProgress);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(GETTING_ALERTS_URL);
HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpGet);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpEntity httpEntity = httpResponse.getEntity();
try {
Serv_Response = EntityUtils.toString(httpEntity);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (Serv_Response != null) {
JSONObject jsonObj = new JSONObject(Serv_Response);
JSONArray quiz = jsonObj.getJSONArray(TAG_NAME);
for (int i = 0; i < quiz.length(); i++) {
JSONObject c = quiz.getJSONObject(i);
String alert_title = c.getString(TAG_ALERT_TITLE);
String alert_owner = c.getString(TAG_ALERT_OWNER);
String alert_desc = c.getString(TAG_ALERT_DESCRIPTION);
String alert_dealdine = c.getString(TAG_ALERT_DEADLINE);
Alerts alertObject = new Alerts();
alertObject.setAlertTitle(alert_title);
alertObject.setAlertDescription(alert_desc);
alertObject.setAlertDeadline(alert_dealdine);
alertObject.setAlertOwner(alert_owner);
alertsList.add(alertObject);
}
}
} catch (JSONException e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
CustomAlertListAdapter adapter = new CustomAlertListAdapter(
GettingAlerts.this, alertsList);
setListAdapter(adapter);
pg.setVisibility(View.GONE);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pg.setVisibility(View.VISIBLE);
}
}
}
图片
正如您所见,进度条位于活动的顶部。在加载List之前,我想在活动的center
中使用进度条。
答案 0 :(得分:2)
试试这个..
删除android:layout_alignParentTop="true"
和android:layout_gravity="center_horizontal"
并添加android:layout_centerInParent="true"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="invisible" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ListView>
</RelativeLayout>
答案 1 :(得分:0)
更改可见性以显示/隐藏进度条。您可能需要设置更改某些属性,如样式:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="@+id/progress_bar_wrapper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@null"
android:visibility="gone">
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="100dp"
android:layout_height="30dp"
android:progress="0"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
答案 2 :(得分:0)
我假设您目前使用了LinearLayout
,而是在XML中获取RelativeLayout
并更改小部件的顺序,如下所示,并从ProgressBar中删除android:layout_alignParentTop="true"
。
<RelativeLayout
...
...>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ListView>
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>