androidlistviewgoogle-playcheckedtextview

Disable Listview item clicks if user is not premium user


I have a listview with checkedTextview items as the rows. the listview is the layout for a navigation drawer.

What I'm trying to achieve is, If they are not premium, prompt the play store dialog box and don't not allow them to alter the state of the check. If they are premium, do not show the premium dialog box, and allow them to alter the state of the check boxes.

I tried an if/else in several different places to Check if they are premium or not, but it didn't work because I was still able to click and change the state of the checkboxes.

the items in the list view are only access able/clickable to premium users. if they are not premium then when they click on a list item a google play dialog box will prompt the user to upgrade. I have tried alot of different options like setting listvivew.setEnabled(false) and setClickable(false), and .invalidate(); amongst other things but nothings worked so far.

 private void addDrawerItems() {
    String[] osArray = {"Bluetooth", "Reply to Calls", "Reply to sms", "customise message"};

    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, osArray);
    mDrawerList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    mDrawerList.setAdapter(mAdapter);



    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            CheckedTextView ctv = (CheckedTextView) view;


            switch (position) {

                case 0:

                        if (ctv.isChecked()) {
                            Toast.makeText(getApplicationContext(), "Bluetooth On", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(getApplicationContext(), "Bluetooth OFF", Toast.LENGTH_LONG).show();
                        }

                    break;

                case 1:
                    if (ctv.isChecked()) {
                        Toast.makeText(getApplicationContext(), "Calls Reply On", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "Calls Reply OFF", Toast.LENGTH_LONG).show();
                    }
                    break;

                case 2:
                    if (ctv.isChecked()) {
                        Toast.makeText(getApplicationContext(), "Sms Reply On", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "Sms Reply OFF", Toast.LENGTH_LONG).show();
                    }
                    break;

                case 3:

                    Toast.makeText(getApplicationContext(), "Customised Message", Toast.LENGTH_LONG).show();

                    break;
            }
        }
    });
}

!What my Navigation Drawer Looks like1


Solution

  • You need to set the ListView choice mode depending on the user privileges.

    If we assume you have a global variable to indicate the user state

    boolean mIsPremiumUser; 
    

    then depending on its value:

    if (mIsPremiumUser)
    {
        mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
    }
    else
    {
        mDrawerList.setChoiceMode(AbsListView.CHOICE_MODE_NONE);
    }
    

    This will toggle the checkable state of the list items. The OnItemClickListener has to be changed as follows:

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        CheckedTextView ctv = (CheckedTextView) view;
    
        if (! mIsPremiumUser)
        {
            // code to display upgrade dialog here
            return;
        }
    
        switch (position)
        {
            case 0:
            // continue as before...
        }
    }