将数据附加到列表

时间:2015-03-31 05:38:05

标签: android list android-listview

我在建议列表中显示来自两个不同API调用的数据,所以我在onQueryTextChange()内调用两个API。因为我从两个不同的api调用得到响应,我使用addAll()来追加来自不同api的响应。但是没有添加数据,列表的大小为零。但是如果我使用searchResponses= (List<UserVideo>) response;,那么数据会被添加到列表中但是不会附加两个不同的响应。我哪里出错?

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

根据DavidJohns的回答更新 -

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

1 个答案:

答案 0 :(得分:0)

分开调用您的Web服务,并将这些结果放在两个不同的列表中, 然后将这些列表值复制到searchResponses中。

如果结果类型相同,您可以按如下方式合并结果,

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