2016-06-23 2 views
0

목록보기에 확인란이 있으면 선택된 확인란이 사라지고 임의의 확인란이 선택됩니다. 목록보기에 확인란이 있으면 스크롤 할 때 선택된 확인란이 선택 취소되고 무작위 확인란이 선택됩니다. 이 목록보기를 스크롤하고 있습니다. 확인란을 선택하지 않았습니다. 체크 박스는목록보기에서 확인란을 선택하면 선택된 확인란이 사라지고 임의의 확인란이 선택됩니다.

public class FriendList extends ArrayAdapter<FriendListResult> { 
    private List<FriendListResult> list; 
    LayoutInflater layoutInflater; 
    public int resource; 
    private Holder holder = null; 

    //Constructor 
    public FriendList(Context context, int resource, List<FriendListResult> objects) { 
    super(context, resource, objects); 
    this.list = objects; 
    this.resource = resource; 
     // Cache the LayoutInflate to avoid asking for a new one each time. 
    this.layoutInflater = LayoutInflater.from(context); 
    } 

// getview method 
@Override 
public View getView(final int position, View view, ViewGroup parent) { 
// check if view is null or not 
// create new row view 
    if (view == null) { 
// inflating layout 
     view = layoutInflater.inflate(resource, null); 
// initializing holder 
     holder = new Holder(); 
     holder.txtTitle = (TextView) view.findViewById(R.id.tvContactNameMain); 
    // checkbox defined 
     holder.checkBox = (CheckBox) view.findViewById(R.id.chkboxFriendListItem); 
     view.setTag(holder); 
    } 
    else { 
     holder = (Holder) view.getTag(); 
    } 
    // set value from the list 
    final FriendListResult friendListResult = list.get(position); 
    // get and set name 
    holder.txtTitle.setText(friendListResult.getNickname()); 

// checkbox changeListener 
holder.checkBox.setOnCheckedChangeListener(new >CompoundButton.OnCheckedChangeListener() { 
@Override 
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
// Maximum three selection for checkbox 
    { 
    if (checkArrayList.size() >= 3 && isChecked) { 
     buttonView.setChecked(false); 
Toast.makeText(getContext(), "You have already chosen 3 friends", 
     Toast.LENGTH_LONG).show(); 
    } else if (isChecked) { 
     // storing position id to arraylist 
     checkArrayList.add(getItem(position).getId()); 
     notifyDataSetChanged(); 
     // userCheckCount to get the count 
     userCheckCount++; 
    } else if (!isChecked) {  
    //removing position from arraylist 
     checkArrayList.remove(getItem(position).getId()); 
     notifyDataSetChanged(); 
     userCheckCount--; 
    }  
    } 
}); 
    return view; 
} 
// holder class initializing widgets 
    class Holder { 
    private TextView txtTitle; 
    private CheckBox checkBox; 
    } 
} 
+0

I 사용 권장 [RecyclerView (https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html)을 제공하는 이 문제를 다루는 기본 메커니즘 –

답변

관련 문제