简体   繁体   中英

Show/hide TextView for each item in custom listview

I have :

  • a custom listview item layout with a TextView called numberView inside.
  • a checkable option menu item called showNumbersItem

I'd like to set isVisible property of numberView to true if showNumbersItem is checked, and to false if not.

At the moment, I've this code :

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View itemView = convertView;

    if (itemView == null) {
        itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
    }

    TextView numberView = (TextView) itemView.findViewById(R.id.numberView);
    numberView.setText("+33601234567");
    numberView.setEnabled(showNumbers); //showNumbers is a boolean

    return itemView;
}

1. How can I set showNumbers boolean to showNumbersItem.isChecked just before the list is populated ?

I tried with this code... :

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    return super.onPrepareOptionsMenu(menu);

    showNumbers = menu.findItem(R.id.show_numbers).isChecked();
}

...but Android Studio says : "Unreachable statement"

2. And how can I set isVisible property of numberView for each item in the listview ?

At the moment, I have this code :

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case (R.id.show_numbers):
            item.setChecked(!item.isChecked());
            showNumbers = item.isChecked();

            //I don't know

            break;
    }
    return super.onOptionsItemSelected(item);
}

For #1:

While the default value is checked, you can just init your showNumbers boolean with true , showNumbers = true;

For #2:

To change view visibility:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    .....

    if(showNumbers){
       numberView.setVisibility(View.VISIBLE);
    }else{
       numberView.setVisibility(View.INVISIBLE);
    }
    ....
    return itemView;
}

To notify your list adapter:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case (R.id.show_numbers):
            item.setChecked(!item.isChecked());
            showNumbers = item.isChecked();

            yourAdapter.notifyDatasetChanged();

            break;
    }
    return super.onOptionsItemSelected(item);
}

First you need to pass showNumbers variable to your adapter and you need to add code in getView() to make the View invisible. Your probably want to set the Visibility to GONE instead. Invisible leaves space for the View event if it is not Shown.

if(showNumbers)
    numberView.setVisibility(View.VISIBLE);
else
    numberView.setVisibility(View.INVISIBILE);
    // or numberView.setVisibility(View.GONE);

Then I don't think you have anything to do with your onPrepareOptionMenus(). It tells you code not reachable because you have a return statement before your code. If it is necessary you should do it like this:

showNumbers = menu.findItem(R.id.show_numbers).isChecked();
return super.onPrepareOptionsMenu(menu);

Finally in onOptionItemSelected() you should change your boolean in the adapter and call notifyDatasetChanged():

case (R.id.show_numbers):
    item.setChecked(!item.isChecked());
    showNumbers = item.isChecked();
    yourAdapter.setShowNumbers(showNumbers );
    yourAdapter.notifyDatasetChanged();
    break;

You may need to add the setter for showNumbers in your adapter class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM