【发布时间】:2011-03-23 13:04:26
【问题描述】:
试图拥有由两个 TextView 组成的行的 ListView(可滚动列表)。我有一个来自数据库的项目列表,我想将其填充到 LinearLayout->ListView->TextView 但无法获取 id...
Layout 有点像这个教学链接,但是已经放弃了 RelativeLayout 并使用 LinearLayout 来让它工作。甚至不担心它的外观;只是还不能把它连在一起。
http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html
有两个 XML 文件(下面非常简短的详细信息) main.xml 和 stuff.xml
main.xml 有
...文本视图
...列表视图
stuff.xml 有
... android:id="@+id/firstLine"
... android:id="@+id/secondLine"
是的,我确实 setContentView(R.layout.main);在 onCreate 之后。
在 findViewByID(firstLine) 和 findViewID(secondLine) 上获取 null。
我有一个 ArrayAdapter,我在其中为 stuffView 充气。我对其他示例的思考和理解是,在我故意膨胀之前,它不会膨胀(这个嵌套的 stuffView)。一切正常,但是当我执行 findViewById 时,它返回 null,因此我无法 setText()。
由于我完全无知/新手,史诗般的失败。注意:我已经仔细阅读了 Reto 的书,尤其是第 163 页上的一个类似示例但失败失败失败......
有好心人能指引我正确的方向吗?
我必须膨胀这个嵌套视图吗? (Reto 的例子就是这样)。如果是这样,我错过了什么?我希望有人能给我指出一个更好的例子。我的代码在这一点上可能过于复杂而无法发布并且有点专有。
谢谢
main.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"
>
<TextView
android:id="@+id/identText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="some text here"
/>
<ListView
android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
thing_item.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"
>
<TextView
android:id="@+id/identText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="some text here"
/>
<ListView
android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
一个叫做 Thingy 的 pojo(这里不复制 Thingy.java - 非常简单)
主类:ShowLayoutNicely.java
package com.blap.test;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
public class ShowLayoutNicely extends Activity {
ListView myListView;
TextView myTextView;
ArrayList<Thingy> thingys;
ThingyAdapter aa;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myListView = (ListView) findViewById(R.id.myListView);
myTextView = (TextView) findViewById(R.id.identText);
thingys = new ArrayList<Thingy>();
aa = new ThingyAdapter(this, android.R.layout.simple_list_item_1, thingys);
myListView.setAdapter(aa);
// real deal has a call to go find items from database, populate array and notify of change.
Thingy thing1 = new Thingy("first", "first row");
thingys.add(thing1);
// sadly - this isn't triggering getView() which I've overriden...
aa.notifyDataSetChanged();
Thingy thing2 = new Thingy("second", "second row");
thingys.add(thing2);
aa.notifyDataSetChanged();
}
}
适配器重写类 ThingyAdapter.java
package com.blap.test;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ThingyAdapter extends ArrayAdapter<Thingy> {
int resource;
public ThingyAdapter (Context _context, int _resource, List<Thingy> _items){
super( _context, _resource, _items);
resource = _resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout thingView;
Thingy thingItem = getItem(position);
String identString = thingItem.getIdent();
String nameString =thingItem.getName();
if (convertView == null){
thingView= new LinearLayout(getContext());
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View nuView = vi.inflate(resource, thingView, true);
}
else{
thingView = (LinearLayout) convertView;
}
//here's the problem - these calls return nulls.
TextView row1 = (TextView)thingView.findViewById(R.id.firstLine);
TextView row2 = (TextView)thingView.findViewById(R.id.secondLine);
//and naturally these puke ingloriously....
row1.setText(thingItem.getIdent());
row2.setText(thingItem.getName());
return thingView;
}
}
所以这段代码本质上就是我正在寻求帮助的内容;将名称中性化为 Thingy.... 此示例未触发 getView()。这是我必须解决的次要问题。更重要的是,您对 findViewById 失败的帮助以及如果我的 XML 正确,将会帮助很多。
【问题讨论】:
-
您是否包含 main.xml 中的 stuff.xml?例如curious-creature.org/2009/02/25/…
-
如果您的意思是“您是否在 main.xml 中包含对 stuff.xml 的引用”?没有。就这么简单?
-
"是的,我确实 setContentView(R.layout.main); 就在 onCreate 之后。"您的意思是在调用 super.onCreate 之后,在您的 onCreate 方法中执行此操作吗?
-
是的,我愿意: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);设置内容视图(R.layout.main); // setup DB infra 我之前的 ListView 工作正常;正是当我在一个单独的 XML 文件中添加了从属的 RelativeView 时,它的连接才能访问 id,这才搞砸了。
标签: android listview nested textview