gpt4 book ai didi

android - 帮助 ProgressDisplay

转载 作者:行者123 更新时间:2023-11-29 22:23:14 27 4
gpt4 key购买 nike

我希望我的应用在连接到在线资源时以 ProgressDialog 开始,抓取数据,解析数据,然后用它填充微调器。下面代码中的 DirectoryTransaction.doDepts() 将数据连接并解析为 String[][] DeptResults。我已经尝试了其他问题和答案但没有成功,并且真的很想让它起作用。就像现在一样,应用程序在完成所有这些工作时似乎会卡住几秒钟。我宁愿让它显示对话框。

这是我现在的代码:

package com.test.directory;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class Directory extends Activity {

public static boolean test = false;
public static int Start = 1;
public static int Page = 1;
public static int ResultsCount = 0;
public static int sdk = new Integer(Build.VERSION.SDK).intValue();
public static String[] Fields = new String[6];
public static String[][] DeptResults;
public static String[][] ReturnResults;
public static String[] ResultsDetails = new String[11];

public EditText fname = (EditText) findViewById(R.id.fname);
public EditText lname = (EditText) findViewById(R.id.lname);
public EditText aim = (EditText) findViewById(R.id.aim);
public EditText phone = (EditText) findViewById(R.id.phone);
public Spinner depts = (Spinner) findViewById(R.id.dept);


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchscreen);

DirectoryTransaction.doDepts();

//Sets up and fills the department spinner
final ArrayAdapter<CharSequence> deptsAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
deptsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
depts.setAdapter(deptsAdapter);
for(int i = 0 ; i < DeptResults.length ; i++){
deptsAdapter.add(DeptResults[i][0]);
}

//Search button
findViewById(R.id.search).setOnClickListener(new OnClickListener() {
public void onClick(View v) {

Start = 1;
Page = 1;

//Put all the info from the fields into a string array
Fields[0] = fname.getText().toString();
Fields[1] = lname.getText().toString();
Fields[2] = aim.getText().toString();
Fields[3] = phone.getText().toString();
Fields[4] = DeptResults[(int)depts.getSelectedItemId()][1];
Fields[5] = Integer.toString(Start);

DirectoryTransaction.doSearch(Directory.Start, Fields[0], Fields[1], Fields[2], Fields[4], Fields[3]);


//Shows error if more than 300 results
if(ResultsCount > 300){
Toast.makeText(Directory.this, "Too many results found, please narrow your search.", Toast.LENGTH_LONG).show();
}else{
//Load a new Intent and start the results activity
Intent i = new Intent(v.getContext(), DirectoryResults.class);
startActivity(i);
}
}
});

//Clear Button
findViewById(R.id.clear).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Clears all the fields, sets the Spinner to 0.
fname.setText("");
lname.setText("");
aim.setText("");
phone.setText("");
depts.setSelection(0);
}
});
}
}

基本上,这是我在 ProgressDialog 显示时想要做的事情。

        DirectoryTransaction.doDepts();

//Sets up and fills the department spinner
final ArrayAdapter<CharSequence> deptsAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
deptsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
depts.setAdapter(deptsAdapter);
for(int i = 0 ; i < DeptResults.length ; i++){
deptsAdapter.add(DeptResults[i][0]);
}

有什么想法/例子可以告诉我我需要什么吗?我已经看了很多,但一直没弄明白。

谢谢,我成功了。我不确定我是否需要它,所以我会玩弄它。这是代码:

package com.test.directory;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class Directory extends Activity {

public static boolean test = false;
public static int Start = 1;
public static int Page = 1;
public static int ResultsCount = 0;
public static int sdk = new Integer(Build.VERSION.SDK).intValue();
public static String[] Fields = new String[6];
public static String[][] DeptResults;
public static String[][] ReturnResults;
public static String[] ResultsDetails = new String[11];

public ArrayAdapter<CharSequence> deptsAdapter;

ProgressDialog dialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchscreen);

new getAllData().execute(this);

final EditText fname = (EditText) findViewById(R.id.fname);
final EditText lname = (EditText) findViewById(R.id.lname);
final EditText aim = (EditText) findViewById(R.id.aim);
final EditText phone = (EditText) findViewById(R.id.phone);
final Spinner depts = (Spinner) findViewById(R.id.dept);

//Sets up and fills the department spinner
deptsAdapter = new ArrayAdapter<CharSequence>(Directory.this, android.R.layout.simple_spinner_item);
deptsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
depts.setAdapter(deptsAdapter);

//Search button
findViewById(R.id.search).setOnClickListener(new OnClickListener() {
public void onClick(View v) {

Start = 1;
Page = 1;

//Put all the info from the fields into a string array
Fields[0] = fname.getText().toString();
Fields[1] = lname.getText().toString();
Fields[2] = aim.getText().toString();
Fields[3] = phone.getText().toString();
Fields[4] = DeptResults[(int)depts.getSelectedItemId()][1];
Fields[5] = Integer.toString(Start);

DirectoryTransaction.doSearch(Directory.Start, Fields[0], Fields[1], Fields[2], Fields[4], Fields[3]);


//Shows error if more than 300 results
if(ResultsCount > 300){
Toast.makeText(Directory.this, "Too many results found, please narrow your search.", Toast.LENGTH_LONG).show();
}else{
//Load a new Intent and start the results activity
Intent i = new Intent(v.getContext(), DirectoryResults.class);
startActivity(i);
}
}
});

//Clear Button
findViewById(R.id.clear).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Clears all the fields, sets the Spinner to 0.
fname.setText("");
lname.setText("");
aim.setText("");
phone.setText("");
depts.setSelection(0);
}
});
}

private class getAllData extends AsyncTask<Context, Void, Cursor> {
protected void onPreExecute () {
dialog = ProgressDialog.show(Directory.this, "",
"Loading. Please wait...", true);
}

@Override
protected Cursor doInBackground(Context... params) {
DirectoryTransaction.doDepts();

return null;
}

protected void onPostExecute(Cursor c) {

for(int i = 0 ; i < DeptResults.length ; i++){
deptsAdapter.add(DeptResults[i][0]);
}
//update the UI or do soemthing
dialog.dismiss();
}
}

}

最佳答案

这是我的做法:

我将 ProgressDialog dialog; 放在我的类文件的顶部。

在我的 onCreate() 方法中我有这个:

new getAllData().execute(this);   

由于我的任务需要一些时间,所以我让它在后台线程中运行(如果您的应用“卡住”,我建议您也这样做)。后台线程的代码如下所示:

 private class getAllData extends AsyncTask<Context, Void, Cursor> {
protected void onPreExecute () {
dialog = ProgressDialog.show(Shows.this, "",
"Loading shows. Please wait...", true);
}

@Override
protected Cursor doInBackground(Context... params) {
//do all the work
}

protected void onPostExecute(Cursor c) {
//update the UI or do soemthing
dialog.dismiss();
}
}

这里有更多关于 ASync 的信息:http://developer.android.com/reference/android/os/AsyncTask.html

如果您要使用 Async,您的 onPreExecute 将显示对话框,doInBackground() 将获取您需要从网络获取的任何内容,然后 onPostExecute() 会将所有内容粘贴到您的微调器中。

如果您不想这样做,那么您仍然可以在完成时执行 ProgressDialog.show(...) 和 dialog.dimiss()

关于android - 帮助 ProgressDisplay,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6629063/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com