2017-07-16 74 views
0

的如何獲得變量在doInBackground走出的AsyncTask得到變量在doInBackground走出的AsyncTask

請求API URL設置新的一樣,並得到新的像和像模式現在

如何獲得新的像和像模式出來的AsyncTask的使用的setText

得到這樣從GetDataLike的AsyncTask:

  new GetDataLike().execute(textViewQuoteId.getText().toString()); 
      String newLike = GetDataLike().newLike; // get like this from GetDataLike 
      String modeLike = GetDataLike().modeLike; // get like this from GetDataLike 
      textViewQuoteLike.setText("پسندیدم ("+newLike+")"); 
      Toast.makeText(rootView.getContext(), "لایک شما برای سخن "+textViewProfileName.getText().toString()+" ثبت شد.", 
        Toast.LENGTH_LONG).show(); 

JSON結果例如:

{"Like":{"quote_like":"27","quote_like_mode":"1"}} 

QuoteArrayAdapter.java

package com.example.adapter; 

import android.content.ClipData; 
import android.content.ClipboardManager; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapShader; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Typeface; 
import android.os.AsyncTask; 
import android.support.annotation.Nullable; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.example.R; 
import com.example.model.QuoteDataModel; 
import com.example.parser.JSONParser; 
import com.example.utils.Keys; 
import com.squareup.picasso.Picasso; 
import com.squareup.picasso.Transformation; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.List; 

import static android.content.Context.CLIPBOARD_SERVICE; 

public class QuoteArrayAdapter extends ArrayAdapter<QuoteDataModel> { 

    List<QuoteDataModel> modelList; 
    Context context; 
    private LayoutInflater mInflater; 

    // Constructors 
    public QuoteArrayAdapter(Context context, List<QuoteDataModel> objects) { 
     super(context, 0, objects); 
     this.context = context; 
     this.mInflater = LayoutInflater.from(context); 
     modelList = objects; 
    } 

    @Override 
    public QuoteDataModel getItem(int position) { 
     return modelList.get(position); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     final ViewHolder vh; 
     if (convertView == null) { 
      View view = mInflater.inflate(R.layout.quote_row, parent, false); 
      vh = ViewHolder.create((RelativeLayout) view); 
      view.setTag(vh); 
     } else { 
      vh = (ViewHolder) convertView.getTag(); 
     } 

     QuoteDataModel item = getItem(position); 

     vh.textViewQuoteContent.setText(item.getQuoteContent()); 
     vh.textViewProfileName.setText(item.getProfileName()); 
     vh.textViewQuoteLike.setText("پسندیدم ("+item.getQuoteLike()+")"); 
     vh.textViewQuoteCopy.setText("کپی"); 
     vh.textViewQuoteShare.setText("اشتراک"); 
     vh.textViewQuoteId.setText(item.getQuoteId()); 
     Picasso.with(context).load(item.getProfilePhoto()).placeholder(R.drawable.empty_profile_photo).error(R.drawable.empty_profile_photo).transform(new CircleTransform()).into(vh.imageViewProfilePhoto); 

     return vh.rootView; 
    } 

    private static class ViewHolder { 
     public final RelativeLayout rootView; 
     public final ImageView imageViewProfilePhoto; 
     public final TextView textViewQuoteContent; 
     public final TextView textViewProfileName; 
     public final TextView textViewQuoteLike; 
     public final TextView textViewQuoteCopy; 
     public final TextView textViewQuoteShare; 
     public final TextView textViewQuoteId; 
     public final TextView textViewQuoteLikeIcon; 
     public final TextView textViewQuoteShareIcon; 
     public final TextView textViewQuoteCopyIcon; 

