android studio不能从适配器SetText

时间:2016-04-18 10:17:26

标签: android textview listview-adapter

所以我在CountDownTimer的自定义列表视图适配器中使用此功能,但不知怎的,我无法将文本更新为UI。帮帮我PLZ

private Handler mHandler = new Handler();
    private Runnable updateRemainingTimeRunnable = new Runnable() {
        @Override
        public void run() {
                long waktu = millisUntilFinished;
                updateTimeRemaining(waktu);
        }
    };



    private void startUpdateTimer() {
        Timer tmr = new Timer();
        tmr.schedule(new TimerTask() {
            @Override
            public void run() {
                mHandler.post(updateRemainingTimeRunnable);
            }
        }, 1000, 1000);
    }



    public void updateTimeRemaining(long currentTime) {
        millisUntilFinished = (int) currentTime - 1000;
        if (millisUntilFinished > 0) {
            tempminute = (int) Math.floor((millisUntilFinished/1000)/60);
            minutes = tempminute%60;
            seconds = (millisUntilFinished/1000)%60;
            hours = tempminute/60;

            Log.i("myarc","00 day " + hours + " hr " + minutes + " min " + seconds + " sec");

            context.runOnUiThread(new Runnable() {
                public void run() {
                    vouchercountdown.setText("00 day " + hours + " hr " + minutes + " min " + seconds + " sec");
                }
            });
            //vouchercountdown.setText("00 day " + hours + " hr " + minutes + " min " + seconds + " sec");
        } else {
            context.runOnUiThread(new Runnable() {
                public void run() {
                    vouchercountdown.setText("00 day 00 hr 00 min 00 sec");
                }
            });
            //vouchercountdown.setText("00 day 00 hr 00 min 00 sec");
        }
    }

但是当我第一次从getView中调用它时,文本发生了变化。

public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View listViewItem = inflater.inflate(R.layout.list_layout_voucher, null, true);

        ImageView imagevoucher = (ImageView) listViewItem.findViewById(R.id.ImageVoucher);
        vouchercountdown = (TextView) listViewItem.findViewById(R.id.CountdownText);
        final TextView soldcounter = (TextView) listViewItem.findViewById(R.id.SoldCount);
        TextView buybutton = (TextView) listViewItem.findViewById(R.id.BuyText);

        buybutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                countersold[position] += 1;
                soldcounter.setText("Sold \n " + String.valueOf(countersold[position]));
            }
        });

        updateTimeRemaining(Integer.valueOf(timeinseconds[position]));


        soldcounter.setText("Sold \n " + String.valueOf(countersold[position]));
        imagevoucher.setImageResource(imageid[position]);
        return  listViewItem;
    }

那么为什么textView无法更改startUpdateTimer代码循环中的文本?

感谢。

1 个答案:

答案 0 :(得分:0)

如果没有更新UI,我会像下面那样重新格式化Thread,请尝试这样

{{1}}