如何在另一个线程中填充我的 ListViews?

How to populate my ListViews in another thread?

我一直在绞尽脑汁试图让它工作但是当我尝试填充它时我的应用程序总是崩溃......这是我到目前为止所拥有的......更糟糕的是似乎没有logcat 崩溃时出错...我将过滤器设置为 "no filters"。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_bodypart_clicked);

    initViews();
    handleIntentData();

    loadDataFromDatabase();
}

Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        populateExerListView(exercises);
    }
};

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This method calls another method in the MyDBHandler class that
returns all corresponding exercises that are linked to the String
"bodypart_chosen"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
public void loadDataFromDatabase(){

    Runnable r = new Runnable() {
        @Override
        //Message message = Message.obtain();
        public void run() {
            exercises = dbh.getAllExercises(bodypart_chosen);
            handler.sendEmptyMessage(0);
        }
    };
    Thread edsThread = new Thread(r);
    edsThread.start();

    //exercises = dbh.getAllExercises(bodypart_chosen);
    //populateExerListView(exercises);
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This method populates the exerciseListView with the
custom list view from adapter_exercise_list.xml. It gets an
array of EXERCISES in the params.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
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();
                    //System.out.println("Current time =&gt; "+c.getTime());

                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                    String formattedDate = df.format(c.getTime());
                    // Now formattedDate have current date/time
                    Log.e("CHECK ME", formattedDate);

                    exerciseClicked(exercise_chosen, formattedDate);
                }
            }
    );
}

不不不。如果我清楚地理解你的要求,你就不能从另一个线程更新 UI 上的任何内容。至少你得用runOnUIThread().