I'm new to Android development and I was needed to quickly develop simple app. I need to dynamically generate list of views
and and use timer in each of them, so my current code looks like this:
public class MainActivity extends AppCompatActivity {
private List<TextView> times = new ArrayList<>();
private Handler handler;
private void initWatchlist() {
LinearLayout line = new LinearLayout(this);
line.setOrientation(LinearLayout.HORIZONTAL);
TextView time = new TextView(this);
time.setText(sdf.format(new Date()));
time.setTextSize(32);
line.addView(time);
times.add(time);
//add couple of other views to line
//...
LinearLayout watchlist = findViewById(R.id.watchlist);
watchlist.addView(line);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initWatchlist();
Thread thread = new Thread(){
private Calendar previousDate = Calendar.getInstance();
public void run(){
if (previousDate.get(Calendar.MINUTE) != Calendar.getInstance().get(Calendar.MINUTE)) {
for (TextView textView : times) {
textView.setText(sdf.format(new Date()));
}
}
}
};
handler = new Handler();
handler.post(thread);
}
//other code
}
So the problem is that it seems like my Thread
doesn't do anything actually. I don't see my textviews
update time. What am I doing wrong?