尝试创建一个由两个 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 有
... TextView
... ListView
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 页上的一个类似示例,但失败失败失败...
有好心人能给我指出正确的方向吗?
我必须膨胀这个嵌套 View 吗? (Reto 的例子确实如此)。如果是这样,我错过了什么?我希望有人能给我指出一个更好的例子。我的代码此时可能过于复杂,无法发布并且有点专有。
谢谢
主.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 正确,将会帮助很多。
最佳答案
在您展开布局的地方,您可以在正在展开的布局上使用 findViewById,因为我不知道您的代码是什么样的,这里是我将要执行的操作的示例:
LayoutInflater li = LayoutInflater.from(mContext);
LinearLayout mLayout = (LinearLayout) li.inflate(R.layout.stuff,null);
View mView = mLayout.findViewById(R.id.firstLine);
您可能希望您的 null 成为您希望膨胀布局拥有的父级。希望这对您有所帮助!
更新
我会尝试将您拥有的替换为:
thingView= new LinearLayout(getContext());
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View nuView = vi.inflate(resource, thingView, true);
与:
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
thingView = vi.inflate(R.layout.thing_item, null);
因为您似乎无缘无故地制作了一个新的线性布局,因为您的通货膨胀已经从您的 xml 文件中获得了线性布局。这几乎就是我上面所说的,这对我有用:)
关于android - 尝试为嵌套的 TextView(在 ListView 中)找到 findViewById 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3376678/