Home > Blockchain >  ListView not showing up in the fragment when using with Navigarion Drawer
ListView not showing up in the fragment when using with Navigarion Drawer

Time:09-17

I have a Navigation Drawer that I can use to switch between the Fragment layouts. In the fragment_teacher Fragment, I have an empty listView, that I am trying to populate. In theory, the list does get populated, but when I switch to that Fragment in the application itself, nothing shows up. How could I make this work?

MainActivity.java:

package com.promate04.dszp_map;

import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.navigation.NavigationView;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;

import com.promate04.dszp_map.databinding.ActivityMainBinding;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;
    private ActivityMainBinding binding;
    //private List<Teacher> myTeachers = new ArrayList<Teacher>();
    private ListView myTeachers;
    private String fullNames[] = {
            "Teacher #1",
            "Teacher #2",
            "Teacher #3",
            "Teacher #4",
            "Teacher #5",
            "Teacher #6",
            "Teacher #7",
            "Teacher #8"
    };

    private String descrptions[] = {
            "something",
            "something else",
            "also this",
            "also that",
            "this aswell",
            "and this",
            "this needs to be here aswell",
            "yes this aswell"
    };

    private int pictureID[] = {
            R.drawable.placeholder,
            R.drawable.placeholder,
            R.drawable.placeholder,
            R.drawable.placeholder,
            R.drawable.placeholder,
            R.drawable.placeholder,
            R.drawable.placeholder,
            R.drawable.placeholder
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setSupportActionBar(binding.appBarMain.toolbar);
        DrawerLayout drawer = binding.drawerLayout;
        NavigationView navigationView = binding.navView;
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_bell, R.id.nav_timetable, R.id.nav_map, R.id.nav_news, R.id.nav_teachers, R.id.nav_settings)
                .setOpenableLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);

        ListView _teacherListView = (ListView) findViewById(R.id.teachersListView);
        //_teacherListView.addHeaderView(textView);

        Teacher teacherList = new Teacher(this, fullNames, descrptions, pictureID);
        _teacherListView.setAdapter(teacherList);

        _teacherListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Toast.makeText(getApplicationContext(), "You selected "   fullNames[position]   " teacher", Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu, this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
}

Teacher.java:

package com.promate04.dszp_map;

import android.app.Activity;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import com.promate04.dszp_map.databinding.FragmentTimetableBinding;

public class Teacher extends ArrayAdapter {
    private String[] fullNames;
    private String[] descriptions;
    private int[] pictureID;
    private Activity context;


    public Teacher(Activity _context, String[] _fullNames, String[] _descriptions, int[] _pictureID) {
        super(_context, R.layout.teacher_profile, _fullNames);
        this.context = _context;
        this.fullNames = _fullNames;
        this.descriptions = _descriptions;
        this.pictureID = _pictureID;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View item_view = convertView;
        LayoutInflater inflater = context.getLayoutInflater();
        if (convertView == null)
            item_view = inflater.inflate(R.layout.teacher_profile, null, true);
        TextView _txtFullName = (TextView) item_view.findViewById(R.id.teacher_txtFullName);
        TextView _txtDescription = (TextView) item_view.findViewById(R.id.teacher_txtDescription);
        ImageView _teacher_pfp = (ImageView) item_view.findViewById(R.id.teacher_pfp);

        _txtFullName.setText(fullNames[position]);
        _txtDescription.setText(descriptions[position]);
        _teacher_pfp.setImageResource(pictureID[position]);
        return item_view;
    }
}

fragment_teacher.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.teacher.TeacherFragment">

    <ListView
        android:id="@ id/teachersListView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginBottom="0dp" />
</RelativeLayout>

CodePudding user response:

I'm dumb, of course I need to put my code into the fragment's code file, not the MainActivity..... Thanks for commenting, it made me realise

  • Related