1

我想每 1 分钟更改一次 textview。问题是它只更改第一次,在其他情况下保持不变。这是我使用每 1 分钟调用一次的可运行文件的地方:

public void ReduceTimeIteration() 
{
    try
    {
        final Handler m_handler = new Handler();

        runOnUiThread(new Runnable() {  
            @Override 
            public void run() {
                DiscussArrayAdapter.MyMethod(SplashActivity.this,(long)60000);
                m_handler.postDelayed(this, 60000); 
            }
        });
    }
    catch(Exception e)
    {
        Log.e("Exception: ",e+" in ReduceTimeIteration() of SplashActivity.java");
    }
}

这是我要更新的类textview TimeLeft

public class DiscussArrayAdapter extends ArrayAdapter<OneComment> {

    private TextView countryName;
    private List<OneComment> countries = new ArrayList<OneComment>();
    private LinearLayout wrapper;
    public View row;
    static OneComment coment;
    static TextView TimeLeft;

    @Override
    public void add(OneComment object) 
    {
        try
        {
            countries.add(object);
            super.add(object);
        }
        catch(Exception e)
        {
            Log.e("Exception: ",e+" Exception occured in add() of DiscussArrayAdapter.java");
        }
    }

    public DiscussArrayAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    public int getCount() 
    {
        try
        {
            return this.countries.size();
        }
        catch(Exception e)
        {
            Log.e("Exception: ",e+" Exception occured in getCount() of DiscussArrayAdapter.java");
        }
        return this.countries.size();
    }

    public OneComment getItem(int index)
    {
        try
        {
            return this.countries.get(index);
        }
        catch(Exception e)
        {
            Log.e("Exception: ",e+" Exception occured in getItem() of DiscussArrayAdapter.java");
        }
        return null;        
    }

    public View getView(int position, View convertView, ViewGroup parent) 
    {
        try
        {
            row = convertView;
            if (row == null) {
                LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.listitem_discuss, parent, false);
            }

            wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

            coment = getItem(position);

            countryName = (TextView) row.findViewById(R.id.comment);

            countryName.setText(coment.comment);

            TimeLeft = (TextView) row.findViewById(R.id.timeleft);

            int hours,minutes,seconds; 

            if(coment.timeLeft > 0) // When the time is greater than 0
            {
                if(TimeLeft == null)
                {
                    ((ViewGroup) row).addView(TimeLeft);
                }

                                    long t = coment.timeLeft - SecureMessagesActivity.TimeChecker();

                seconds = (int) (t  / 1000) % 60 ;
                minutes = (int) (t / (1000*60)) % 60;
                hours = minutes / 60;

                TimeLeft.setText("hello");
            }
            else
            {
                if(TimeLeft != null)
                {
                    ((ViewGroup)TimeLeft.getParent()).removeView(TimeLeft); 
                }   
            }

            return row;
        }
        catch(Exception e)
        {
            Log.e("Exception: ",e+" Exception occured in View getView(int position, View convertView, ViewGroup parent) of DiscussArrayAdapter.java");
        }
        return row;
    }

    public static void MyMethod(Activity activity,long time)
    {
        try
        {

                if(TimeLeft.getText().equals("hello"))
                {
                    TimeLeft.setText("hi"); 
                }
                else if(TimeLeft.getText().equals("hi"))
                {
                    TimeLeft.setText("hello");  
                }

            Toast.makeText(activity, "Value of TextView is "+TimeLeft.getText() , Toast.LENGTH_SHORT).show();
        }
        catch(Exception e)
        {
            Log.e("Exception: ", e+" Occured in MyMethod() of DiscussArrayAdapter.java");
        }
    }
}

我不知道是什么问题,因为 TextView TimeLeft 仅更改其值一次,在其余情况下它保持不变。请帮助我。提前致谢。

4

1 回答 1

2

我想试试这段代码:-

int delay = 5000; // delay for 5 sec.
    int period = 1000; // repeat every sec.

    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask()
        {
            public void run()
            {
                //your code
            }
        }, delay, period);

提供另一种解决方案

Handler h=new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.setContentView(R.layout.main);
    h.post(new Runnable(){

        @Override
        public void run() {
            // call your function
            h.postDelayed(this,5000);

        }

    });
于 2012-11-03T11:25:17.270 回答