ListView未填充

时间:2013-02-25 13:44:18

标签: android

我有扩展FragmentActivity的类,我还有一个包含ListView listview_layout.xml

的布局xml文件

<LinearLayout
    android:layout_width="310dip"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:orientation="vertical"
    android:paddingLeft="15dp"
    android:paddingRight="15dp" >

    <ListView
        android:id="@+id/menuList"
        android:layout_width="310dip"
        android:layout_height="wrap_content"
        android:background="#00FFFF"
        android:dividerHeight="2dip" />
</LinearLayout>

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

请注意ListView menuList

的ID

从我的java代码中,我以这种方式获取此列表视图:

private View getElementFromView(int layout, int element) {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(layout, null);
        return view.findViewById(element);
    }

此方法会向我返回View,我会进行检查,以查看此ViewListView的实例

    View v = getElementFromView(R.layout.listview_layout, R.id.menuList);
    if(v instanceof ListView) {
        Log.v("View: ", "This view is an instance of a ListView"); 
        ListView lv = (ListView)v; 
        String[] listItems = {"Item 1", "Item 2", "Item 3", "Item 4"}; 
        lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems));
    }

正如您所看到的,我想为此Adapter设置ListView,这应该使用文本第1项,第2项等填充ListView

我不明白为什么列表视图根本没有出现?我想它自ListView根本没有填充,但为什么不填充?

编辑:

我还应该提到这个项目被标记为图书馆。这是BaseWindow.java

的有趣部分
  public class BaseWindow extends SlidingFragmentActivity {
        public void InitializeWindow(Activity act, int layoutResourceId, int menuResourceId, String windowTitle)  {
                    super.setContentView(layoutResourceId); 
                    super.setBehindContentView(R.layout.menu_frame);  
                    this.menuResourceId = menuResourceId; 
                    this.act = act; 
                    initializeSlidingMenu();
                    setWindowTitle(windowTitle); 
         }

         public void initializeSlidingMenu()  {

            this.setSlidingActionBarEnabled(true);
            this.setSlidingActionBarEnabled(true);
            getSlidingMenu().setBehindOffsetRes(R.dimen.actionbar_home_width);
            getSlidingMenu().setShadowWidthRes(R.dimen.shadow_width);
            getSlidingMenu().setShadowDrawable(R.drawable.shadow);
            getSlidingMenu().setMenu(R.layout.listview_layout);
            getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
            getSlidingMenu().setBehindScrollScale(0.25f);

            View v = getElementFromView(R.layout.listview_layout, R.id.menuList);
            if(v instanceof ListView) {
                Log.v("View: ", "This view is an instance of a ListView"); 
                ListView lv = (ListView)v; 
                String[] listItems = {"Item 1", "Item 2", "Item 3", "Item 4"}; 
                lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems));
                //lv.setAdapter(new MenuAdapter(this, menuResourceId));
            }       
        }
    }

这个类是从我工作区中的另一个项目调用的,是的,我已将类/项目作为库包含在内:

public class MainActivity extends BaseWindow {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.InitializeWindow(MainActivity.this, R.layout.activity_main, R.menu.slide_menu, "Home"); 

    }
}

一切都运行良好,但列表视图根本没有填充。希望这可以让您了解我想要实现的目标。

2 个答案:

答案 0 :(得分:2)

据我所知,此处创建的视图View view = inflater.inflate(layout, null);永远不会附加到窗口。

您必须调用setContentView(View v)或其他东西将此布局放在屏幕上

答案 1 :(得分:0)

ListView工作正常,所有项目都显示出来,问题是FrameLayout位于LinearLayout之上。