Kennard Stewart
Kennard Stewart

Reputation: 1

ListView displays arraylist data twice

Can anyone help me with figuring out why my listview repeats the results it gets from the database. I am using a Listview with Checkbox. Here is my code below.

public class AccessLevels extends Fragment {

MyAdminAdapter adminAdapter = null;
Guards guards;

ListView listView;

public ArrayList<Guards> guardsName;

public String str_name;
public boolean isAdmin;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_access_levels, container, false);

    listView = (ListView)view.findViewById(R.id.list_of_guards);

    return view;
}

private class MyAdminAdapter extends ArrayAdapter<Guards>{

    private ArrayList<Guards> objectsList;

    public MyAdminAdapter(Context context, int resource, ArrayList<Guards> objectsList) {
        super(context, resource, objectsList);
        this.objectsList = objectsList;
        this.objectsList.addAll(objectsList);
    }

    private class ViewHolder{
        CheckBox chk_name;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        if (convertView == null){
            LayoutInflater vi = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.single_row_access, null);

            holder = new ViewHolder();
            holder.chk_name = (CheckBox) convertView.findViewById(R.id.chk_guard);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        final Guards guards = objectsList.get(position);
        holder.chk_name.setText(guards.getName());
        holder.chk_name.setChecked(guards.isChecked());
        holder.chk_name.setTag(guards);

        holder.chk_name.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                CheckBox cb = (CheckBox) v;
                Guards guards = (Guards) cb.getTag();
                guards.setChecked(cb.isChecked());
            }
        });

        return convertView;
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
}

@Override
public void onDetach() {
    super.onDetach();
}
}

I get the data an store it like this.

private void DisplayAllNames() {

    guardsName = new ArrayList<Guards>();

    ParseQuery<ParseObject> guardsQuery = ParseQuery.getQuery("Guards");
    guardsQuery.whereExists("Name");
    guardsQuery.orderByAscending("Name");
    guardsQuery.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> list, ParseException e) {
            if (e == null) {
                if (list.size() > 0) {
                    for (int i = 0; i < list.size(); i++) {
                        ParseObject data = list.get(i);
                        str_name = data.getString("Name");
                        isAdmin = data.getBoolean("admin");

                        guards = new Guards(str_name, isAdmin);
                        guardsName.add(guards);
                    }

                    adminAdapter = new MyAdminAdapter(getActivity(), R.layout.single_row_access, guardsName);
                    listView.setAdapter(adminAdapter);
                } else {
                }
            } else {
            }
        }
    });
}

Thanks in advance.

Upvotes: 0

Views: 112

Answers (2)

Floern
Floern

Reputation: 33904

In your MyAdminAdapter you are adding the data twice:

    this.objectsList = objectsList;
    this.objectsList.addAll(objectsList);

You just have to remove the second line.

Upvotes: 1

Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17132

Remove this line from your MyAdminAdapter's constructor:

this.objectsList.addAll(objectsList);

You are already adding your items in the previous instruction:

this.objectsList = objectsList;

Upvotes: 1

Related Questions