0

Before I added a ListView, the Activity displayed a menu with with a plus symbol and a trash can symbol. With the ListView, the menu disappeared and the ListView takes up the whole screen. Is there something in the code that's doing that or is there something that needs to be added to code to get the menu to display?

I'm trying to make a password vault, so I hope this will help someone doing the same thing will benefit from this question.

The XML Layout

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".PasswordListActivity"
        android:orientation="vertical">

        <ListView
            android:id="@+id/passwordsListView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

The Java Activity

package com.joshuawhipple.mypasswords;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

public class PasswordListActivity extends Activity {

    private ListView passwordsListView;

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

        //create the data for the listview - TEST
        ArrayList <HashMap<String,String>> data = new ArrayList<>();
        for(int i = 1; i < 15; i++){
            HashMap<String,String> map = new HashMap<>();
            map.put("Username","Username" + i);
            map.put("Password","Password" + i);
            data.add(map);
        }

        //create the resource, from, and to variables - TEST
        int resource = R.layout.listview_item;
        String[] from = {"Username","Password"};
        int[] to = {R.id.usernameTextView,R.id.passwordTextView};

        //create and set the adapter - TEST
        passwordsListView = (ListView)findViewById(R.id.passwordsListView);
        SimpleAdapter adapter = new SimpleAdapter(this, data, resource,from,to);
        passwordsListView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.my_passwords_menu, menu);
        return true;
    }

    //this controls the action of menu icons when click
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_add:
                Toast.makeText(this, "Add", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.menu_delete:
                Toast.makeText(this, "Delete", Toast.LENGTH_SHORT).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

The Display #

A screen shot of the Activity with the listview and missing menu

1 Answers1

1

I think your problem may be with the action bar. Try to extend from AppCompatActivity instead of Activity. Check this answer from another thread: Android ActionBar/Toolbar Disappears in my App

Vera Rivotti
  • 500
  • 4
  • 9