我希望每1分钟后更改一次textview。问题是它只是第一次更改,在其他情况下保持不变。这就是我使用每1分钟调用一次的runnable:
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只改变了它的值一次,在剩下的情况下它保持不变。请帮助我。谢谢。
答案 0 :(得分: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);
}
});