jeudi 8 juin 2017

Spinner with Checkbox cannot get selected item in activity

I created a custom Spinner with an ImageView, TextView and Checkbox.

I defined a custom Spinner item :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/iv_profile_pic"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:src="@drawable/profile"/>
    <TextView
        android:id="@+id/tv_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_centerInParent="true"
        android:text="Username"/>
    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        />
</RelativeLayout>

I have a model class that contains ImageView, TextView and Checkbox values :

public class UserAllowed {
    private String username;
    private ParseFile pic;
    private Boolean checked;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public ParseFile getPic() {
        return pic;
    }

    public void setPic(ParseFile pic) {
        this.pic = pic;
    }

    public Boolean getChecked() {
        return checked;
    }

    public void setChecked(Boolean checked) {
        this.checked = checked;
    }


}

Here my custom adapter :

private ArrayList<UserAllowed> userAllowedList;
    private SpinnerAdapter spinnerAdapter;
    int position;


    public SpinnerAdapter(Context context, int resource, List<UserAllowed> objects) {
        this.context = context;
        this.userAllowedList = (ArrayList<UserAllowed>) objects;
        this.spinnerAdapter = this;
        inflter = (LayoutInflater.from(context));
    }


    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        view = inflter.inflate(R.layout.user_row, null);

        final CheckBox checkBox = (CheckBox)view.findViewById(R.id.checkbox);

        names.setText(userAllowedList.get(i).getUsername());



        checkBox.setTag(i);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                position = (Integer) buttonView.getTag();
                Log.d("user checked",position+"");
                userAllowedList.get(i).setChecked(isChecked);

            }
        });

        return view;
    }

Here's how i set my adapter in activity :

  ParseQuery<ParseUser> userQuery = ParseUser.getQuery();
        userQuery.whereWithinKilometers("location",ParseUser.getCurrentUser().getParseGeoPoint("location"),10);
        userQuery.whereNotEqualTo("user",ParseUser.getCurrentUser());
        userQuery.findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(final List<ParseUser> objects, ParseException e) {

                if (e == null) {
                    for (int i = 0; i < objects.size(); i++) {
                        UserAllowed userAllowed = new UserAllowed();

                        userAllowed.setUsername(objects.get(i).getUsername());
                        userAllowed.setPic(objects.get(i).getParseFile("image"));
                        userAllowed.setChecked(userAllowed.getChecked());

                        allowed.add(userAllowed);
                    }

                    final SpinnerAdapter spinnerAdapter=new SpinnerAdapter(getApplicationContext(),0,allowed);
                    spUsers.setAdapter(spinnerAdapter);
                    /* for(int i = 0;i<spinnerAdapter.getCount();i++ ) {
                                UserAllowed userAllowed = new UserAllowed();
                                userAllowed.setChecked(userAllowed.getChecked());
                                userAllowed.setUsername(objects.get(i).getUsername());
                                allowed.add(userAllowed);

                            }*/

                } else {
                    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();

                }
            }
        });
    }

**OUTPUT : ** enter image description here

I want to get the username of checked item in my activity : userAllowed.setChecked(userAllowed.getChecked());




Aucun commentaire:

Enregistrer un commentaire