Android Timer and TimerTask scheduling

12:58 AM Nilesh Deokar 0 Comments

All you need to do is write a function which will schedule the timerTask and call it in the onCreate().
then manage the activity life cycle with the timer task.

public void callAsynchronousTask() {

        final Handler handler = new Handler();
        timer = new Timer();
        doAsynchronousTask = new TimerTask() {
            @Override            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                        try {


                            TaskFinder tf = new TaskFinder(MainActivity.this);
                            tf.execute();
                            isTimerRunning=true;

                        } catch (Exception e) {
                            // TODO Auto-generated catch block                        }
                    }
                });
            }
        };
        timer.schedule(doAsynchronousTask, 0, 30000); //execute in every 30 sec    }
Call this function from the onCreate()

protected void onCreate(Bundle savedInstanceState) {
    // requestWindowFeature(Window.FEATURE_ACTION_BAR);    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    ...
    callAsynchronousTask();
}
here is how you manage the onStop() and onRestart() of the activity.

@Overrideprotected void onStop() {
    //thread stop here
    if (timer != null) {
        timer.cancel();
        timer = null;
    }
    super.onStop();

}

@Overrideprotected void onRestart() {
    callAsynchronousTask();
    super.onRestart();

}

0 comments:

Recyclerview With Multiple view types

2:55 AM Nilesh Deokar 0 Comments

Ever wondered how to implement the multiple view types in recycler view?



Method invocation in Recyclerview:

1. Constuctor method
2. getItemViewType()
3. onCreateViewHolder()
4. onBindViewHolder()

to achieve the tast all we need to de is implement the  getItemViewType(), And it will take care of the  viewTyp parameter in   onCreateViewHolder().

/**
 * Created by Nilesh Deokar on 26/08/15.
 */
public class MyHoodAdapter extends RecyclerView.Adapter {


    LayoutInflater inflater;
    List data = Collections.emptyList();
    Context mCOn;
    Calendar calendar;

    private static final int TYPE_CRAVING = 1;
    private static final int TYPE_SHARED_DISH = 3;
    private static final int TYPE_ONLY_SHARE_DIHS = 7;

    public MyHoodAdapter(Context context, List list, int userId, String userName, int b_id) {
        this.mCOn = context;
        inflater = LayoutInflater.from(context);
        this.data = list;
        this.userId = userId;
        this.userName = userName;
        this.b_id = b_id;

    }


    @Override
    public int getItemViewType(int position) {

        return model.getStrType();

    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        switch (viewType) {
            case TYPE_SHARED_DISH:
                return new SharedDishViewHolder(LayoutInflater.from(mCOn).inflate(R.layout.myhood_custom_row_dish, parent, false));
            case TYPE_ONLY_SHARE_DIHS:
                return new OnlySharedDishViewHolder(LayoutInflater.from(mCOn).inflate(R.layout.nearby_custom_row_shared, parent, false));
            case TYPE_CRAVING:
                return new MyViewHolder(LayoutInflater.from(mCOn).inflate(R.layout.myhood_custom_row_craving, parent, false));
         
        }
        return null;


    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {

        if (holder.getItemViewType() == TYPE_SHARED_DISH) {
            SharedDishViewHolder mholder = (SharedDishViewHolder) holder;
            MyHoodModel model = data.get(position);

           /*
            * set the values to the view from your model object
            */

          } else if(holder.getItemViewType() == TYPE_ONLY_SHARE_DIHS){
            OnlySharedDishViewHolder mholder = (OnlySharedDishViewHolder) holder;
            MyHoodModel mo = data.get(position);

          }else if(holder.getItemViewType() == TYPE_CRAVING){
            MyViewHolder mHolder = (MyViewHolder) holder;
            MyHoodModel model = data.get(position);

        }
      }

    }


    @Override
    public int getItemCount() {
        return data.size();
    }




    public class MyViewHolder extends MainViewHolder {
     
    public MyViewHolder(View itemView) {
            super(itemView);
            /* Bind your object here */
         }
    }

    public class SharedDishViewHolder extends MainViewHolder {

    public SharedDishViewHolder(View itemView) {
            super(itemView);
            /* Bind your object here */
         }
    }


    public class OnlySharedDishViewHolder extends MainViewHolder {

    public OnlySharedDishViewHolder(View itemView) {
            super(itemView);
            /* Bind your object here */
         }
    }

  public class MainViewHolder extends RecyclerView.ViewHolder {
        public MainViewHolder(View v) {
            super(v);
        }
    }
}


0 comments:

How to resolve Glide setTag() issue

12:57 AM Nilesh Deokar 0 Comments

The setTag() is common method for storing data for temporary purpose and utilize it later on.
but if you are using Glide lib for image processing purpose then you might not able to use the setTag() function.

This is the stack trace..

 java.lang.IllegalArgumentException: You must not call setTag() on a view Glide is targeting
            at com.bumptech.glide.request.target.ViewTarget.getRequest(ViewTarget.java:105)
            at com.bumptech.glide.GenericRequestBuilder.into(GenericRequestBuilder.java:605)
            at net.twisterrob.app.android.view.Adapter.bindView()
            ...


This is the common use case of the Glide..
    Glide.with(mCon).load(model.getStrImagePath()).
                        placeholder(R.drawable.loading).
                        error(R.drawable.dish_placeholder)
                        .fitCenter()
                        .into(mholder.ivDishImg);

Problem arises when u write..
    mholder.ivDishImg.setTag(position);

we can use  setTag(ViewTarget,data) a constant id and data while adding tag to the ViewTarget.
and retrive the same by using getTag(ViewTarget)

ImageView ivDishImg = (ImageView) itemView.findViewById(R.id.ivMyHoodDish);
             
mholder.ivDishImg.setTag(R.id.ivMyHoodDish,position);
mHolder.ivDishImg.setOnClickListener(this);

  public void onClick(View v) {
        int position = 0;
   
        try {
            position = (Integer) v.getTag(R.id.ivMyHoodDish);
        } catch (Exception e) {
            e.printStackTrace();
       }
}








0 comments: