2010-07-31 46 views
1

想有做了两个TextViews行的一个ListView(滚动列表)(内的ListView)。我有一个数据库中的项目列表,我想要填充到LinearLayout-> ListView-> TextView中,但无法获得ID ...失败的尝试findViewById嵌套的TextView

布局有点像这个教学链接,但已退出使用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 has

...机器人:ID = “@ + ID/firstLine中”

... 机器人:ID = “@ + ID /二线”

YES,我做setContentView(R.layout.main);在onCreate之后。

获得零上findViewByID(firstLine中)和findViewID(二线)。

我有,我膨胀stuffView一个ArrayAdapter。我对其他例子的思考和理解是它没有膨胀(这个嵌套stuffView),直到我故意夸大它。这一切工作正常,但当我做findViewById它返回null,因此我不能setText()。

史诗失败的原因完全在我的部分无知/ newbieness。注:我已经通过我所能雷托的书,尤其是simliar例如钻研上页163,但失败失败失败...

某种灵魂可以点我在正确的方向?

我必须膨胀这个嵌套视图吗? (雷托的例子呢)。如果是这样,我错过了什么?我希望有人能指点我一个更好的例子。我的代码可能在这一点上过于涉及发布和专有。

感谢

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> 

称为啄一个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权利将有助于一堆。

+0

您是否包含main.xml中的stuff.xml?例如http://www.curious-creature.org/2009/02/25/android-layout-trick-2-include-to-reuse/ – I82Much 2010-07-31 03:39:12

+0

如果你的意思是“你在main.xml中包含对stuff.xml的引用“?不。它可能很简单吗? – teachableMe 2010-07-31 04:15:47

+0

“是的,我做了setContentView(R.layout.main); onCreate之后。”你的意思是你在你的onCreate方法中,在调用super.onCreate之后? – MatrixFrog 2010-07-31 05:20:39

回答

1

如果你正在膨胀的布局,你可以在你的膨胀布局使用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); 

你似乎做无故新的LinearLayout,如您的通货膨胀将已经从您的xml文件中获得线性布局。这几乎是我上面所说的,这对我有用:)

+0

是的,我已经有了膨胀,虽然做了一点不同。所有的电话都在工作(或者他们似乎是这样),即:mLayout返回一个值。这只是调用findViewById,它返回null。所以在你上面的例子中,mView == null。 – teachableMe 2010-08-02 01:31:10

+0

那么你有它的工作? – stealthcopter 2010-08-02 11:50:14

+0

nope;我把这件事解决掉,以便我可以发布代码。我想,如果没有一些具体的代码,人们很难帮助。 – teachableMe 2010-08-04 04:03:37