0

Ok so i tried to learn of all These other questions here, but i am not getting the hang of it, so i decided to ask by myself. I got a Main Activity with different Fragments for changing the views (used the Android Standard sidebar activity template). From there the user creates Tasks via a separate Acitivty which Returns the values. In the Main Activity the Tasks are getting stored into the SQLite-database. So far it works.

Now i have the Fragment TaskList with the corresponding layout TaskList_main.xml. In this XML file i got a simple ListView which i want to fill with the values from the database. My Problem is where to write that method and when to Access it. the method would be

public void showAllListEntries() {

    List<TaskData> TaskList = datasource.getAllTasks();
    //Daten werden im ArrayAdapter gespeichert
    ArrayAdapter<TaskData> TaskListArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, TaskList);

    ListView TaskDataListView = (ListView) findViewById(R.id.TaskListView);
    TaskDataListView.setAdapter(TaskListArrayAdapter);
}

My Fragment is empty like this atm

public class TaskList extends Fragment {
View view;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.activity_main_tasklist, container, false);

    return view;
}
}

I can also send the main Activity if you like but it's a bit messy

So can someone tell me how i get this to work? And did i explain my Problem clearly?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • My Android is a bit rusty, but if I understand you correctly, the most logical place to put the `showAllListEntries()` method would be in the `TaskList` fragment... – Magnus Nov 27 '17 at 16:30
  • then id have to create my datasource there, so far no prob, but i cant get a reference to my listview from the Fragment, cuz the Fragment doesn't know the "findViewById" method – ein siedler Nov 27 '17 at 16:42
  • You can get the reference to the `ListView` in `onViewCreated` - see https://stackoverflow.com/questions/6495898/findviewbyid-in-fragment . For the datasource - you could either pass its reference to the `Fragment` from your `Activity` or use a singleton. – Magnus Nov 27 '17 at 17:54
  • Now what's a singleton? – ein siedler Nov 27 '17 at 18:23

1 Answers1

0

You can do it in onCreateView. But you should make an async call to get the tasks to display and have your fragment as a listener. When you get the tasks you can create your adapter and attach it to the ListView.

And you should have a ProgressBar in TaskList_main.xml (which should be renamed to task_list_fragment.xml, I don't think there is a naming convention for layouts, but this is quite used) and hide it when you receive the data.

andrei
  • 2,934
  • 2
  • 23
  • 36
  • What is a loader? How to integrate that? And what do you mean by async calls and Fragment as listener? Sorry for that many questions – ein siedler Nov 27 '17 at 16:39
  • @einsiedler sorry, by loader I mean a ProgressBar. I'll edit the answer. And I'll add a link for the async part – andrei Nov 27 '17 at 16:56