Saturday, July 2, 2011

[android-developers] Re: Async Task

In doInBackground do processes/operation that require no updating on
the UI. On postExecute always do the updates on the UI. Remember that
resources must be available everytime. Let me show you something from
my apps what I mean by the updates on UI from non-ui thread....


protected class UpdateUi extends AsyncTask<String, String, String>
{

public String text="Add to Favorites";
public GeoPoint Item=null;
public SQLiteDatabase datbase=null;
public UpdateUi(GeoPoint itemPoint)
{

this.Item=itemPoint;


}
@Override
protected String doInBackground(String... arg0) {
datbase = Map.this.openOrCreateDatabase("favorites",
SQLiteDatabase.OPEN_READONLY, null);
Cursor c = datbase.rawQuery("SELECT lat, long FROM favorites;",
null);
c.moveToFirst();
if(datbase!=null)
{
datbase.close();

while(c.isLast()==false)
{

if(c.getInt(0)==Item.getLatitudeE6() && c.getInt(1) ==
Item.getLongitudeE6())
{

text = "Already to Favorites";
}
c.moveToNext();
}

c.close();
}
return text;
}

@Override
protected void onPostExecute(String result)
{ //updates on the UI based on previous
results
findViewById(R.id.infoLayout).setVisibility(View.VISIBLE);
Button b= (Button)findViewById(R.id.add2favorites);
ImageView iv = (ImageView)findViewById(R.id.srce);

b.setText(text)
if(text.equalsIgnoreCase("Already to Favorites"))
{
((TextView)findViewById(R.id.infotext)).setText(txt);
b.setClickable(false);
b.setEnabled(false);
iv.setImageBitmap(BitmapFactory.decodeResource(getResources(),
R.drawable.srcecelocheck));
}
else
{
b.setClickable(true);
b.setEnabled(true);
iv.setImageBitmap(BitmapFactory.decodeResource(getResources(),
R.drawable.srcecelo));

}
super.onPostExecute(result);
}

}

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate