I want to update my TextView in android

109
June 19, 2017, at 02:34 AM

Hi i hope that you can help m. I'm using RecycleView and want to update my textview with the number of row of my table in sqlite db after each insert on it.But it didn't work for me.

This my Activity MAin

@BindView(R.id.materialViewPager)
MaterialViewPager mViewPager;
Calendar cal = Calendar.getInstance();
String t[];
final Timer timer = new Timer();
private int nbrr,nbbc;
private int a;
int dayOfWeek;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setTitle("");
    ButterKnife.bind(this);
    final Context cnx=this;
    Dispposition dispposition =new Dispposition(this);
    dispposition.execute("");
    RetreiveUser user  = new RetreiveUser(this);
    user.execute("");
    final ReservationSource data =new ReservationSource(cnx);
    data.open();
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {

            Intent serviceIntent = new Intent(cnx, ReservationService.class);
            startService(serviceIntent);
            final ReservationSource data =new ReservationSource(cnx);
            data.open();
            a= data.CountNumConfirmation();
            Log.v("NBR", String.valueOf(a));


        }}, 0, 5000);


    final Toolbar toolbar = mViewPager.getToolbar();
    if (toolbar != null) {
        setSupportActionBar(toolbar);
    }

    mViewPager.getViewPager().setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
        @Override
        public  Fragment getItem(int position) {
            Date d = new Date();
            long today =d.getTime();
            long yesterday=d.getTime()-(24*60*60*1000);
            long tommorow =d.getTime()+(24*60*60*1000);
            Date date=null;
            int nbr_res=0,nbr_con=0;

            switch (position % 3) {
                case 0:
                    try {
                        SimpleDateFormat formatter =  new SimpleDateFormat("yyyy-MM-dd", Locale.US);
                        formatter.setLenient(false);
                        String yesterdayString = formatter.format(new Date(yesterday));
                        dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
                        if(dayOfWeek==1)
                            dayOfWeek=7;
                        else dayOfWeek--;
                         date = formatter.parse(yesterdayString);
                        t =retrieve(yesterdayString);

                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();}
                        return RecyclerViewFragment.newInstance(DayByWeek(dayOfWeek), Month(Integer.parseInt(t[1])) + " " + t[0], Integer.parseInt(t[2]), a, 1);
                        case 1:
                    SimpleDateFormat formatter =  new SimpleDateFormat("yyyy-MM-dd", Locale.US);
                    formatter.setLenient(false);
                    String todayString = formatter.format(new Date(today));
                    date =new Date();

                    dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
                   t =retrieve(todayString);
                    return RecyclerViewFragment.newInstance(DayByWeek(dayOfWeek),Month(Integer.parseInt(t[1]))+" "+t[0],Integer.parseInt(t[2]),1,1);
               case 2:
                   try {
                        formatter =  new SimpleDateFormat("yyyy-MM-dd", Locale.US);
                       formatter.setLenient(false);
                       String tommorowString = formatter.format(new Date(tommorow));
                        date = formatter.parse(tommorowString);
                        dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
                       if(dayOfWeek==7)
                           dayOfWeek=1;
                       else dayOfWeek++;
                       t =retrieve(tommorowString);
                   } catch (ParseException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();}
                return RecyclerViewFragment.newInstance(DayByWeek(dayOfWeek),Month(Integer.parseInt(t[1]))+" "+t[0],Integer.parseInt(t[2]),1,1);
                default:
                    return RecyclerViewFragment.newInstance("","",0,0,0);
            }
        }
        @Override
        public int getCount() {
            return 3;
        }
        @Override
        public CharSequence getPageTitle(int position) {

            switch (position % 3) {
                case 0:
                    return "Hier";
                case 1:

                    return "Aujourd'hui";
                case 2:

                    return "Demain";

            }
            return "";
        }
    });
    mViewPager.setMaterialViewPagerListener(new MaterialViewPager.Listener() {
        @Override
        public HeaderDesign getHeaderDesign(int page) {
            switch (page) {
                case 0:
                    return HeaderDesign.fromColorAndDrawable(
                        getResources().getColor(R.color.green),
                            ContextCompat.getDrawable(cnx, R.drawable.food));
                case 1:
                    return HeaderDesign.fromColorResAndDrawable(
                        R.color.blue,
                            ContextCompat.getDrawable(cnx, R.drawable.piscine));
                case 2:
                    return HeaderDesign.fromColorResAndDrawable(
                        R.color.cyan,
                            ContextCompat.getDrawable(cnx, R.drawable.paillote));
            }
            //execute others actions if needed (ex : modify your header logo)
            return null;
        }
    });
    mViewPager.getViewPager().setOffscreenPageLimit(mViewPager.getViewPager().getAdapter().getCount());
    mViewPager.getPagerTitleStrip().setViewPager(mViewPager.getViewPager());
    final View logo = findViewById(R.id.logo_white);
    if (logo != null) {
        logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPager.notifyHeaderChanged();
                Toast.makeText(getApplicationContext(), "Yes, the title is clickable", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
public String DayByWeek(int jour)
{
    switch (jour)
    {
        case 1: return "DIM";
        case 2: return"LUN";
        case 3: return"MAR";
        case 4: return"MER";
        case 5: return"JEU";
        case 6: return"VEN";
        case 7: return"SAM";
        default:return "";
    }
}
public String Month(int mois)
{
    switch (mois)
    {
        case 6: return "Juin";
        case 7: return"Juillet";
        case 8: return"Aout";
        case 9: return"Septembere";
        default:return "";

    }
}
public String [] retrieve(String date)
{
      String [] tab = date.split("-");
   return tab;
}

}

This my RecycleView fragment

    RecyclerViewFragment fragment = new RecyclerViewFragment();
    Bundle args = new Bundle();
    args.putString("SEMAINE", semaine);
    args.putInt("JOUR", jour);
    args.putString("MOIS", mois);
    args.putInt("Reserve",nbr_res);
    args.putInt("Confirme",nbr_con);
    fragment.setArguments(args);
    return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        semaine = getArguments().getString("SEMAINE");
        jour = getArguments().getInt("JOUR");
        mois = getArguments().getString("MOIS");
        nbr_res=getArguments().getInt("Reserve");
        nbr_con=getArguments().getInt("Confirme");

    }
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_recyclerview, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    ButterKnife.bind(this, view);
    final List<DataObject> items = new ArrayList<>();
    final List<DateObject> item = new ArrayList<>();
    String [] pieces = {"Parasol","Salon","Piscine","Resto","Snack","Paillote"};
    String nbrr=String.valueOf(nbr_res);
    if (nbrr.length()==1)
        nbrr="0"+nbrr;
    String nbrc=String.valueOf(nbr_con);
    if (nbrc.length()==1)
        nbrc="0"+nbrc;



    for (int i = 0; i <ITEM_COUNT; ++i) {
            DataObject obj = new DataObject("28",
                    nbrc,nbrr,pieces[i]);
            items.add(i, obj);

    }
    for (int i = 0; i < 1; ++i) {
        DateObject obj = new DateObject(jour,
                semaine,mois);
        item.add(i, obj);

    }

    //setup materialviewpager
    if (GRID_LAYOUT) {
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
    } else {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    }
    mRecyclerView.setHasFixedSize(true);
    //Use this now
    mRecyclerView.addItemDecoration(new MaterialViewPagerHeaderDecorator());
    mRecyclerView.setAdapter(new TestRecyclerViewAdapter(items,item));
   // mRecyclerView.setAdapter(new MySecondRecyclerViewAdapter((ArrayList<DateObject>) item));
}

}

