进度而加载的ListView(使用的AsyncTask) [英] ProgressBar while loading ListView (using AsyncTask)
问题描述
我试图加载一个自定义ListView和事后显示进度条 - 隐藏它。 我使用的异步任务,但由于某种原因 - 直到所有的列表视图中加载的内容来看没有设置和previous布局视图被卡住
下面是我的code:
私人的ListView listViewGameResults;
受保护的视图dialogLayout;
受保护的ArrayList<游戏> listGames;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.adresults);
GameResultsLoader gameResultsLoader =新GameResultsLoader();
gameResultsLoader.execute();
}
私有类GameResultsLoader扩展的AsyncTask<虚空,虚空,虚空> {
私人GameResultsAdapter适配器;
公共GameResultsLoader(){
}
@覆盖
在preExecute保护无效(){
}
@覆盖
保护无效doInBackground(虚空...... PARAMS){
尝试 {
listGames = GameResultsCache.getInstance()getGameResults()。
适配器=新GameResultsAdapter(getBaseContext(),listGames);
listViewGameResults =(ListView控件)findViewById(R.id.listViewGameResults);
}
赶上(例外五){
// TODO自动生成的catch块
完();
}
返回null;
}
@覆盖
保护无效onPostExecute(无效RES){
listViewGameResults.setAdapter(适配器);
listViewGameResults.setDivider(空);
listViewGameResults.setDividerHeight(0);
进度PB =(进度)findViewById(R.id.progressbar_loading);
pb.setVisibility(View.GONE);
}
}
进度和ListView在我的布局:
<进度
风格=机器人:ATTR / progressBarStyle
机器人:layout_centerInParent =真
机器人:ID =@ + ID / progressbar_loading
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT/>
<的ListView
机器人:ID =@ + ID / listViewGameResults
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:layout_marginTop =1dip
机器人:layout_below =@ + ID / upperstrip
机器人:layout_above =@ + ID / ivDownStrip/>
您需要设置进度
gone.and 默认可视性preExecute()
设置可见光和 onPostExecute()
集了。
<进度
风格=机器人:ATTR / progressBarStyle
机器人:layout_centerInParent =真正的'输入code here`
机器人:ID =@ + ID / progressbar_loading
机器人:能见度=水涨船高
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT/>
<的ListView
机器人:ID =@ + ID / listViewGameResults
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:layout_marginTop =1dip
机器人:layout_below =@ + ID / upperstrip
机器人:layout_above =@ + ID / ivDownStrip/>
您的活动应该是这样的。
公共类演示延伸活动{
私人的ListView listViewGameResults;
受保护的视图dialogLayout;
受保护的ArrayList<游戏> listGames;
进度进展情况;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.adresults);
进度=(进度)findViewByid(R.id.progressbar_loading);
GameResultsLoader gameResultsLoader =新GameResultsLoader(本);
gameResultsLoader.execute();
}
}
使用一个单独的类的AsyncTask
公共类GameResultsLoader扩展的AsyncTask<虚空,虚空,虚空> {
私人GameResultsAdapter适配器;
演示Demo;
公共GameResultsLoader(演示Demo){
this.demo =演示;
}
@覆盖
在preExecute保护无效(){
demo.progress.setvisibility(View.Visible);
}
@覆盖
保护无效doInBackground(虚空...... PARAMS){
尝试 {
listGames = GameResultsCache.getInstance()getGameResults()。
适配器=新GameResultsAdapter(getBaseContext(),listGames);
listViewGameResults =(ListView控件)findViewById(R.id.listViewGameResults);
}
赶上(例外五){
// TODO自动生成的catch块
完();
}
返回null;
}
@覆盖
保护无效onPostExecute(无效RES){
listViewGameResults.setAdapter(适配器);
listViewGameResults.setDivider(空);
listViewGameResults.setDividerHeight(0);
进度PB =(进度)findViewById(R.id.progressbar_loading);
demo.progress.setVisibility(View.GONE);
}
}
I'm trying to show a progress bar when loading a custom ListView and afterwards - hide it. I'm using ASync task, but for some reason - The content view is not set and the previous layout view is stuck until all the list view is loaded.
Here is my code:
private ListView listViewGameResults;
protected View dialogLayout;
protected ArrayList<Game> listGames;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adresults);
GameResultsLoader gameResultsLoader = new GameResultsLoader();
gameResultsLoader.execute();
}
private class GameResultsLoader extends AsyncTask<Void, Void, Void> {
private GameResultsAdapter adapter;
public GameResultsLoader() {
}
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... params) {
try {
listGames = GameResultsCache.getInstance().getGameResults();
adapter = new GameResultsAdapter(getBaseContext(), listGames);
listViewGameResults = (ListView)findViewById(R.id.listViewGameResults);
}
catch (Exception e) {
// TODO Auto-generated catch block
finish();
}
return null;
}
@Override
protected void onPostExecute(Void res) {
listViewGameResults.setAdapter(adapter);
listViewGameResults.setDivider(null);
listViewGameResults.setDividerHeight(0);
ProgressBar pb = (ProgressBar)findViewById(R.id.progressbar_loading);
pb.setVisibility(View.GONE);
}
}
ProgressBar and ListView in my layout:
<ProgressBar
style="?android:attr/progressBarStyle"
android:layout_centerInParent="true"
android:id="@+id/progressbar_loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/listViewGameResults"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="1dip"
android:layout_below="@+id/upperstrip"
android:layout_above="@+id/ivDownStrip" />
You need to set default visibility of progressBar
gone.and onPreExecute()
set Visible and onPostExecute()
set gone.
<ProgressBar
style="?android:attr/progressBarStyle"
android:layout_centerInParent="true"`enter code here`
android:id="@+id/progressbar_loading"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/listViewGameResults"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="1dip"
android:layout_below="@+id/upperstrip"
android:layout_above="@+id/ivDownStrip" />
your Activity should look like this
public class demo extends Activity{
private ListView listViewGameResults;
protected View dialogLayout;
protected ArrayList<Game> listGames;
progressBar progress;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adresults);
progress=(ProgressBar)findViewByid(R.id.progressbar_loading);
GameResultsLoader gameResultsLoader = new GameResultsLoader(this);
gameResultsLoader.execute();
}
}
Use one separate class for AsyncTask
public class GameResultsLoader extends AsyncTask<Void, Void, Void> {
private GameResultsAdapter adapter;
Demo demo;
public GameResultsLoader(Demo demo) {
this.demo=demo;
}
@Override
protected void onPreExecute() {
demo.progress.setvisibility(View.Visible);
}
@Override
protected Void doInBackground(Void... params) {
try {
listGames = GameResultsCache.getInstance().getGameResults();
adapter = new GameResultsAdapter(getBaseContext(), listGames);
listViewGameResults = (ListView)findViewById(R.id.listViewGameResults);
}
catch (Exception e) {
// TODO Auto-generated catch block
finish();
}
return null;
}
@Override
protected void onPostExecute(Void res) {
listViewGameResults.setAdapter(adapter);
listViewGameResults.setDivider(null);
listViewGameResults.setDividerHeight(0);
ProgressBar pb = (ProgressBar)findViewById(R.id.progressbar_loading);
demo.progress.setVisibility(View.GONE);
}
}
这篇关于进度而加载的ListView(使用的AsyncTask)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!