Naoto Ida
Naoto Ida

Reputation: 1295

While displaying ListView, query server using item content

Here is an issue I'm having.

I have an Activity with a ListView,

ArrayList<Server> serverArrayList = new ArrayList<Server>();
List<Server> serverList;
DatabaseHandler db;
ServerAdapter adapter;
ListView listView;

public void loadListView() {
    for (Server server : serverList = db.getAllServers()) {
        serverArrayList.add(server);
    };

    adapter = new ServerAdapter(this, serverArrayList);
    listView = (ListView) findViewById(R.id.serverListView);
    listView.setAdapter(adapter);
}

I need to query a server based on server.url, get information, then append it to an TextViews in the row it originated from.

When should I query the server? Before setting the adapter, or after?

In addition, am I able to do this at all?

Thanks in advance.

Upvotes: 0

Views: 53

Answers (2)

AndroidEx
AndroidEx

Reputation: 15824

I think you can do it both ways.

One way is to send a request to a server in your ServerAdapter in getView() method, where you have references to your textview(s)(/other views). Since server request is usually done asynchronously you will have to register a callback on receiving response that will update your row views with the information you received.

The second option is to send requests after your listview is fully ready. Again using a listener, on receipt of a response from a server you update the corresponding element of the serverArrayList and call adapter.notifyDataSetChanged().

Upvotes: 1

Raj Turakhia
Raj Turakhia

Reputation: 245

Better you have do before setting adapter because you want to display server response in listview so better approach to set adapter after you got response from server.

Upvotes: 1

Related Questions