为什么我的ListView没有显示任何内容?

时间:2015-07-03 04:21:12

标签: android listview android-listview android-adapter android-adapterview

我正在尝试在Android中创建NavigationDrawer布局,主面板工作正常,抽屉是ListView。我已经尝试用TextView替换ListView,它工作正常。所以,这是我将List内容添加到ListView的代码。

以下是相关代码。

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);


} 

以下是nav_panel_item的代码(在布局文件夹中)

<?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"
/>

以下是实际DrawerLayout的代码:(如果相关则为idk)

<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"/>

那么,我该如何使这项工作?因为我不知道。谢谢!

2 个答案:

答案 0 :(得分:1)

在strings.xml中,创建一个数组列表:

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

创建一个名为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>

创建一个名为BaseActivity的新Activity;它将帮助您创建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);
}
}

在MainActivity中:

public class HomeActivity extends BaseActivity {

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

home_activity.xml是&#34;正常&#34;布局文件。例如:

<?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>

答案 1 :(得分:0)

我认为这是因为你将GridLayout的高度设置为match_parent,它占据了所有的屏幕 将GridLayout高度更改为wrap_content并检查。

var query = _sqliteService.Conn.Table<TTable>();