and this my RecycleViewAdapter

List<DataObject> mDataset;
List<DateObject> mDateset;
static final int TYPE_HEADER = 0;
static final int TYPE_CELL = 1;
public TestRecyclerViewAdapter(List<DataObject> mDataset,List<DateObject> mDateset) {
    this.mDataset = mDataset;
    this.mDateset = mDateset;
}
@Override
public int getItemViewType(int position) {
    switch (position) {
        case 0:
            return TYPE_HEADER;
        default:
            return TYPE_CELL;
    }
}
@Override
public int getItemCount() {
    return mDataset.size();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = null;
    switch (viewType) {
        case TYPE_HEADER: {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_item_card_big, parent, false);
            final SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
            formatter.setLenient(false);
            Button next = (Button) view.findViewById(R.id.next);
            Button previous = (Button) view.findViewById(R.id.previous);
            final TextView mois= (TextView) view.findViewById(R.id.mois);
            final CalendarView cal = (CalendarView) view.findViewById(R.id.calendrier);


            next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(mois.getText()=="Juin")
                    {
                        mois.setText("Juillet");
                        String oldTime = "01.07.2017, 00:00";
                        Date oldDate = null;
                        try {
                            oldDate = formatter.parse(oldTime);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        long oldMillis = oldDate.getTime();
                        cal.setDate(oldMillis);

                    }
                    else if(mois.getText()=="Juillet")
                    {
                        mois.setText("Aout");
                        String oldTime = "01.08.2017, 00:00";
                        Date oldDate = null;
                        try {
                            oldDate = formatter.parse(oldTime);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        long oldMillis = oldDate.getTime();
                        cal.setDate(oldMillis);
                    }
                    else
                    {
                        mois.setText("Juin");
                        String oldTime = "25.06.2017, 00:00";
                        Date oldDate = null;
                        try {
                            oldDate = formatter.parse(oldTime);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        long oldMillis = oldDate.getTime();
                        cal.setDate(oldMillis);
                    }
                }
            });
            previous.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(mois.getText()=="Aout")
                    {
                        mois.setText("Juillet");
                        String oldTime = "01.07.2017, 00:00";
                        Date oldDate = null;
                        try {
                            oldDate = formatter.parse(oldTime);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        long oldMillis = oldDate.getTime();
                        cal.setDate(oldMillis);

                    }
                    else if(mois.getText()=="Juillet")
                    {
                        mois.setText("Juin");
                        String oldTime = "25.06.2017, 00:00";
                        Date oldDate = null;
                        try {
                            oldDate = formatter.parse(oldTime);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        long oldMillis = oldDate.getTime();
                        cal.setDate(oldMillis);
                    }
                    else
                    {
                        mois.setText("Aout");
                        String oldTime = "01.08.2017, 00:00";
                        Date oldDate = null;
                        try {
                            oldDate = formatter.parse(oldTime);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        long oldMillis = oldDate.getTime();
                        cal.setDate(oldMillis);
                    }
                }
            });
            return new ViewHolder(view) {
            };
        }
        case TYPE_CELL: {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_item_card_small, parent, false);
            return new ViewHolder(view) {
            };
        }
    }
    return null;
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    switch (getItemViewType(position)) {
        case TYPE_HEADER:
            holder.j.setText(String.valueOf(mDateset.get(position).getJour()));
            holder.js.setText(mDateset.get(position).getSemaine());
            holder.ma.setText(mDateset.get(position).getMois());
            break;
        case TYPE_CELL:
            holder.piece.setText(mDataset.get(position).getPiece());
            holder.nbr_conf.setText(mDataset.get(position).getNbr_Conf());

          holder.nbr_res.setText(mDataset.get(position).getNbr_res());
            holder.nbr_res.setText(mDataset.get(position).getNbr_res());
            holder.nbr_dispo.setText(mDataset.get(position).getNbr_dispo());
            break;
    }
}
public class ViewHolder extends RecyclerView.ViewHolder {
    TextView piece;
    TextView nbr_dispo;
    TextView nbr_res;
    TextView nbr_conf;
    TextView j;
    TextView js;
    TextView ma;
    public ViewHolder(View view)
    {
        super(view);
        piece = (TextView) view.findViewById(R.id.piece);
        nbr_conf = (TextView) view.findViewById(R.id.nbr_confirme);
        nbr_res = (TextView) view.findViewById(R.id.nbr_reserve);
        nbr_dispo = (TextView) view.findViewById(R.id.nbr_disponible);
        j = (TextView) itemView.findViewById(R.id.jour);
        js = (TextView) itemView.findViewById(R.id.jour_semaine);
        ma= (TextView) itemView.findViewById(R.id.mois_annee);
        Log.i("TAG", "Adding Listener");
    }
}

}

thank you for helping me.

READ ALSO
What is the proper way to load SubType&#39;s into ArrayList of type of BaseType with sqlite select?

What is the proper way to load SubType's into ArrayList of type of BaseType with sqlite select?

I have a table of BaseType which have the column Type for SubType objects

84
who is responsible for offset maintenance?

who is responsible for offset maintenance?

Here are the Kafka docs for public ConsumerRecords<K,V> poll(long timeout)

112
Saving and Reading an ArrayList

Saving and Reading an ArrayList

Saving method(save list is an Arraylist)-

86
Show confirm dialog in Flux architecture

Show confirm dialog in Flux architecture

I'm trying to understand Flux architectureI'm using JavaFX and the FluxFX experimental framework

87