我阅读了所有关于stackoverflow的问题,可以给我一个提示,但我无法得到它的工作。在Android中搜索时显示ProgressDialog
我有什么:
我有叫 “这个wantlist” 的activy这巫婆开始我的应用程序。在这个活动中,我有一个按钮,通过onSearchRequested()开始搜索;
我还有另一项名为“discogssearch”的活动,它可以调用Internet上的XML Api,解析数据并显示搜索结果。
所有这一切都工作正常。
我想要什么来实现的:
我想同时beeing发布的耗时API调用来显示ProgessDialog。
我的尝试:
我试着搜索开始之前,许多方面打开一个ProgessDialog才知,我必须这样做异步主线程。所以我把耗时的代码放到一个AsyncTask中并启动它。
会发生什么:
当我进入了一个文本,然后输入空话发生(如前),直到得出结果。现在,“discogssearch”观点与结果一起显示,ProgressDialog显示出来,但这一点已经完成了所有的工作。
我的代码:
wantlist.java
public class wantlist extends Activity implements OnClickListener {
/** Called when the activity is first created. */
ArrayList<Want> wants = new ArrayList<Want>();
@Override
public void onCreate(Bundle savedInstanceState) {
wants.add(new Want("Want1"));
wants.add(new Want("Want2"));
wants.add(new Want("Want3"));
wants.add(new Want("Want4"));
wants.add(new Want("Want5"));
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView) findViewById(R.id.WantList);
ImageButton b = (ImageButton) findViewById(R.id.searchbutton);
b.setOnClickListener(this);
lv.setAdapter(new WantAdapter(this,
R.layout.lplistitem, wants));
lv.setTextFilterEnabled(true);
}
public void onClick(View v) {
onSearchRequested();
}
}
discogssearch.java
public class discogssearch extends Activity implements OnItemClickListener{
/** Called when the activity is first created. */
private static final int PROGRESS_DIALOG = 0;
private static final String LOGTAG = discogssearch.class.getName();
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
// Eliminates color banding
window.setFormat(PixelFormat.RGBA_8888);
}
private ListView alv = null;
private ListView rlv = null;
private ListView llv = null;
ArrayList<Want> results = new ArrayList<Want>();
View.OnTouchListener gestureListener;
GestureDetector pageFlip;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchresults);
Intent intent = getIntent();
String query = intent.getStringExtra(SearchManager.QUERY);
searchDiscogs(query);
}
public void searchDiscogs(String query)
{
query = URLEncoder.encode(query);
alv = (ListView) findViewById(R.id.ArtistList);
rlv = (ListView) findViewById(R.id.ReleaseList);
llv = (ListView) findViewById(R.id.LabelList);
rlv.setOnItemClickListener(this);
rlv.setFastScrollEnabled(true);
rlv.setVerticalFadingEdgeEnabled(true);
results.clear();
AsyncTask<String, Integer, DiscogsXMLHandler> at = new AsyncDiscogsSearch().execute(query);
DiscogsXMLHandler discogsHandler = null;
try {
discogsHandler = at.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("WICHTIG: ",discogsHandler.getReleaseResults().size() + "");
alv.setAdapter(new WantAdapter(this,
R.layout.lplistitem, discogsHandler.getArtistResults()));
rlv.setAdapter(new WantAdapter(this,
R.layout.lplistitem, discogsHandler.getReleaseResults()));
llv.setAdapter(new WantAdapter(this,
R.layout.lplistitem, discogsHandler.getLabelResults()));
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if(arg0.equals(rlv))
{
String uri = ((Want)rlv.getItemAtPosition(arg2)).getDetailsUri();
Log.d("Itemclick", uri);
Intent viewUri = new Intent("android.intent.action.VIEW", Uri.parse(uri));
viewUri.setClass(this, releasedetails.class);
startActivity(viewUri);
}
}
public Dialog onCreateDialog(int dial)
{
switch(dial) {
case PROGRESS_DIALOG:
Log.d(LOGTAG,"Showing Progress Dialog");
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
return progressDialog;
default:
return null;
}
}
private Animation inFromRightAnimation() {
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromRight.setDuration(100);
inFromRight.setInterpolator(new AccelerateInterpolator());
return inFromRight;
}
private Animation outToLeftAnimation() {
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoLeft.setDuration(100);
outtoLeft.setInterpolator(new AccelerateInterpolator());
return outtoLeft;
}
private Animation inFromLeftAnimation() {
Animation inFromLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromLeft.setDuration(100);
inFromLeft.setInterpolator(new AccelerateInterpolator());
return inFromLeft;
}
private Animation outToRightAnimation() {
Animation outtoRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoRight.setDuration(100);
outtoRight.setInterpolator(new AccelerateInterpolator());
return outtoRight;
}
private class AsyncDiscogsSearch extends AsyncTask<String, Integer, DiscogsXMLHandler>{
public static final String APIKEY = "xxxxxxxx";
private final ProgressDialog pg = new ProgressDialog(discogssearch.this);
@Override
protected DiscogsXMLHandler doInBackground(String... params) {
String urlString = "http://www.discogs.com/search?type=all&q="+params[0]+"&f=xml&api_key="+APIKEY ;
//Retrieve XML-Data as InputStream
InputStream instream = GZipStreamHelper.getStream(urlString);
//Parse XML-Data
DiscogsXMLHandler discogsHandler = new DiscogsXMLHandler();
XMLParsingHelper.getInstance().parseAndHandle(instream, discogsHandler);
return discogsHandler;
}
@Override
protected void onPostExecute(DiscogsXMLHandler result) {
// TODO Auto-generated method stub
pg.dismiss();
}
@Override
protected void onPreExecute() {
pg.show();
}
}
}
感谢您的任何建议!
这工作就像一个魅力!谢谢。 – Chris 2011-02-28 23:17:18