问题描述:

I am displaying data from two different API calls inside suggestion list so I'm calling two API's inside onQueryTextChange() .Since I am getting response from two different api calls i am using addAll() to append response from different apis.But data is not added and size of list is zero.But if i use searchResponses= (List<UserVideo>) response; then data is added to list but two different responses is not appended.Where am i going wrong?

 @Override

public boolean onQueryTextChange(String s)

{

if(s.length()>=1) {

String mStart = "";

int mCount = 10;

String[] columnNames = {"_id", "name", "userImage", "location"};

final String[] temp = new String[4];

final MatrixCursor cursor = new MatrixCursor(columnNames);

SearchAPI.getSearchFeed(getApplicationContext(), s, mStart, mCount, mSettingsManager.getInstance().getAccessToken(), new APIResponseListener() {

@Override

public void onResponse(Object response) {

searchResponses.addAll((List<UserVideo>) response);

for (UserVideo searchResponse :searchResponses)

{searchResponse.setIsVideo(true);

temp[0] = Long.toString(id++);

temp[1] = searchResponse.getCaption();

temp[3] = searchResponse.getLocation();

cursor.addRow(temp);

}

mSearchView.setSuggestionsAdapter(new SearchListAdapter(HomeActivity.this, cursor));

}

@Override

public void onError(VolleyError error) {

if (error instanceof NoConnectionError) {

}

}

});

SearchAPI.getSearchUser(getApplicationContext(), s,mSettingsManager.getInstance().getAccessToken(), new APIResponseListener() {

@Override

public void onResponse(Object response)

{

searchResponses.addAll((List<UserVideo>) response);

for (UserVideo searchResponse :searchResponses) {

searchResponse.setIsVideo(false);

temp[0] = Long.toString(id++);

temp[1] = searchResponse.getUserName2();

temp[3] = searchResponse.getName();

cursor.addRow(temp);

}

mSearchView.setSuggestionsAdapter(new SearchListAdapter(HomeActivity.this, cursor));

}

@Override

public void onError(VolleyError error)

{

if (error instanceof NoConnectionError) {

}

}

});

}

return true;

}

Updated according to DavidJohns answer-

 public boolean onQueryTextChange(String s)

{

if(s.length()>=0) {

String mStart = "";

int mCount = 10;

String[] columnNames = {"_id", "name", "userImage", "location"};

final String[] temp = new String[4];

final MatrixCursor cursor = new MatrixCursor(columnNames);

SearchAPI.getSearchFeed(getApplicationContext(), s, mStart, mCount, mSettingsManager.getInstance().getAccessToken(), new APIResponseListener() {

@Override

public void onResponse(Object response) {

searchResponses= (List<UserVideo>) response;

for (UserVideo searchResponse :searchResponses)

{

searchResponse.setIsVideo(true);

temp[0] = Long.toString(id++);

temp[1] = searchResponse.getCaption();

temp[2] = searchResponse.getUserId();

temp[3] = searchResponse.getLocation();

cursor.addRow(temp);

}

mSearchView.setSuggestionsAdapter(new SearchListAdapter(HomeActivity.this, cursor));

}

@Override

public void onError(VolleyError error) {

if (error instanceof NoConnectionError) {

}

}

});

SearchAPI.getSearchUser(getApplicationContext(), s,mSettingsManager.getInstance().getAccessToken(), new APIResponseListener() {

@Override

public void onResponse(Object response)

{

searchUserResponses= (List<UserVideo>) response;

for (UserVideo searchResponse :searchUserResponses) {

searchResponse.setIsVideo(false);

temp[0] = Long.toString(id++);

temp[1] = searchResponse.getUserName2();

temp[2] = searchResponse.getUserId();

temp[3] = searchResponse.getName();

searchResponses.add(searchResponse);

cursor.addRow(temp);

}

mSearchView.setSuggestionsAdapter(new SearchListAdapter(HomeActivity.this, cursor));

}

@Override

public void onError(VolleyError error)

{

if (error instanceof NoConnectionError) {

}

}

});

}

return true;

}

@Override

public boolean onSuggestionClick(int i)

{

if((searchResponses!=null)&&(searchResponses.get(i).getIsVideo()))

{

UserVideo searchResponse = searchResponses.get(i);

setSearchVideoToShow(searchResponse);

switchFragment(HomeActivity.FRAGMENT_VIDEO_SEARCH, false, "Video Details");

}

else if((searchResponses!=null)&&(!searchResponses.get(i).getIsVideo()))

{

mSingleClickHandle.put(ENABLE_FRAGMENT_SEARCH_VIEW, false);

setUserIdToShow(searchResponses.get(i).getUserId2());

closeDrawer();

switchFragment(HomeActivity.FRAGMENT_PROFILE_VIEW, false, "video");

}

return true;

}

网友答案:

Call your web services separatly and locate those results in two different lists, then copy those list values into searchResponses.

you can merge the results as follows if their type is equal,

for(int i = 0; i < searchFeedList.size() ; i ++)
{
    searchResponses.add(searchFeedList.get(i);
}

int totalSoze = searchFeedList.size() + searchUserList.size();

for(int j = searchFeedList.size(); j < totalSoze.size() ; j ++)
{
    searchResponses.add(searchUserList.get(j);
}
相关阅读:
Top