     private ViewHolder(RelativeLayout rootView, ImageView imageViewProfilePhoto, TextView textViewQuoteContent, TextView textViewProfileName, TextView textViewQuoteLike, TextView textViewQuoteCopy, TextView textViewQuoteShare, TextView textViewQuoteId, TextView textViewQuoteLikeIcon, TextView textViewQuoteShareIcon, TextView textViewQuoteCopyIcon) { 
      this.rootView = rootView; 
      this.imageViewProfilePhoto = imageViewProfilePhoto; 
      this.textViewQuoteContent = textViewQuoteContent; 
      this.textViewProfileName = textViewProfileName; 
      this.textViewQuoteLike = textViewQuoteLike; 
      this.textViewQuoteCopy = textViewQuoteCopy; 
      this.textViewQuoteShare = textViewQuoteShare; 
      this.textViewQuoteId = textViewQuoteId; 
      this.textViewQuoteLikeIcon = textViewQuoteLikeIcon; 
      this.textViewQuoteShareIcon = textViewQuoteShareIcon; 
      this.textViewQuoteCopyIcon = textViewQuoteCopyIcon; 
     } 

     public static ViewHolder create(final RelativeLayout rootView) { 
      ImageView imageViewProfilePhoto = (ImageView) rootView.findViewById(R.id.imageViewProfilePhoto); 
      final TextView textViewQuoteContent = (TextView) rootView.findViewById(R.id.textViewQuoteContent); 
      final TextView textViewProfileName = (TextView) rootView.findViewById(R.id.textViewProfileName); 
      final TextView textViewQuoteLike = (TextView) rootView.findViewById(R.id.textViewQuoteLike); 
      TextView textViewQuoteCopy = (TextView) rootView.findViewById(R.id.textViewQuoteCopy); 
      TextView textViewQuoteShare = (TextView) rootView.findViewById(R.id.textViewQuoteShare); 
      final TextView textViewQuoteId = (TextView) rootView.findViewById(R.id.textViewQuoteId); 
      final TextView textViewQuoteLikeIcon = (TextView) rootView.findViewById(R.id.textViewQuoteLikeIcon); 
      final TextView textViewQuoteShareIcon = (TextView) rootView.findViewById(R.id.textViewQuoteShareIcon); 
      final TextView textViewQuoteCopyIcon = (TextView) rootView.findViewById(R.id.textViewQuoteCopyIcon); 

      textViewQuoteLike.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        new GetDataLike().execute(textViewQuoteId.getText().toString()); 
        String currentLike = textViewQuoteLike.getText().toString(); 
        currentLike = currentLike.replace("پسندیدم (",""); 
        currentLike = currentLike.replace(")",""); 
        int newLike = Integer.valueOf(currentLike.toString()) + 1; 
        textViewQuoteLike.setText("پسندیدم ("+newLike+")"); 
        Toast.makeText(rootView.getContext(), "لایک شما برای سخن "+textViewProfileName.getText().toString()+" ثبت شد.", 
          Toast.LENGTH_LONG).show(); 
       } 
      }); 
      textViewQuoteLikeIcon.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        new GetDataLike().execute(textViewQuoteId.getText().toString()); 
        String currentLike = textViewQuoteLike.getText().toString(); 
        currentLike = currentLike.replace("پسندیدم (",""); 
        currentLike = currentLike.replace(")",""); 
        int newLike = Integer.valueOf(currentLike.toString()) + 1; 
        textViewQuoteLike.setText("پسندیدم ("+newLike+")"); 
        Toast.makeText(rootView.getContext(), "لایک شما برای سخن "+textViewProfileName.getText().toString()+" ثبت شد.", 
          Toast.LENGTH_LONG).show(); 
       } 
      }); 

      Typeface font = Typeface.createFromAsset(rootView.getContext().getAssets(), "fontawesome-webfont.ttf"); 
      textViewQuoteLikeIcon.setTypeface(font); 
      textViewQuoteLikeIcon.setText(String.valueOf((char) 0xf164)); 

      return new ViewHolder(rootView, imageViewProfilePhoto, textViewQuoteContent, textViewProfileName, textViewQuoteLike, textViewQuoteCopy, textViewQuoteShare, textViewQuoteId, textViewQuoteLikeIcon, textViewQuoteShareIcon, textViewQuoteCopyIcon); 
     } 
    } 


    static class GetDataLike extends AsyncTask<String, String, String> { 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
     } 

     @Nullable 
     @Override 
     protected String doInBackground(String... params) { 

      String quoteId = params[0]; 

      JSONObject jsonObject = JSONParser.getDataFromWeb("http://example.com/api-service/v1/platform_quote_like/?id="+quoteId); 
      try { 
       if (jsonObject != null) { 
        if(jsonObject.length() > 0) { 
         JSONArray array = jsonObject.getJSONArray(Keys.KEY_LIKE); 
         int lenArray = array.length(); 
         if(lenArray > 0) { 
          for(int jIndex = 0; jIndex < lenArray; jIndex++) { 
           JSONObject innerObject = array.getJSONObject(jIndex); 
           String quote_like = innerObject.getString(Keys.KEY_QUOTE_LIKE); 
       String quote_like_mode = innerObject.getString(Keys.KEY_QUOTE_LIKE_Mode); 
          } 
         } 
        } 
       } else { 

       } 
      } catch (JSONException je) { 
       Log.i(JSONParser.TAG, "" + je.getLocalizedMessage()); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(String aVoid) { 
      super.onPostExecute(aVoid); 
     } 
    } 

    public class CircleTransform implements Transformation { 
     @Override 
     public Bitmap transform(Bitmap source) { 
      int size = Math.min(source.getWidth(), source.getHeight()); 

      int x = (source.getWidth() - size)/2; 
      int y = (source.getHeight() - size)/2; 

      Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size); 
      if (squaredBitmap != source) { 
       source.recycle(); 
      } 

      Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig()); 

      Canvas canvas = new Canvas(bitmap); 
      Paint paint = new Paint(); 
      BitmapShader shader = new BitmapShader(squaredBitmap, 
        BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP); 
      paint.setShader(shader); 
      paint.setAntiAlias(true); 

      float r = size/2f; 
      canvas.drawCircle(r, r, r, paint); 

      squaredBitmap.recycle(); 
      return bitmap; 
     } 

     @Override 
     public String key() { 
      return "circle"; 
     } 
    } 
} 
+0

