【发布时间】:2011-03-22 05:17:17
【问题描述】:
我有一个 ListView 活动,我希望在其上方显示一个 EditText(最终还有一个按钮)。
我相信我的 xml 很好,但由于某种原因,EditText 没有显示。 ListView 占据了整个屏幕:(
这是我的 XML 的样子:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/newitemtext"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="@string/add_new_item"
android:maxLines="1"
/>
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/inbox_no_items"
/>
</LinearLayout>
有什么想法吗?
注意:在 Eclipse 中 xml 文件的布局选项卡中,EditText 显示。当我在模拟器中运行我的应用程序时,它没有。
感谢您的宝贵时间
更新:
这里是我在 onCreate 方法中设置内容视图的地方
m_aryNewListItems = new ArrayList<MyListItem>();
m_Adapter = new MyListAdapter(this, R.layout.layout_list, m_aryNewListItems);
setListAdapter(m_Adapter);
这是我的扩展 ArrayAdapter:
private class MyListAdapter extends ArrayAdapter<MyListItem> {
// items in the list
private ArrayList<MyListItem> m_items;
// constructor
public MyListAdapter(Context context, int textViewResourceId, ArrayList<MyListItem> items) {
super(context, textViewResourceId, items);
this.m_items = items;
}
// get specific list item
public MyListItem getItem(int position) {
if (m_items.size() > position) {
return m_items.get(position);
}
else {
return null;
}
}
/*
* Update screen display
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// get the View for this list item
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.layout_list_item, null);
}
// get the next MyListItem object
MyListItem lstItem = m_items.get(position);
// set up the list item
if (lstItem != null) {
TextView txtItem = (TextView) v.findViewById(R.id.list_itemname);
TextView txtQty = (TextView) v.findViewById(R.id.list_itemqty);
TextView txtPlace = (TextView) v.findViewById(R.id.list_place);
// set item text
if (txtItem != null) {
txtItem.setText(lstItem.getItemName());
}
// set qty text
String strQtyText = "Qty: ";
if (txtQty != null && lstItem.getItemQty() != -1) {
BigDecimal bd = new BigDecimal(lstItem.getItemQty());
strQtyText += bd.stripTrailingZeros().toString();
if(lstItem.getItemQtyUnitId() != -1) {
strQtyText += " " + lstItem.getItemQtyUnitTextAbbrev();
}
}
else {
strQtyText += "--";
}
txtQty.setText(strQtyText);
// set place text
if (txtPlace != null && lstItem.getPlaceName() != null) {
txtPlace.setText(lstItem.getPlaceName());
}
}
// return the created view
return v;
}
}
【问题讨论】:
-
是否有任何代码正在重新排列视图,或者活动可能正在将不同的 xml 设置为内容视图?
-
感谢您的回复。我使用正确的 xml 作为内容视图。我已经扩展了 ArrayAdapter 并制作了自己的。我将编辑 OP 并发布我的扩展 ArrayAdapter 的代码。
-
使用扩展的 ArrayAdapter 定义更新了 OP
-
天哪。你完全正确。我正在查看我的 onCreate 的错误部分。我将内容视图设置为另一个 xml 文件。谢谢你敲我的头:P
-
我自己也被它咬过!
标签: android listview hidden android-edittext