由于arraylist或arrayadapter在listview中获得重复的结果?

时间:2013-11-18 15:18:36

标签: java android json facebook

我遇到了一些问题。每次我运行我的应用程序时,它都会给我重复的结果。 这是我的代码:

首先我得到了OnCreateView:

@Override
    public View onCreateView(LayoutInflater inflater, 
            ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.event, 
                container, false);
        // Check for an open session
        Session session = Session.getActiveSession();
        if (session != null && session.isOpened()) {
            // Get the user's data
        ShowEventsFromFQL();

        }
        return view;
    }

您可以看到包含方法ShowEventsDFromFQL(); :

public void ShowEventsFromFQL(){
         // Check for an open session
        Session session = Session.getActiveSession();
        if (session != null && session.isOpened()) {
            // Get the user's data

            String fqlQuery = "SOME QUERY";
            Bundle params = new Bundle();
            params.putString("q", fqlQuery);
            Session session2 = Session.getActiveSession();
            Request request = new Request(session2,
                "/fql",                         
                params,                         
                HttpMethod.GET,                 
                new Request.Callback(){         
                    public void onCompleted(Response response) {
                        Log.i(TAG, "Result: " + response.toString());
                        parseEventFromFQLResponse(response);
                    }                  
            }); 
            Request.executeBatchAsync(request);
    }         
    }

我得到的查询是正确的。我在我的日志中检查了它,我得到了应该给予的所有内容。

parseEventFromFQLResponse(响应):

public void parseEventFromFQLResponse( Response response )
    {
        try
        {
            GraphObject graphObj  = response.getGraphObject();
            JSONObject  jsonObj = graphObj.getInnerJSONObject();
            JSONArray   jsonArr = jsonObj.getJSONArray( "data" );

            for ( int i = 0; i < ( jsonArr.length() ); i++ )
            {
                JSONObject jObject = jsonArr.getJSONObject( i );

                  SOME STRING ALL CORRECT

                  dataEvent.add(new EventDetails(THE STRINGS));


            }
            if(dataEvent != null){

                ListView lvEvents = (ListView) getView().findViewById(R.id.lvEvent);

                ListEventAdapter eventAdapter = new ListEventAdapter(getActivity(), R.layout.event_item, dataEvent);

                lvEvents.setAdapter(eventAdapter);

               }
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }

它给出了5个结果然后它给了我结果nr 1然后2然后3然后返回结果nr1结果nr 2 ..

1 个答案:

答案 0 :(得分:0)

您可以将列表项添加到hashset并检索它们。它将删除其中的所有重复值。

ArrayList al = new ArrayList();
// add elements to al, including duplicates
HashSet<EventDetails> hs = new HashSet<EventDetails>();
hs.addAll(al);
al.clear();
al.addAll(hs);