我正在开发以下屏幕:
通过使用Base Adapter填充listview来显示好友列表。
适配器
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;
}
}
单击复选框时,我将在Bean中将字符串值保存为true。
豆
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;
}
}
现在在包含列表视图的活动中,在“想要访问顶部工具栏上单击单个勾选项时勾选了复选框的朋友的ID。”
点击单一刻度时使用以下方法:
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();
}
}
}
但这对我有用。我想访问点击单勾选中复选框的朋友的ID。请帮我解决问题。
更新代码:
我做了以下更改。现在它正在为我工作。
适配器
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;
}
private class ViewHolder {
ImageView imageView;
TextView txtName;
CheckBox chkFriend;
}
@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) {
ViewHolder viewHolder = null;
if (inflater == null) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (view == null) {
view = inflater.inflate(R.layout.feed_item_friends, null);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView) view.findViewById(R.id.friendsImage);
viewHolder.txtName = (TextView) view.findViewById(R.id.nameFriends);
viewHolder.chkFriend = (CheckBox) view.findViewById(R.id.chkFriends);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
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(viewHolder.imageView);
//Setting name in the textview
viewHolder.txtName.setText(name);
//Setting boolean isSelected if the Checkbox is checked
viewHolder.chkFriend.setChecked(bean_friends.isSelected());
viewHolder.chkFriend.setTag(bean_friends);
viewHolder.chkFriend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CheckBox cb = (CheckBox) view;
Bean_Friends bean_friends = (Bean_Friends) cb.getTag();
Toast.makeText(context, "Clicked on Checkbox: " + bean_friends.getName() + " is " + cb.isChecked(), Toast.LENGTH_LONG).show();
bean_friends.setIsSelected(cb.isChecked());
}
});
return view;
}
}
豆
public class Bean_Friends {
private String name, url, extension, friendsID;
boolean isSelected;
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 boolean isSelected() {
return isSelected;
}
public void setIsSelected(boolean isSelected) {
this.isSelected = isSelected;
}
}
活动内的方法
public void createNewGroup() {
StringBuffer responseText = new StringBuffer();
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 friendName = bean_friends.getName();
Log.e("FriendsName", friendName);
Log.e("FriendID", friendID);
Log.e("FriendSelected", String.valueOf(bean_friends.isSelected()));
if (bean_friends.isSelected()) {
responseText.append("\n" + bean_friends.getName() + " " + bean_friends.getFriendsID());
}
}
Toast.makeText(NewGroupActivity.this, responseText, Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:1)
尝试替换此行
listBeanFriends = new ArrayList<>();
使用:
List<Bean_Friends> listBeanFriends = new ArrayList<>();
listBeanFriends = adapter.listBeanFriends;
答案 1 :(得分:1)