如何使用AsyncTask

时间:2019-01-05 09:52:42

标签: android android-recyclerview android-asynctask recycler-adapter

我正在尝试从recyclerview数据库中的数据更新SQLite项,而这些数据又会不时使用从API提取的数据进行更新。

我有一个TabLayout,其中包含三个fragments

现在在一个片段中,我以recyclerView的形式展示用户玩的游戏。 recyclerView的每个项目均包含用户名,其高分和编号。击败他的高分的其他用户。

现在在该片段中,我正在调用API更新AsyncTask内的数据库,然后再次使用RecyclerView适配器类中的相同数据库来检索另一个AsyncTask中的数据。使用AsyncTask和Handler定期调用TimerTask

 @TargetApi(Build.VERSION_CODES.CUPCAKE)
    @RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
    public class PerformBackgroundTask extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... gameID) {

            try{

                SQLiteDatabase challengeDatabase = mContext.openOrCreateDatabase("Challenge", Context.MODE_PRIVATE, null);

                challengeDatabase.execSQL("CREATE TABLE IF NOT EXISTS DISPLAY_MESSAGE (gameID VARCHAR, highestScorer VARCHAR, count INT)");
                Cursor d = challengeDatabase.rawQuery("SELECT * FROM DISPLAY_MESSAGE WHERE gameID", null);

                int gameCode = d.getColumnIndex("gameID");
                int highestScorer = d.getColumnIndex("highestScorer");
                int count = d.getColumnIndex("count");

                d.moveToFirst();

                while(d!=null&&d.getCount()>0){
                    if((d.getString(gameCode)).equals(gameID)){

                        int k = Integer.parseInt(d.getString(count));

                        if(k>0)
                        {
                            return d.getString(highestScorer) + " and " + k + " others have beaten your highscore.";
                        }else if(k==0){
                            return d.getString(highestScorer) + " has beaten your highscore.";
                        }
                        else{
                            return "Yay! you have beaten all your opponents!";
                        }
                    }

                    d.moveToNext();
                }

                challengeDatabase.close();
            }catch (Exception e){
                e.printStackTrace();
            }
            return null;
        }
    }


    public GridAdapterChallenge(Context c, List<CardContainer> listCards, Vector<Object> cardDetails, String sname) {
        //selectedCards = new HashSet<Integer>();
        inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mContext = c;
        this.listCards = listCards;
        this.sname = sname;
        cards = cardDetails;
        //notifyDataSetChanged();

    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private int viewType;
        private ImageView imageView;
        private Button increaseScore, challengeFriend;
        private TextView challengeDetails, points, name, challengeCode;
        private ImageView nativeAdIcon;
        private TextView nativeAdTitle;
        private TextView nativeAdBody;
        private MediaView nativeAdMedia;
        private TextView nativeAdSocialContext;
        private Button nativeAdCallToAction;
        private ProgressBar detailProgress;


        public ViewHolder(@NonNull View itemView, int viewType) {
            super(itemView);

            view = itemView;
            this.viewType = viewType;
            if (viewType == 1) {
                this.imageView = (ImageView) itemView.findViewById(R.id.thumb);
                this.name = (TextView) itemView.findViewById(R.id.gameName);
                this.challengeCode = itemView.findViewById(R.id.challengeCode);
                this.points = itemView.findViewById(R.id.points);
                this.challengeDetails = itemView.findViewById(R.id.challengeDetail);
                this.increaseScore = itemView.findViewById(R.id.increaseScore);
                this.challengeFriend = itemView.findViewById(R.id.challengeFriend);
                this.challengeDetails = itemView.findViewById(R.id.challengeDetail);
                this.detailProgress = itemView.findViewById(R.id.detailProgressBar);
            } else {
                this.nativeAdIcon = (ImageView) itemView.findViewById(R.id.native_ad_icon);
                this.nativeAdTitle = (TextView) itemView.findViewById(R.id.native_ad_title);
                this.nativeAdBody = (TextView) itemView.findViewById(R.id.native_ad_body);
                this.nativeAdMedia = (MediaView) itemView.findViewById(R.id.native_ad_media);
                this.nativeAdSocialContext = (TextView) itemView.findViewById(R.id.native_ad_social_context);
                this.nativeAdCallToAction = (Button) itemView.findViewById(R.id.native_ad_call_to_action);
            }
        }
    }

    public void updateDetails(final TextView challengeDetails, final String gameId) {
        final Handler handler;
        handler = new Handler();
        Timer timer = new Timer();
        doAsynchronousTask = new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                        try {
                            PerformBackgroundTask performBackgroundTask = new PerformBackgroundTask();
                            challengeDetails.setText((CharSequence) performBackgroundTask.execute(gameId));

                        } catch (Exception e) {
                        }
                    }
                });
            }
        };
        timer.schedule(doAsynchronousTask, 4000, 5000); //execute in every 1000 ms
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
        if (viewHolder.viewType == 0) {
            if (adList != null && adList.size() > (i+1)/5 - 1 && adList.get((i+1)/5 - 1) != null) {
                NativeAd ad = adList.get((i+1)/5 - 1);
                viewHolder.nativeAdSocialContext.setText(ad.getAdSocialContext());
                viewHolder.nativeAdCallToAction.setText(ad.getAdCallToAction());
                viewHolder.nativeAdCallToAction.setVisibility(View.VISIBLE);
                viewHolder.nativeAdTitle.setText(ad.getAdHeadline());
                viewHolder.nativeAdBody.setText(ad.getAdBodyText());
                ad.registerViewForInteraction(viewHolder.itemView, viewHolder.nativeAdMedia, viewHolder.nativeAdIcon, Arrays.asList(viewHolder.nativeAdCallToAction, viewHolder.nativeAdMedia));
                NativeAd.Image adCoverImage = ad.getAdCoverImage();
                int bannerWidth = adCoverImage.getWidth();
                int bannerHeight = adCoverImage.getHeight();
                DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
                int mediaWidth = viewHolder.itemView.getWidth() > 0 ? viewHolder.itemView.getWidth() : metrics.widthPixels;
                viewHolder.nativeAdMedia.setLayoutParams(new LinearLayout.LayoutParams(mediaWidth, Math.min(
                        (int) (((double) mediaWidth / (double) bannerWidth) * bannerHeight), metrics.heightPixels / 3)));
                ad.registerViewForInteraction(viewHolder.itemView, viewHolder.nativeAdMedia, Arrays.asList(viewHolder.nativeAdCallToAction, viewHolder.nativeAdMedia));
                ad.registerViewForInteraction(viewHolder.itemView, viewHolder.nativeAdMedia);
            }
        } else{

            viewHolder.name.setText(((CardContainer)cards.get(i)).name);
            challengeCode = getChallengeCode(i);
            viewHolder.challengeCode.setText(challengeCode);
            DisplayMetrics displayMetrics = new DisplayMetrics();
            ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

            if(getHighScore(((CardContainer)cards.get(i)).gameId)>0){
                PerformBackgroundTask performBackgroundTask = new PerformBackgroundTask();
                viewHolder.challengeDetails.setText(performBackgroundTask.execute(gameId).toString());
                //updateDetails(viewHolder.challengeDetails, ((CardContainer)cards.get(i)).gameId);
                }
            else{
                viewHolder.challengeDetails.setText("Press Increase Score To Play the Game");
            }
            viewHolder.detailProgress.setVisibility(View.GONE);


            int width = displayMetrics.widthPixels;
            try {
                final String uri = ((CardContainer) cards.get(i)).imageurl;
                if (uri != null) {
                    Picasso.get().load(uri)
                            .placeholder(R.mipmap.place)
                            .error(R.mipmap.place)
                            .resize(width / 2, width / 2)
                            .centerCrop()
                            .into(viewHolder.imageView);

                }
            }
            catch (Exception e){
                final String uri = null;
            }

            viewHolder.points.setText(String.valueOf(getHighScore(((CardContainer)cards.get(i)).gameId)));

            viewHolder.increaseScore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    if (((CardContainer) cards.get(i)).link.contains("play.google.com")) {
                        openAppRating(mContext,((CardContainer) cards.get(i)).link.split("id=", 2)[1]);
                    }
                    else{
                        name = ((CardContainer) cards.get(i)).name;
                        link = ((CardContainer) cards.get(i)).link;
                        imageurl = ((CardContainer) cards.get(i)).imageurl;
                        type = ((CardContainer) cards.get(i)).type;
                        packageName = ((CardContainer) cards.get(i)).packageName;
                        gameId = ((CardContainer)cards.get(i)).gameId;

                        challengeNames.add(name);

                        updateDatabase(name, gameId, Config.uid+"@"+gameId, 0, viewHolder.points);

                        //String gameName, String gameId, String mychCode, int myHighScore
                    }
                }
            });
            viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                String name = ((CardContainer) cards.get(i)).name;
                String link = ((CardContainer) cards.get(i)).link;
                String imageurl = ((CardContainer) cards.get(i)).imageurl;
                String type = ((CardContainer) cards.get(i)).type;
                String packageName = ((CardContainer) cards.get(i)).packageName;
                Bitmap bitmap = null;
                Integer xyz = android.os.Build.VERSION.SDK_INT;
                @Override
                public boolean onLongClick(View view) {

                    bitmap = ((BitmapDrawable) viewHolder.imageView.getDrawable()).getBitmap();
                    if (sname.equals("fav")) {
                        //    Toast.makeText(mContext, "in fav", Toast.LENGTH_SHORT).show();
                        final Dialog dialog = new Dialog(mContext);
                        dialog.setContentView(R.layout.custom_dialog);
                        TextView dialogText=dialog.findViewById(R.id.txt_dia);
                        dialogText.setText("Do you want to remove "+name+" from your favorites?");
                        dialog.show();
                        Button yes_button = (Button) dialog.findViewById(R.id.btn_yes);
                        yes_button.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                favf = new FavFunction(mContext);
                                int resultFav=favf.removeFromFavorite(link);
                                if (resultFav>=0){
                                    cards.remove(resultFav);

                                    notifyDataSetChanged();
                                }
                                dialog.dismiss();
                            }
                        });
                        Button no_button = (Button) dialog.findViewById(R.id.btn_no);
                        no_button.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });
                        return true;
                    }
                    else{
                        final Dialog dialog = new Dialog(mContext);
                        dialog.setContentView(R.layout.custom_dialog);
                        TextView dialogText=dialog.findViewById(R.id.txt_dia);
                        dialogText.setText("Adding "+name+" to favorites... Do you also want to create it's homescreen shortcut?");
                        dialog.show();
                        Button yes_button = (Button) dialog.findViewById(R.id.btn_yes);
                        yes_button.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                favf = new FavFunction(mContext);
                                Boolean favstatus=favf.addToFavorite(imageurl, link ,name,type,packageName);
                                favf.addShortcut(name, link , imageurl,type,packageName,bitmap);
                                if(favstatus){
                                    ((Activity) mContext).recreate();
                                }
                                dialog.dismiss();
                            }
                        });
                        Button no_button = (Button) dialog.findViewById(R.id.btn_no);
                        no_button.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                favf = new FavFunction(mContext);
                                Boolean favstatus=favf.addToFavorite(imageurl, link, name,type,packageName);
                                if(favstatus){
                                    ((Activity) mContext).recreate();
                                }
                                dialog.dismiss();
                            }
                        });
                        return true;
                    }
                }
            });

            viewHolder.challengeFriend.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent sendIntent = new Intent();
                    sendIntent.setAction(Intent.ACTION_SEND);
                    sendIntent.putExtra(Intent.EXTRA_TEXT, "Hey! I challenge you to beat my highscore " + getHighScore(gameId) + " in this awesome game. Enter my challenge code " + challengeCode + " in the \"Challenges\" Tab of 101 Game Store App. You can download the app from bit.ly/101gamestore");
                    sendIntent.setType("text/plain");
                    mContext.startActivity(sendIntent);
                }
            });
        }
    }

