2010-07-31 74 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