Question

So I am using a custom ArrayAdapter implementation so that I have access to the toggle button inside my listview elements.

Each listview has a textview and a toggle button. I had to use a custom ArrayAdapter attached to the listview so that I could get the correct toggle button attached to the position of the listview that was clicked. Yet, each time I inflate the layout to get the view, the textview in the listview is disappearing. The values are still there, but the textview isnt. However the toggle buttons show up fine. This is the code used to inflate the layout and get the view:

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            v = inflater.inflate(R.layout.activity_alarm_item, parent, false);
        }
        ToggleButton toggle = (ToggleButton) v.findViewById(R.id.alarm_status);
        toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Log.d("ENABLE ALARM", Integer.toString(position));
                    enableAlarm(position);
                } else {
                    disableAlarm(position);
                }

            }
        });
        return v;
    }

Does something look wrong with the two lines that inflate the layout?

The layout is fine. I reverted back to the original adapter and the textview text came back up.

However, Here is the activity layout:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" >

    <TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:textIsSelectable="false"
        android:textSize="20sp"
        android:paddingLeft="5dp" />


    <ToggleButton
        android:id="@+id/alarm_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:clickable="true"
        android:onClick="enableAlarm"
        android:background="?android:attr/selectableItemBackground"
        android:focusable="false" />

    <TextView
        android:layout_width="1px"
        android:layout_height="fill_parent"
        android:layout_toLeftOf="@id/alarm_status"
        android:layout_centerVertical="true"
        android:background="?android:attr/dividerVertical" />

    </RelativeLayout>
Was it helpful?

Solution

Where you are setting the text to TextView ? TextView seems to be empty. That's why it is not displaying i guess.

Either set the text to TextView in Xml file itself

android:text ="text"

or else inside getView function

TextView textView = (TextView) v.findViewById(R.id.time);
textView.setText("Text");

OTHER TIPS

Too long to write in a comment, I try it as an answer. I am not sure you are getting the context correctly. Personally in a custom adapter I declare the layout inflater as follow :

private LayoutInflater mInflater = (LayoutInflater)   
                getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

then I use the inflater in the getView() as follow

v = mInflater.inflate(R.layout.activity_alarm_item, parent, false);

Tell me if it solves your problem orelse I remove my answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top