vendredi 26 février 2016

Check Box selected is displayed wrongly

I am working on a screen where i am populating a list view using base adapter.Each row of list view contains a circular image view,Text View and Check box .On clicking single tick on toolbar ,i am displaying the id of user corresponding to checked button But it is displayed wrongly.I am implementing the following screen:

Screenshot

1.Bean_Friends

    public class Bean_Friends {
    private String name, url, extension, friendsID;
    private String friendSelected = "false";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getExtension() {
        return extension;
    }

    public void setExtension(String extension) {
        this.extension = extension;
    }

    public String getFriendsID() {
        return friendsID;
    }

    public void setFriendsID(String friendsID) {
        this.friendsID = friendsID;
    }

    public String getFriendSelected() {
        return friendSelected;
    }

    public void setFriendSelected(String friendSelected) {
        this.friendSelected = friendSelected;
    }
}

2.Adapter_Friends_Group.java

    public class Adapter_Friends_Group extends BaseAdapter {
    private Context context;
    public List<Bean_Friends> listBeanFriends;
    private LayoutInflater inflater;
    private ApiConfiguration apiConfiguration;
    private Bean_Friends bean_friends;


    public Adapter_Friends_Group(Context context, List<Bean_Friends> listBeanFriends) {
        this.context = context;
        this.listBeanFriends = listBeanFriends;
    }

    @Override
    public int getCount() {
        return listBeanFriends.size();
    }

    @Override
    public Object getItem(int i) {
        return listBeanFriends.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (inflater == null) {
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        if (view == null) {
            view = inflater.inflate(R.layout.feed_item_friends, null);
        }

        //finding different views
        ImageView pic = (ImageView) view.findViewById(R.id.friendsImage);
        TextView txtName = (TextView) view.findViewById(R.id.nameFriends);
        CheckBox chkFriends = (CheckBox) view.findViewById(R.id.chkFriends);

        bean_friends = listBeanFriends.get(i);
        String name = bean_friends.getName();
        String url = bean_friends.getUrl();
        String extension = bean_friends.getExtension();
        apiConfiguration = new ApiConfiguration();
        String api = apiConfiguration.getApi();
        String absolute_url = api + "/" + url + "." + extension;

        //loading image into ImageView                                                                                                                                            iew
        Picasso.with(context).load(absolute_url).error(R.drawable.default_avatar).into(pic);

        //Setting name in the textview
        txtName.setText(name);

        //If check box is checked,then add true to bean else add false to bean
        if (chkFriends.isChecked()) {
            bean_friends.setFriendSelected("true");
        } else {
            bean_friends.setFriendSelected("false");
        }


        chkFriends.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CheckBox cb = (CheckBox) view;
                if (cb.isChecked()) {
                    bean_friends.setFriendSelected("true");
                    Toast.makeText(context, "Check Box is checked : " + cb.isChecked(), Toast.LENGTH_SHORT).show();
                } else {
                    bean_friends.setFriendSelected("false");
                }
            }
        });


        return view;
    }
}

3. Code of Activity containing view

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.clear();
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu_new_group, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.createGroup:
            createNewGroup();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}


public void createNewGroup() {
    Toast.makeText(NewGroupActivity.this, "clicked", Toast.LENGTH_SHORT).show();
    List<Bean_Friends> listBeanFriends = new ArrayList<>();
    //Log.e("Size of listbeanFriends", String.valueOf(listBeanFriends.size()));

    listBeanFriends = adapter_friends_group.listBeanFriends;
    //  Log.e("Size of adapter_friends", String.valueOf(adapter_friends_group.listBeanFriends.size()));
    Log.e("Size of listbeanFriends", String.valueOf(listBeanFriends.size()));
    for (int i = 0; i < listBeanFriends.size(); i++) {
        Log.e("Loop Working", String.valueOf(i));
        Bean_Friends bean_friends = listBeanFriends.get(i);
        String friendID = bean_friends.getFriendsID();
        String friendSelected = bean_friends.getFriendSelected();
        String friendName = bean_friends.getName();
        Log.e("FriendsName", friendName);
        Log.e("FriendID", friendID);
        Log.e("friendSelected", friendSelected);
        if (friendSelected.equals("true")) {
            Toast.makeText(NewGroupActivity.this, friendID, Toast.LENGTH_SHORT).show();
        } else {
            // Toast.makeText(NewGroupActivity.this,"true",Toast.LENGTH_SHORT).show();
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire