0

我正在开发一个 Android 应用程序,当应用程序从 Web 服务获取一些数据时,它会显示一段时间的空白屏幕。我怎样才能防止这种情况发生?我会很感激帮助。

    protected void onListItemClick(ListView l, View v, final int position,
        long id) {
    super.onListItemClick(l, v, position, id);

    progressDialog = ProgressDialog.show(ProjectListActivity.this,
            "Please wait...", "Loading...");

    new Thread() {

        public void run() {

            try {
                String project = titles.get(position - 1);

                performBackgroundProcess(project);

            } catch (Exception e) {

                Log.e("tag", e.getMessage());

            }

            progressDialog.dismiss();
        }

    }.start();





private void performBackgroundProcess(String project) {

    String spaceId = null;
    String spaceName = null;
    /*
     * for (Space space : spaces){
     * if(space.getName().equalsIgnoreCase((String) ((TextView)
     * v).getText())){ spaceId = space.getId(); } }
     */
    for (Space space : spaces) {

        if (project.equals(space.getName())) {

            newSpace = space;
        }

    }

    spaceId = newSpace.getId();
    spaceName = newSpace.getName();

    /*
     * Intent intent = new Intent(this, SpaceComponentsActivity.class);
     * intent.putExtra("spaceId", spaceId); intent.putExtra("tabId", 0);
     * intent.putExtra("className", "TicketListActivity"); TabSettings ts =
     * new TabSettings(); ts.setSelTab(1); this.startActivity(intent);
     */
    Intent intent = new Intent(this, SpaceComponentsActivity.class);
    intent.putExtra("spaceId", spaceId);
    intent.putExtra("tabId", 0);
    intent.putExtra("spaceName", spaceName);

    // intent.putExtra("className", "TicketListActivity");
    TabSettings ts = new TabSettings();
    ts.setSelTab(0);
    ts.setSelTabClass("TicketListActivity");
    this.startActivity(intent);
4

1 回答 1

1

这意味着您正在 UI 线程上运行与网络相关的操作。您应该考虑使用 anAsyncTask<?, ?, ?>来在网络线程中运行操作以防止 UI 锁定。

例子:

@Override
public void onResume() {
    super.onResume();
    new MyAsyncTask().execute();
}

private class MyAsyncTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {

         // Do your network operations here

    }

    @Override
    protected void onPostExecute(Void result) {

       // Add items to your ListView here


    }

}
于 2012-07-14T09:03:07.157 回答