vendredi 26 juillet 2019

Trying to change picture(actually checkbox) when item clicked and save it in a custom listview with adapter

Im trying to change the picture(i mean one pic is a "checked box" and the other one "blank box") so when user clicks on an item in the listview the blank box will turn to "checked" box. I could do it with the dafault checkbox that android studio offers but i began to do it this way and i dont understand why my code doesnt do the work with "pictures"instead of default checkbox. By the way the code is letting user choos only one box. After having selected the item i tried to save the state with sharedpreferences but it didnt go well. Eveytime i start the app i see unchecked,blank boxes. I would appreciate any help. Thank you. Heres my code :

CUSTOMADAPTER

       public class CustomAdapter extends BaseAdapter {

 Activity activity;
List<UserModel> users;
LayoutInflater inflater;
CheckBoxActivity c=new CheckBoxActivity();

public CustomAdapter(Activity activity) {
    this.activity = activity;
}

public CustomAdapter(Activity activity, List<UserModel> users) {
    this.activity = activity;
    this.users = users;
    inflater = activity.getLayoutInflater();

}


@Override
public int getCount() {
    return users.size();
}

@Override
public Object getItem(int i) {
    return i;
}

@Override
public long getItemId(int i) {
    return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    ViewHolder holder = null;
    if (view == null) {
        view = inflater.inflate(R.layout.list_view_item, viewGroup, false);
        holder = new ViewHolder();
        holder.tvUserName = view.findViewById(R.id.tv_user_name);
        holder.ivCheckBox = view.findViewById(R.id.iv_check_box);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
        UserModel model = users.get(i);
        holder.tvUserName.setText(model.getUserName());
        if (model.isSelected) {
            c.switchOnOff=true;
            holder.ivCheckBox.setBackgroundResource(R.drawable.ic_check_box_black_24dp);

        } else {
            c.switchOnOff=false;

            holder.ivCheckBox.setBackgroundResource(R.drawable.ic_check_box_outline_blank_black_24dp);

        }

    }
    return view;
}

public void updateRecords(List<UserModel> users) {
    this.users = users;
    notifyDataSetChanged();
}

class ViewHolder {

    TextView tvUserName;
    ImageView ivCheckBox;
}

}

CheckBoxActivity

    public class CheckBoxActivity extends Activity {
    int preSelectedIndex =-1;
    public static final String SHARED_PREFS = "sharedPrefs";
    public static final String TEXT = "text";
    public static final String SWITCH1 = "switch1";
    private String text;
    public boolean switchOnOff;

   @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.singleselectionlistview);

     ListView listView=findViewById(R.id.singleselectionlistview);
    //CheckBox cb = (CheckBox) findViewById(R.id.iv_check_box);
    final List<UserModel> users=new ArrayList<>();
    users.add(new UserModel(false,"Dharm"));
    users.add(new UserModel(false,"Dharm"));
    users.add(new UserModel(false,"Dharm"));
    users.add(new UserModel(false,"Dharm"));
   final CustomAdapter adapter=new CustomAdapter(this,users);
  listView.setAdapter(adapter);

  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int i, long 
   l) {
    UserModel model=users.get(i);
   model.setSelected(true);
    users.set(i,model);

   if(preSelectedIndex>-1){
    UserModel preRecord=users.get(preSelectedIndex);
     preRecord.setSelected(false);
     users.set(preSelectedIndex,preRecord);

    }
   preSelectedIndex=i;
  adapter.updateRecords(users);




     }
   });
    }
   public void saveData() {
    SharedPreferences sharedPreferences = 
    getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();

    //editor.putString(TEXT, tv.getText().toString());
    editor.putBoolean(SWITCH1, switchOnOff);
    editor.apply();
    Toast.makeText(this, "data saved", Toast.LENGTH_SHORT).show();

   }
  public void loadData() {
    SharedPreferences sharedPreferences = 
    getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    switchOnOff = sharedPreferences.getBoolean(SWITCH1, false);
  }
    public void updateViews(){
    //tv.setText(text);
    //sw.setChecked(switchOnOff);

   }
   }




Aucun commentaire:

Enregistrer un commentaire