0
我正在使用基礎適配器填充列表視圖的屏幕上。列表視圖的每一行都包含圓形圖像視圖,文本視圖和複選框。單擊工具欄上的單個勾號,我正在顯示的用戶ID相對應的檢查按鈕,但它顯示wrongly.I正在執行以下屏幕:複選框被選中顯示錯誤
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.含有活動視圖的代碼
@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();
}
}
}
更新代碼:
解決做以下修改後的問題:
適配器
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();
}
什麼定義錯誤?錯誤的位置,而不是通知,當您預期檢查時檢查或未檢查時檢查? – MikeT
例如,當我選中對應於人員A的複選框時,它顯示人員C的ID。我認爲我在適配器內部發生了一些錯誤。 –