我有扩展类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
给我,我做一个检查,看看这View
是否是一个实例ListView
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
,这应该用文本 Item 1、Item 2 等ListView
填充。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");
}
}
一切都很好,但列表视图根本没有填充。希望这能让您对想要实现的目标有所了解。