2015-08-10 1 views
0

Я пытаюсь изо всех сил пытаться сделать эту работу, но мое приложение продолжает терпеть крах, когда я пытаюсь заселить его ... Вот что я до сих пор ... Что еще хуже, похоже, что ошибка logcat, он падает ... У меня есть фильтры, настроенные на «без фильтров».Как заполнить ListViews в другом потоке?

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); 
       } 
      } 
    ); 
} 

ответ

1

Нет, нет. Если я четко понимаю, что вы просите, вы ничего не можете обновить в пользовательском интерфейсе из другого потока. По крайней мере, вы должны использовать runOnUIThread().

+0

Не могли бы вы подробно рассказать об этом, я совершенно новый для потоков и т. Д. Я делал некоторые исследования по этому поводу, потому что, когда я загружаю свое приложение, он говорит о своих пропущенных кадрах и предложил использовать потоки –

+0

Проверьте http: // stackoverflow. com/questions/11140285/how-to-use-runonuithread – Nabin

+1

СПАСИБО СМОТРЕТЬ! абсолютно работает! –