0

어댑터가있는 사용자 정의 목록보기로 활동합니다. 확인란을 선택하여 많은 이름을 선택합니다. OK 버튼을 누르면 이전 값으로 돌아갑니다. 활동을 다시 열면 이미 선택된 값 확인란이 다시 선택되지 않습니다. 이 문제를 해결하는 방법? 코드활동을 다시 열 때 선택란 값이있는 사용자 정의 어댑터가 재확인되지 않습니다.

public class MultiselectAdapter extends ArrayAdapter<String> implements CompoundButton.OnCheckedChangeListener{ 

private final LayoutInflater mInflater; 
private final Context mContext; 
private final List<TeamModel> items = null; 
private final int mResource; 
SparseBooleanArray mCheckStates; 
public String[] teamIDValues; 

ArrayList<Object> ListObject; 

public MultiselectAdapter(@NonNull Context context, @LayoutRes int resource, 
          @NonNull ArrayList objects){ 
    super(context, resource, 0, objects); 

    mContext = context; 
    mInflater = LayoutInflater.from(context); 
    mResource = resource; 
    ListObject = objects; 

    mCheckStates = new SparseBooleanArray(ListObject.size()); 
    System.out.println("teh size vaooo --"+mCheckStates.size()); 
    System.out.println("teh objects vaooo --"+objects.size()); 
} 

@Override 
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { 

    mCheckStates.put((Integer) compoundButton.getTag(), isChecked); 
} 

static class ViewHolder { 
    protected TextView name; 
    protected TextView id; 
    protected CheckBox check; 

} 

@Override 
public @NonNull View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { 

    ViewHolder holder; 
    if (convertView == null) { 
     holder = new ViewHolder(); 
     convertView = mInflater.inflate(mResource, null); 
     holder.name = (TextView) convertView.findViewById(R.id.name); 
     holder.id = (TextView) convertView.findViewById(R.id.idval); 
     holder.check = (CheckBox) convertView.findViewById(R.id.choseboxes); 

     convertView.setTag(holder); 
     convertView.setTag(R.id.name, holder.name); 
     convertView.setTag(R.id.idval, holder.id); 
     convertView.setTag(R.id.choseboxes, holder.check); 

    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 
    holder.check.setTag(position); 


    holder.check.setChecked(mCheckStates.get(position, false)); 
    holder.check.setOnCheckedChangeListener(this); 

    if(ListObject.get(position) instanceof TeamModel.Data) 
    { 
    // System.out.println("teh valus are--"+((TeamModel.Data) ListObject.get(position)).getDescription()); 
     holder.name.setText(((TeamModel.Data) ListObject.get(position)).getDescription()); 
     holder.id.setText(Integer.toString(((TeamModel.Data) ListObject.get(position)).getId())); 

    } 
    else if(ListObject.get(position) instanceof TeamModel.GroupData) 
    { 
     // System.out.println("teh valus are-geroup name--"+((TeamModel.GroupData) ListObject.get(position)).getCceTeamName()); 
     holder.name.setText(((TeamModel.GroupData) ListObject.get(position)).getCceTeamName()); 
     holder.id.setText(Integer.toString(((TeamModel.GroupData) ListObject.get(position)).getId())); 
    } 
    // holder.check.setChecked(items.get(position).isSelected()); 
    return convertView; 
} 
public boolean isChecked(int position) { 
    return mCheckStates.get(position, false); 
} 

public void setChecked(int position, boolean isChecked) { 
    mCheckStates.put(position, isChecked); 

} 

public void toggle(int position) { 
    setChecked(position, !isChecked(position)); 

} 
} 

+0

인스턴스를 저장해야합니다. 다음 예제를 시도하십시오 : https://gist.github.com/curioustechizen/6ed0981b013f63236f0b – Fakher

+0

먼저'startActivityForResult()'를 호출하여 select 액티비티를 호출해야합니다. 그리고 나서 ok 버튼을 누르면'setResult() '의 이전 액티비티로 모든 선택된 값을 전달합니다. '. 다음에 데이터를 입력하여 select 활동을 시작합니다. –

+0

저장 인스턴스를 관리하거나 상태를 저장해야 onCreate() 또는 onResume()에서 호출 할 수 있습니다. 다음 링크를 확인하십시오. https://developer.android.com/guide/components/activities/activity-lifecycle.html –

답변

0

당신은 (예를 들어) SharedPreferences에 확인 된 이름을 저장하고 이전 값을 설정하는 활동의 onCreate() 환경 설정에서 확인해야합니다.

활동을 다시 열면 0에서부터 생성됩니다.


UPDATE :

통과하려면 확인 값 :

long[] checkedPositions = new long[]{1, 2, 6, 9, 12}; //Example checked positions 

Intent intent = new Intent(context, Activity.class); 
intent.putExtra("checked_names", checkedPositions); 
context.startActivity(intent); 

어댑터가 새로운 활동에 만든 후 어댑터의 방법을 사용할 수있는 체크 박스를 확인하려면 :

public void setChecked(int position, boolean isChecked) { 
    mCheckStates.put(position, isChecked); 

} 

또는 예를 들어 생성자.