danishanish
danishanish

Reputation: 345

Why does my ListView not show any content?

I'm trying to create a NavigationDrawer layout in Android, the main panel is working properly, and the drawer is a ListView. I've tried replacing the ListView with a TextView, which works fine. So, here's my code to add String content to the ListView.

Here is the relevant code.

private ArrayList<String> drawerContentStrings = new ArrayList<>();
private DrawerLayout homeLayout;
private ListView navigationDrawer;
private GridLayout homeContent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_layout);
    //homeContent = (GridLayout) findViewById(R.id.home_content);
    //drawerContentStrings = getResources().getStringArray(R.array.navigation_content_strings);
    //homeLayout = (DrawerLayout) findViewById(R.id.home_layout);
    navigationDrawer = (ListView) findViewById(R.id.navigation_drawer);
    System.out.println(drawerContentStrings.get(0));
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.nav_panel_item, drawerContentStrings);
    navigationDrawer.setAdapter(arrayAdapter);


} 

Here is the code for nav_panel_item (in the layout folder)

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:textColor="#000"
/>

And here is the code for the actual DrawerLayout: (idk if this is even relevant)

<GridLayout
    android:id="@+id/home_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
    android:orientation="horizontal"
    android:rowCount="2">

    <Button
        android:id="@+id/button1"
        android:layout_gravity="left|top"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_gravity="left|top"
        android:text="Button" />

    <Button
        android:id="@+id/button3"
        android:layout_gravity="left|top"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        android:layout_gravity="left|top"
        android:text="Button" />

</GridLayout>

<ListView android:id="@+id/navigation_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#aff"/>

So, how do I make this work? Because I have no idea. Thanks!

Upvotes: 1

Views: 299

Answers (2)

Lazy Ninja
Lazy Ninja

Reputation: 22527

I think it is because your set GridLayout height to match_parent which takes all the screen.
Change your GridLayout height to wrap_content and check.

<GridLayout
    android:id="@+id/home_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" // <---- change here to wrap_content and see.
    android:columnCount="2"
    android:orientation="horizontal"
    android:rowCount="2">

Upvotes: 0

sadtroll
sadtroll

Reputation: 191

In strings.xml, create an array list:

<string-array name="drawer_options">
    <item>Test</item>
    <item>Test</item>
    <item>Test</item>
</string-array>

Create a new layout file, called activity_drawer.xml :

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

<ListView
    android:id="@+id/nav_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#fff"
    android:choiceMode="singleChoice" />

</android.support.v4.widget.DrawerLayout>

Create a new Activity, called BaseActivity; it will help you to create a NavigationDrawer.

public class BaseActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {

private ListView drawerList;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private String[] options;

private TextView titleActionBar;

protected FrameLayout content;

protected void onCreateDrawer(final int layoutResID) {
    setContentView(R.layout.activity_drawer);

    content = (FrameLayout) findViewById(R.id.content_frame);
    getLayoutInflater().inflate(layoutResID, content, true);

    setupActionBar();

    options = getResources()
            .getStringArray(R.array.drawer_options);

    drawerLayout =
            (DrawerLayout) findViewById(R.id.drawer_layout);

    drawerList = (ListView) findViewById(R.id.nav_drawer);

    drawerList.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, options));

    drawerList.setOnItemClickListener(this);

    setupDrawer();

}

private void setupActionBar() {
           getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.action_bar);

    titleActionBar = (TextView) findViewById(R.id.title_action_bar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
}


private void setupDrawer() {
    mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close) {

        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            titleActionBar.setText("AppName");
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            titleActionBar.setText("Options");
            invalidateOptionsMenu();
        }
    };

    mDrawerToggle.setDrawerIndicatorEnabled(true);
    drawerLayout.setDrawerListener(mDrawerToggle);
}

//get the position of the clicked list item
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    switch (position) {
        case 2:
    //For example, I redirect users to LoginActivity when they click on position 2 //(in this case, last item)
            startActivity(new Intent(getApplicationContext(),     LoginActivity.class));
            drawerLayout.closeDrawer(drawerList);
            break;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.home_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}
}

In MainActivity:

public class HomeActivity extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.onCreateDrawer(R.layout.home_activity);
}
}

home_activity.xml is a "normal" layout file. Example:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<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"
    android:orientation="vertical"
    android:id="@+id/home_activity"
    android:weightSum="1">

    <TextView
        android:gravity="center"
        android:text="Hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:padding="10dp"
        android:textSize="20dp"
        android:textStyle="bold"/>

</LinearLayout>
</ScrollView>

Upvotes: 1

Related Questions