简体   繁体   中英

Return Value Try catch?

I try to do set Text in onBindViewHolder, I'm sure that I create it with no mistake, because if I set dummy data setText, it does setText to recycler view.

尝试 try catch 但不执行任何设置文本

But if I create try catch, it doesn't change anything. Help me.


try {
            Transaksi transaksi = arrayTransaksi.get(position);
            Calendar calendar = Calendar.getInstance();
            Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(transaksi.getTanggal_transaksi());
            long milis = date1.getTime();
            if (milis == calendar.get(Calendar.DATE)){
                holder.textHariTransaksi.setText("Today");
                holder.textTanggal.setText(transaksi.getTanggal_transaksi());
                holder.textHarga.setText(String.valueOf(transaksi.getHarga_transaksi()));
            }else if(calendar.get(Calendar.DATE) - milis == 1){
                holder.textHariTransaksi.setText("Yesterday");
                holder.textTanggal.setText(transaksi.getTanggal_transaksi());
                holder.textHarga.setText(String.valueOf(transaksi.getHarga_transaksi()));
            }else{
                SimpleDateFormat dateFormat = new SimpleDateFormat("l");
                holder.textHariTransaksi.setText(dateFormat.format(date1));
                holder.textTanggal.setText(transaksi.getTanggal_transaksi());
                holder.textHarga.setText(String.valueOf(transaksi.getHarga_transaksi()));
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

if im not using try catch, it say " error: unreported exception ParseException; must be caught or declared to be thrown "

If you're trying to setText you have to access the function from a UI thread, in order to do that you have to call runOnUiThread function.

Activity.runOnUiThread(() =>{ call setText here })

I'm using a mobile right now, that's why my answer is brief.

Edit: Also check if all the data you're trying to set as text is an actual string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM