所以这是我的方法,我试图用线程实现但是当它运行时它一直崩溃,我不知道为什么.. logcat没有显示错误,因为它会发生什么事情崩溃,这让我非常困惑。
所以基本上我试图在这里做的是开始我的活动,当用户正在查看激活时,它开始在后台填充列表,如果这有助于解释我想要完成的事情。
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
populateExerListView(exercises);
}
};
public void loadDataFromDatabase(){
Runnable r = new Runnable() {
@Override
public void run() {
exercises = dbh.getAllExercises(bodypart_chosen);
handler.sendEmptyMessage(0);
}
};
public void populateExerListView(final ArrayList<AdapterExercisesList.Exercise> exercises){
ListAdapter edsAdapter = new AdapterExercisesList(this, exercises);
exerciseListView = (ListView) findViewById(R.id.exerciseListView);
exerciseListView.setAdapter(edsAdapter);
exerciseListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int pos = (Integer) view.getTag();
//gets the bodypart by passing in the pos
String exercise_chosen = exercises.get(pos).get_exerciseName();
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = df.format(c.getTime());
exerciseClicked(exercise_chosen, formattedDate);
}
}
);
}
答案 0 :(得分:0)
你定义了runnable,但你从未将它传递给线程,
你需要改变这个:
public void loadDataFromDatabase(){
Runnable r = new Runnable() {
@Override
public void run() {
exercises = dbh.getAllExercises(bodypart_chosen);
handler.sendEmptyMessage(0);
}
};
到此:
public void loadDataFromDatabase(){
new Thread(new Runnable() {
@Override
public void run() {
exercises = dbh.getAllExercises(bodypart_chosen);
handler.sendEmptyMessage(0);
}
}).start();
}//end method