【发布时间】:2014-06-02 16:38:16
【问题描述】:
好的,所以现在我有一个ListView,它正在通过PHP script. 填充信息现在,列表一次加载一个。通过这个,我的意思是用户可以看到每个列表何时加载(例如,他们看到一个项目,一秒钟后他们看到第二个项目,等等)我想要做的是等待,直到所有项目都被检索然后显示他们一下子。在发生这种情况时,要有某种类型的“加载”指示器(可能是旋转圆圈类型的交易)。有什么办法可以实现吗?这是我的代码:
public class MainActivity extends ActionBarActivity {
ArrayList<Location> arrayOfLocations;
LocationAdapter adapter;
Button refresh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
// Construct the data source
arrayOfLocations = new ArrayList<Location>();
// Create the adapter to convert the array to views
adapter = new LocationAdapter(this, arrayOfLocations);
getData();
// Attach the adapter to a ListView
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
}
}
所以,我的 getData() 方法将每个位置添加到适配器中,并且 那么我的 Adapter 类就是将数据放入 ListView 的:
public class LocationAdapter extends ArrayAdapter<Location> {
public LocationAdapter(Context context, ArrayList<Location> locations) {
super(context, R.layout.item_location, locations);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Location location = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.item_location, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvDetails = (TextView) convertView
.findViewById(R.id.tvDetails);
TextView tvDistance = (TextView) convertView
.findViewById(R.id.tvDistance);
TextView tvHours = (TextView) convertView.findViewById(R.id.tvHours);
ImageView ivIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
// Populate the data into the template view using the data object
tvName.setText(location.name);
tvDetails.setText(location.details);
tvDistance.setText(location.distance);
tvHours.setText(location.hours);
ivIcon.setImageBitmap(location.icon);
// Return the completed view to render on screen
return convertView;
}
}
另外,我有一个简单的加载指示器的代码:
public class MainActivity extends Activity {
private ProgressDialog progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progress = new ProgressDialog(this);
}
public void open(View view) {
progress.setMessage("Loading...Please Wait");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.show();
final int totalProgressTime = 100;
final Thread t = new Thread() {
@Override
public void run() {
int jumpTime = 0;
while (jumpTime < totalProgressTime) {
try {
sleep(200);
jumpTime += 5;
progress.setProgress(jumpTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
t.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
所以基本上,我想做的是弄清楚如何做到这一点: 1.活动开始时,显示“加载中”指示符 2. 将所有项目加载到 ListView 3. 列出所有项目后,去掉“正在加载”指示符,显示ListView
有什么想法吗?谢谢。
【问题讨论】:
-
我会为此使用 AsynchTask。
-
我正在使用 AsyncTask 加载每个 ListView 项目。您建议如何使用 AsyncTask 来检查列表是否已满?
-
您将需要一个进度对话框,以及您的 AsyncTask 中的一个回调。然后是 AsyncTask,确保将 Context 作为构造函数的参数传递,以及 CallBack。覆盖 onPreExecute() 以显示对话框。重写 onPostExecute() 以将 doInBackground() 返回的值传递给您的 CallBack 并关闭您的进度对话框。我希望这对你有意义。
-
我从未使用过回调,但它确实有点道理。我不知道该怎么做是我怎么知道 ListView 何时完成加载?
-
当你在片段或活动中调用你的 AsyncTask 时,使用:asyncTask.execute(无论你在此处传入什么参数),asynctask 将完成 doInBackground() 中的所有工作,例如加载数据、.. . 只要doInBackground()完成,就会把数据传给onPostExecute()。在 onPostExecute() 中,您可以检查数据是否有效,如果数据有效,则关闭对话框。在您调用异步任务类的片段或活动中,您将需要在其中更新列表的地方实现回调。