我要更新编号。 recyclerView的每个项目中得分高者的得分。但是,发生的情况是TimerTask一开始,UI就会冻结。数据已成功获取。任何解决方案都比我的更好。

2 个答案:

答案 0 :(得分:0)

在这里看看....

 class MyAsyncTask extends AsyncTask<String, Void, String> {
    Context mContext;
    ArrayList<String> values= new ArrayList<String>();

    public MyAsyncTask(Context context) {
        mContext = context;
    }

    @Override
    protected String doInBackground(String... params) {
        // do your all database operation here and try to stroe in arraylist
        values.add("Data comes from database");
        return "";

    }

    @Override
    protected void onPostExecute(String list) {
     // Do your View update here.
    // All your Recycle View here
    yourRecycleView.setAdapter(new MyAdapter(context,values))
    }
}

注意:-在AsynTask中,所有绑定工作都以onPreExecute()方法执行,所有数据库或长线程都以doInBackground()方法执行(例如从数据库中获取数据,下载文件或上传文件等)并以onPostExecute()方法更新视图。

  

如何使用AsyncTask更新RecyclerView项目?

按照相同的过程,以doInBackground()方法获取数据,并以onPostExecute()方法通知适配器。在其他AsynTask中执行此操作

答案 1 :(得分:0)

要更新适配器,应在主线程中进行。 AsyncTask提供onPostExecute方法。

请勿替换适配器,而应替换适配器中的数据。

migrationBuilder.CreateTable(
            name: "Students",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                DateCreated = table.Column<DateTime>(nullable: true, defaultValueSql: "GETDATE()"),
                UserCreated = table.Column<string>(nullable: true),
                DateModified = table.Column<DateTime>(nullable: true, defaultValueSql: "GETDATE()"),
                UserModified = table.Column<string>(nullable: true),
                Name = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Students", x => x.Id);
            });