我想将此代码改编为AsyncTask类,是否可能?有人可以帮忙吗?我怎样才能从互联网上获取数据并通过AsyncTask类显示它?
我目前的代码:
MainActivity:
public class MainActivity extends Activity {
private CardContainer mCardContainer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.mainlayout);
mCardContainer = (CardContainer) findViewById(R.id.layoutview);
Resources r = getResources();
SimpleCardStackAdapter adapter = new SimpleCardStackAdapter(MainActivity.this);
adapter.add(new CardModel("Title1", "Description goes here", r.getDrawable(R.drawable.cats)));
adapter.add(new CardModel("Title2", "Description goes here", r.getDrawable(R.drawable.puppy)));
adapter.add(new CardModel("Title3", "Description goes here", r.getDrawable(R.drawable.sachin)));
CardModel cardModel = new CardModel(null,null);
cardModel.setOnClickListener(new CardModel.OnClickListener() {
@Override
public void OnClickListener() {
// Log.i("Swipeable Cards","I am pressing the card");
Toast.makeText(getApplicationContext(), "Pressing", Toast.LENGTH_SHORT).show();
}
});
cardModel.setOnCardDimissedListener(new CardModel.OnCardDimissedListener() {
@Override
public void onLike() {
//Log.i("Swipeable Cards","I like the card");
Toast.makeText(getApplicationContext(), "I like this", Toast.LENGTH_SHORT).show();
}
@Override
public void onDislike() {
// Log.i("Swipeable Cards","I dislike the card");
Toast.makeText(getApplicationContext(), "I dislike this", Toast.LENGTH_SHORT).show();
}
});
adapter.add(cardModel);
mCardContainer.setAdapter(adapter);
}
}
CardModel.class
public class CardModel {
private String title;
private String description;
private Drawable cardImageDrawable;
/* private Drawable cardLikeImageDrawable;
private Drawable cardDislikeImageDrawable;*/
private OnCardDimissedListener mOnCardDimissedListener = null;
private OnClickListener mOnClickListener = null;
public interface OnCardDimissedListener {
void onLike();
void onDislike();
}
public interface OnClickListener {
void OnClickListener();
}
public CardModel(String string, Drawable drawable) {
this(null, null, (Drawable)null);
}
public CardModel(String title, String description, Drawable cardImage) {
this.title = title;
this.description = description;
this.cardImageDrawable = cardImage;
}
public CardModel(String title, String description, Bitmap cardImage) {
this.title = title;
this.description = description;
this.cardImageDrawable = new BitmapDrawable(null, cardImage);
}
public CardModel(HashMap<String, String> map) {
this.title = title;
this.description = description;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Drawable getCardImageDrawable() {
return cardImageDrawable;
}
public void setCardImageDrawable(Drawable cardImageDrawable) {
this.cardImageDrawable = cardImageDrawable;
}
/*public Drawable getCardLikeImageDrawable() {
return cardLikeImageDrawable;
}
public void setCardLikeImageDrawable(Drawable cardLikeImageDrawable) {
this.cardLikeImageDrawable = cardLikeImageDrawable;
}
public Drawable getCardDislikeImageDrawable() {
return cardDislikeImageDrawable;
}
public void setCardDislikeImageDrawable(Drawable cardDislikeImageDrawable) {
this.cardDislikeImageDrawable = cardDislikeImageDrawable;
}
*/
public void setOnCardDimissedListener( OnCardDimissedListener listener ) {
this.mOnCardDimissedListener = listener;
}
public OnCardDimissedListener getOnCardDimissedListener() {
return this.mOnCardDimissedListener;
}
public void setOnClickListener( OnClickListener listener ) {
this.mOnClickListener = listener;
}
public OnClickListener getOnClickListener() {
return this.mOnClickListener;
}
}
答案 0 :(得分:0)
这是AsyncTask的工作方式,你还应该指定asynctask与你的代码有什么关系
public class ProgressTask extends AsyncTask<String, Void, String> {
private DefaultHttpClient httpclient;
private ProgressDialog dialog;
private Context context;
private static final String URL = "";
public ProgressTask(DefaultHttpClient client) {
this.dialog = new ProgressDialog(context);
this.httpclient = client;
}
//this is used to interact with the view objects or its childs before making requests to the website
protected void onPreExecute() {
this.dialog.setMessage("Loading");
this.dialog.show();
}
@Override
protected void onPostExecute(String params) {
if (dialog.isShowing()) {
dialog.dismiss();
}
//do what you want (the onPostExecute method is used to interact with view objects or its child)
}
//this code execute an async request to the URL page using POST and returns a string reponse to the onPostExecute method
protected String doInBackground(final String... args) {
HttpPost httppost = new HttpPost(URL);
try {
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
} catch (ClientProtocolException e) { // TODO Auto-generated catch block
} catch (IOException e) {
}
return null;
}
}
你可以称之为
ProgressTask pt = new ProgressTask(new DefaultHttpClient());
pt.execute(//string params);