vendredi 26 février 2016

How to access the check box displayed using adapter in the activity containing list view

I am working on the following screen:

New Group screen

The friends list is displayed by populating listview using Base Adapter.

Adapter

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

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

        final Bean_Friends 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);

        chkFriends.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                Log.e("Checkboxxxxxxxxxx", "Clicked");
                if (isChecked) {
                    bean_friends.setFriendSelected("true");
                    Log.e("Checkbox", "Checked");
                } else {
                    bean_friends.setFriendSelected("false");
                    Log.e("Checkbox", "UnChecked");
                }
                listBeanFriends.add(bean_friends);
            }
        });
        return view;
    }
}

When the check box is clicked,I am saving a string value as true in the Bean.

Bean

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

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

Now inside the activity containing list view,In want to access the id of the friend whose check box is checked on clicking single tick on the Top Toolbar.

Following method is used on clicking single tick:

public void createNewGroup() {
    Toast.makeText(NewGroupActivity.this, "clicked", Toast.LENGTH_SHORT).show();
    listBeanFriends = new ArrayList<>();
    for (int i = 0; i < listBeanFriends.size(); i++) {
        Log.e("Loop Working", "-------------");
        Bean_Friends bean_friends = listBeanFriends.get(i);
        String friendSelected = bean_friends.getFriendSelected();
        String friendID = bean_friends.getFriendsID();
        Log.e("FriendID", friendID);
        if (friendSelected.equals("true")) {
            Toast.makeText(NewGroupActivity.this, friendID, Toast.LENGTH_SHORT).show();
        } else {
            // Toast.makeText(NewGroupActivity.this,"true",Toast.LENGTH_SHORT).show();
        }
    }
}

But this is mot working for me .I want to access the id of friends whose check box is checked on clicking single tick.Please help me to fix the issue.




Aucun commentaire:

Enregistrer un commentaire