mardi 24 novembre 2015

Set invisible Check Box to visible in list view

Hey everyone so I have a list view that has a checkbox in the item that I am trying to make visible. I am getting the view out of the list view and setting visibility to visible but it does not show.

** 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://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    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://ift.tt/nIICcg"
    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>




Aucun commentaire:

Enregistrer un commentaire