2016-02-27 159 views
0

我正在使用基礎適配器填充列表視圖的屏幕上。列表視圖的每一行都包含圓形圖像視圖,文本視圖和複選框。單擊工具欄上的單個勾號,我正在顯示的用戶ID相對應的檢查按鈕,但它顯示wrongly.I正在執行以下屏幕:複選框被選中顯示錯誤

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.含有活動視圖的代碼

@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(); 
} 
+0

什麼定義錯誤?錯誤的位置,而不是通知,當您預期檢查時檢查或未檢查時檢查? – MikeT

+0

例如,當我選中對應於人員A的複選框時,它顯示人員C的ID。我認爲我在適配器內部發生了一些錯誤。 –

回答

2

我認爲錯誤是在這一行: -

//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來設置'setFriendSelected'。在列表中查看視圖被重用,如果說你已經選擇了'A'作爲朋友,然後你向下滾動,那麼'A'的視圖可能在'C'中被重用,並且現在這個代碼'C'將被檢查爲朋友。在這裏你反而想根據'C'是否是你的朋友來充氣你的複選框視圖。試試這個:

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

P.S:你可以在這裏使用視圖保持模式來獲得更好的性能。