Saba
Saba

Reputation: 79

OnItemClickListener does not work with CheckBoxes

I've already checked the solutions here, here, here and here. None of them works for me.

My problem is as follows: I've a ListView consists of just CheckBoxes. I would like to save the current status of the relevant CheckBox to the SharedPreferences as soon as the CheckBox is clicked, as checked or unchecked. But I cannot do so yet. I've already tried OnItemClickListener in Activity's onCreate method and then OnClickListener in Adapter's getView method. I don't know if somethings conflict with eachother.

Here is My Activity's onCreate method:

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


    foodItems = new ArrayList<>();
    foodItems.add(new FoodItem("Fridge and Stove", fridgeKey));
    foodItems.add(new FoodItem("Baked Alaska", bakedAlaskaKey));
    ...

    ListView foodListView = (ListView) findViewById(R.id.food_list_view);
    foodListView.setItemsCanFocus(true);
    FoodItemAdapter foodItemAdapter = new FoodItemAdapter(this, foodItems);
    foodListView.setAdapter(foodItemAdapter);
}

Here is current status of my adapter (I've tried this and that):

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View foodItemView = convertView;
    if (foodItemView == null) {
        foodItemView = LayoutInflater.from(getContext()).inflate(R.layout.item_food, parent, false);
    }
    final FoodItem currentFoodItem = getItem(position);
    final CheckBox foodCheckBox = (CheckBox) foodItemView.findViewById(R.id.item_food_check_box);
    foodCheckBox.setText(currentFoodItem.getText());
    Boolean status = getStatus(currentFoodItem.getPrefsKey());
    foodCheckBox.setChecked(status);
    Log.i("Food Item Adapter", "get view");

    foodItemView.setClickable(true);
    foodItemView.setFocusable(true);
    foodItemView.setBackgroundResource(android.R.drawable.menuitem_background);
    foodItemView.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("Food Item Adapter", "on click");
                    SharedPreferences prefs = getContext().getSharedPreferences("com.example.myapplication", Context.MODE_PRIVATE);
                    prefs.edit().putBoolean(currentFoodItem.getPrefsKey(), foodCheckBox.isChecked()).apply();
                    new AlertDialog.Builder(getContext()).setTitle("touched").show();
                }
    });

    return foodItemView;
}

Upvotes: 0

Views: 57

Answers (2)

Omkar C.
Omkar C.

Reputation: 751

Try below code:

final CheckBox foodCheckBox = (CheckBox)findViewById(R.id.item_food_check_box);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    final SharedPreferences.Editor editor = preferences.edit();

    foodCheckBox.setChecked(prefs.getBoolean("checked",false));

foodCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         isCheckedValue = isChecked;
        editor.putBoolean("checked", isChecked);
        editor.apply();
        }
    });

You can use value in isCheckedValue to send anywhere you need.

And then if you want to send this value to any of the activity you can use below code

 Intent in = new Intent(MainActivity.this, IntendedActivity.class);
                in.putExtra("yourBoolName", isCheckedValue );
                startActivity(in);

Hope that helps.

Upvotes: 1

Gabriel Rocha
Gabriel Rocha

Reputation: 49

Try to add this attribute to your list item layout:

android:descendantFocusability="afterDescendants"

This will allow the children of the list item to draw focus.

Upvotes: 0

Related Questions