0

I have been racking my brains trying to make this work however my app keep crashing when I to attempt populating it... Here is what I have so far... Whats worse is there does not seem to be a logcat error when it crashes... I have the filters set to "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);
                }
            }
    );
}
Edward Lim
  • 763
  • 9
  • 31

1 Answers1

1

No no no. If I understand clearly what you ask, you cannot update anything on UI from another thread. At least you have to use runOnUIThread().

Nabin
  • 11,216
  • 8
  • 63
  • 98
  • could you please elaborate on that, im quite new to threads and stuff.. I was doing some research on this because when i load my app it says its skipping frames and suggested I use threads – Edward Lim Aug 10 '15 at 07:17