android-studioitemlistener

Item List not Clickable


Item list instantiates but the items but click event (toast) doesnt occur when clicked.

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

private DrawerLayout drawerLayout;
private NavigationView navigationView;
Toolbar toolbar;
FragmentTransaction fragmentTransaction;
FragmentManager fragmentManager;
Fragment fragment;
FrameLayout frameLayout;
ListView listView;

String[] workoutsRoutines = {"Upper Body", "Lower body"};
int[] workoutsBackgrounds = {R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground};

int count;

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

    drawerLayout = findViewById(R.id.drawer_layout);
    navigationView = findViewById(R.id.nav_view);
    navigationView.bringToFront();
    navigationView.setNavigationItemSelectedListener(this);
    toolbar = findViewById(R.id.toolbar);
    toolbar.setTitle(null);
    toolbar.setNavigationIcon(R.drawable.ic_android_black_24dp);

item list click event listView = findViewById(R.id.listview); CustomAdapter customAdapter = new CustomAdapter();

    listView.setAdapter(customAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
            Toast.makeText(MainActivity.this, "succ", Toast.LENGTH_SHORT).show();
        }
    });

    toolbar.setNavigationOnClickListener(v -> {

        drawerLayout.openDrawer(GravityCompat.START);
        Toast.makeText(MainActivity.this, "succ", Toast.LENGTH_SHORT).show();
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        }


        if (fragment != null) {
            count = fragmentManager.getBackStackEntryCount();
            for (int i = 0; i < count; i++) {
                fragmentManager.popBackStack();
                drawerLayout.closeDrawer(GravityCompat.START);
            }

            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction().remove(fragment);

            fragmentTransaction.commit();
        }

        toolbar.setNavigationIcon(R.drawable.ic_android_black_24dp);
    });

}

@Override
public void onBackPressed() {
    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
        drawerLayout.closeDrawer(GravityCompat.START);
    } else super.onBackPressed();
    count = fragmentManager.getBackStackEntryCount();
    for (int i = 0; i < count; i++) {
        fragmentManager.popBackStack();
    }
    toolbar.setNavigationIcon(R.drawable.ic_android_black_24dp);
}

private class CustomAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        return workoutsBackgrounds.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup parent) {
        View view1 = getLayoutInflater().inflate(R.layout.rowdata, null);

        TextView name = view1.findViewById((R.id.workouts));
        ImageView image = view1.findViewById((R.id.background));

        name.setText(workoutsRoutines[i]);
        image.setImageResource(workoutsBackgrounds[i]);

        return view1;
    }
}

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

    switch (item.getItemId()) {
        case R.id.nav_profile:
            fragment = new profileFragment();
            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction().replace(R.id.frameLayout, fragment).addToBackStack(null);
            fragmentTransaction.commit();
            toolbar.setNavigationIcon(R.drawable.ic_baseline_arrow_back_ios_24);


            break;

        case R.id.nav_custom_workouts:
            fragment = new customWorkoutFragment();
            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction().replace(R.id.frameLayout, new customWorkoutFragment()).addToBackStack(null);
            fragmentTransaction.commit();
            toolbar.setNavigationIcon(R.drawable.ic_baseline_arrow_back_ios_24);

            break;
    }
    item.setChecked(true);
    drawerLayout.closeDrawer(GravityCompat.START);
    return true;
}

}


Solution

  • needed to bring itemlist view to front