Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set invisible Check Box to visible in list view

I have an invisible checkbox in a ListView, which I want to make visible. But, not able to achieve it. Have a look at the following code snippets:

List activity :-

public class SelectDateRangeActivity extends AppCompatActivity {

    ListView mOptionsList;
    LogRangeOptionsAdapter mAdapter;

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

        mOptionsList = (ListView) findViewById(R.id.date_range_options_list);
        mAdapter = new LogRangeOptionsAdapter(this, R.layout.date_range_option_list_item, getResources().getStringArray(R.array.image_log_date_filter_options));
        mOptionsList.setAdapter(mAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String selectedOption = extras.getString(Utility.EXTRA_LOG_SELECTED_DATE_RANGE);
            if (selectedOption.equalsIgnoreCase(getResources().getStringArray(R.array.image_log_date_filter_options)[0])){
                final View view = mOptionsList.getAdapter().getView(0, null, mOptionsList);
                CheckBox c = (CheckBox) view.findViewById(R.id.image_log_checkbox);
                c.setVisibility(View.VISIBLE);
                c.setChecked(true);
                mAdapter.notifyDataSetChanged();
            }

            if (selectedOption.equalsIgnoreCase(getResources().getStringArray(R.array.image_log_date_filter_options)[1])){
                final View view = mOptionsList.getAdapter().getView(1, null, mOptionsList);
                CheckBox c = (CheckBox) view.findViewById(R.id.image_log_checkbox);
                c.setVisibility(View.VISIBLE);
                c.setChecked(true);
                mAdapter.notifyDataSetChanged();
            }

            if (selectedOption.equalsIgnoreCase(getResources().getStringArray(R.array.image_log_date_filter_options)[2])){
                final View view = mOptionsList.getAdapter().getView(0, null, mOptionsList);
                CheckBox c = (CheckBox) view.findViewById(R.id.image_log_checkbox);
                c.setVisibility(View.VISIBLE);
                c.setChecked(true);
                mAdapter.notifyDataSetChanged();
            }

            if (selectedOption.equalsIgnoreCase(getResources().getStringArray(R.array.image_log_date_filter_options)[3])){
                final View view = mOptionsList.getAdapter().getView(0, null, mOptionsList);
                CheckBox c = (CheckBox) view.findViewById(R.id.image_log_checkbox);
                c.setVisibility(View.VISIBLE);
                c.setChecked(true);
                mAdapter.notifyDataSetChanged();
            }
        }
    }
}

Adapter:-

public class LogRangeOptionsAdapter extends ArrayAdapter<String> {

    Context mAdapterContext;
    String[] mItems;

    public LogRangeOptionsAdapter(Context context, int resource, String[] objects) {
        super(context, resource, objects);
        this.mAdapterContext = context;
        this.mItems = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mAdapterContext.getSystemService(mAdapterContext.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.date_range_option_list_item, parent, false);
        }

        TextView mTitleText = (TextView) convertView.findViewById(R.id.date_range_option_text);
        mTitleText.setText(mItems[position]);
        final String titleText = mTitleText.getText().toString();

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent returnOptionIntent = new Intent(mAdapterContext, ImageLogActivity.class);
                returnOptionIntent.putExtra(Utility.EXTRA_LOG_DATE_RANGE_OPTION, titleText);
                Activity activity = (Activity) mAdapterContext;
                activity.setResult(Utility.INTENT_IMAGE_LOG_OPTIONS_CODE, returnOptionIntent);
                activity.finish();
                activity.overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_out_left);
            }
        });

        return convertView;
    }
}

Select date range xml:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    tools:context="com.sigue.sigueagent.SelectDateRangeActivity">

    <ListView
        android:id="@+id/date_range_options_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>

</RelativeLayout>

Date Range options:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="@dimen/item_date_range_padding_top"
    android:paddingBottom="@dimen/item_date_range_padding_bottom">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/date_range_option_text"
        android:layout_alignBottom="@+id/image_log_checkbox"
        android:layout_alignTop="@+id/image_log_checkbox"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:gravity="center" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/image_log_checkbox"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:visibility="invisible"/>

</RelativeLayout>
like image 538
Doug Ray Avatar asked Nov 24 '15 22:11

Doug Ray


1 Answers

So I figured this one out by setting visible or invisible in the adapter instead of trying to do it in the actual activity here is the changed activity. Hope it helps someone :) !

public class SelectDateRangeActivity extends AppCompatActivity {

    ListView mOptionsList;
    LogRangeOptionsAdapter mAdapter;
    String mSelectedOption;

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

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            mSelectedOption = extras.getString(Utility.EXTRA_LOG_SELECTED_DATE_RANGE);
        }

        mOptionsList = (ListView) findViewById(R.id.date_range_options_list);
        mAdapter = new LogRangeOptionsAdapter(this, R.layout.date_range_option_list_item, getResources().getStringArray(R.array.image_log_date_filter_options));
        mAdapter.setCheckedOption(mSelectedOption);
        mOptionsList.setAdapter(mAdapter);
    }
}

**adapter

public class LogRangeOptionsAdapter extends ArrayAdapter<String> {

    Context mAdapterContext;
    String[] mItems;
    String mSelectedOption;

    public LogRangeOptionsAdapter(Context context, int resource, String[] objects) {
        super(context, resource, objects);
        this.mAdapterContext = context;
        this.mItems = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mAdapterContext.getSystemService(mAdapterContext.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.date_range_option_list_item, parent, false);
        }

        TextView mTitleText = (TextView) convertView.findViewById(R.id.date_range_option_text);
        mTitleText.setText(mItems[position]);
        final String titleText = mTitleText.getText().toString();

        CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.date_range_option_checkbox);

        if (mSelectedOption.equalsIgnoreCase(mAdapterContext.getResources().getStringArray(R.array.image_log_date_filter_options)[0]) && position == 0) {
            checkBox.setVisibility(View.VISIBLE);
            checkBox.setChecked(true);
        }

        if (mSelectedOption.equalsIgnoreCase(mAdapterContext.getResources().getStringArray(R.array.image_log_date_filter_options)[1]) && position == 1) {
            checkBox.setVisibility(View.VISIBLE);
            checkBox.setChecked(true);
        }

        if (mSelectedOption.equalsIgnoreCase(mAdapterContext.getResources().getStringArray(R.array.image_log_date_filter_options)[2]) && position == 2) {
            checkBox.setVisibility(View.VISIBLE);
            checkBox.setChecked(true);
        }

        if (mSelectedOption.equalsIgnoreCase(mAdapterContext.getResources().getStringArray(R.array.image_log_date_filter_options)[3]) && position == 3) {
            checkBox.setVisibility(View.VISIBLE);
            checkBox.setChecked(true);
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent returnOptionIntent = new Intent(mAdapterContext, ImageLogActivity.class);
                returnOptionIntent.putExtra(Utility.EXTRA_LOG_DATE_RANGE_OPTION, titleText);
                Activity activity = (Activity) mAdapterContext;
                activity.setResult(Utility.INTENT_IMAGE_LOG_OPTIONS_CODE, returnOptionIntent);
                activity.finish();
                activity.overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_out_left);
            }
        });

        return convertView;
    }

    public void setCheckedOption(String option){
        this.mSelectedOption = option;
    }
}
like image 172
Doug Ray Avatar answered Nov 01 '22 17:11

Doug Ray