你應該看看如何異步任務工作,要做到這一點在你的onPostExcecute您的AsyncTask,而不是在同步方式如你所說 – Thomas

+0

怎麼做而不做的AsyncTask,請例如請求API,並獲得新的像數和模式就像沒有的AsyncTask如果AsynsTask是問題 – user8215914

+0

的AsyncTask是使用最好的東西,你只是用它錯了。正如我所說的,這將有助於如果您想了解asynctasks一些文檔和示例。在這種情況下,你應該使用'onPostExecute'中的AsyncTask的 – Thomas

回答

2

你就必須從doInBackground方法和onPostExecute方法,你可以setText.for多個值這是在你的情況,你應該做這樣的

返回字符串
public class CollectValues{ 
public String quote_like; 
public String quote_like_mode;} 

現在在doInBackground方法,你應該做這樣的

CollectValue cv = new CollectValue(); 
    cv.quote_like=//your response; 
    cv.quote_like=//your response; 
      return cv; 

終於在onPostExecute方法,你應該做這樣的

@Override 
    protected void onPostExecute(CollectValues cv) { 
     //your textview here 
     textview1.setText(cv.quote_like); 
     textview2.setText(cv.quote_like_mode); 
    } 

這將解決你的問題..

+0

謝謝,我想出來的AsyncTask – user8215914

+0

基本上你onPostExecute方法將在UI線程so..i無法理解你到底想要什麼跑?你'onPostExecute需要()''要和SharedPreferences'在你需要它retreive它 –

+0

保存價值。 – Stanojkovic

相關問題