0

I'm trying to display a ListView from an ArrayList of strings in my Navigation Menu and its just coming up blank, no data is being passed to the ListView.

To access a new layout file other than activity_main.xml, I'm using "ConstraintLayout layout1 = (ConstraintLayout) View.inflate(this, R.layout.nav_choose_foods, null);" then, "ListView listView = layout1.findViewById(R.id.search_list);"

This at least doesn't crash the program, it seems to find the ID ok, but it's still not displaying the ListView. I this possible? Or is there a better way to accomplish what I'm trying here?

Thanks for any help!

Code is below:

Main Actvity -



public class MainActivity extends AppCompatActivity implements RecyclerViewAdapter_AllFoods.ItemClickListener, RecyclerViewAdapter_Nutrients.ItemClickListener, RecyclerViewAdapter_FoodTotals.ItemClickListener{

    public DrawerLayout drawerLayout;
    public ActionBarDrawerToggle actionBarDrawerToggle;


    ArrayList<String> list;
    ArrayAdapter<String> adapterList;


    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Objects.requireNonNull(getSupportActionBar()).setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.custom_toolbar);

        ActionBar action = getSupportActionBar();
        action.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);


        drawerLayout = findViewById(R.id.my_drawer_layout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_open, R.string.nav_close);
        ConstraintLayout layout1 = (ConstraintLayout) View.inflate(this, R.layout.nav_choose_foods, null);
        // pass the Open and Close toggle for the drawer layout listener
        // to toggle the button
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();

        // to make the Navigation drawer icon always appear on the action bar
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Pineapple");


        adapterList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);

        ListView listView =  layout1.findViewById(R.id.search_list);

        listView.setAdapter(adapterList);

    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {


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


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<!--the root view must be the DrawerLayout-->
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/my_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity"
    tools:ignore="HardcodedText">

    <com.google.android.material.navigation.NavigationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        app:menu="@menu/navigation_menu"

        />

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="blocksDescendants"
        tools:context=".MainActivity">


</androidx.drawerlayout.widget.DrawerLayout>

nav_choose_foods.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants">


    <ListView
        android:id="@+id/search_list"
        android:layout_width="295dp"
        android:layout_height="254dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.482"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/searchView"
        app:layout_constraintVertical_bias="0.05" />


</androidx.constraintlayout.widget.ConstraintLayout>

navigation_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/menu"
    >
    <group android:checkableBehavior="single">
        <item
            android:title="Choose Foods">
            <menu>
                <group
                    android:id="@+id/menu_top"
                    android:checkableBehavior="single">

                    <item
                        app:actionLayout="@layout/nav_choose_foods"
                        android:id="@+id/nav_radios"
                        android:title=""
                        />

                </group>
            </menu>
        </item>
        <item
            android:title="Activity Level">
            <menu>
                <group
                    android:id="@+id/menu_bottom"
                    android:checkableBehavior="single">

                </group>
            </menu>
        </item>
    </group>

</menu>

1 Answer 1

0

Well, if this helps anyone, I gave up on using "navigation_menu.xml" file. Instead I just used an include layout in my activity_main.xml file under "com.google.android.material.navigation.NavigationView". No need to inflate or anything, the id gets found no problem. Not sure if this is the best way to go, but it works.

If anyone has a better solution for this let me know.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.