想有做了兩個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權利將有助於一堆。
您是否包含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
如果你的意思是「你在main.xml中包含對stuff.xml的引用「?不。它可能很簡單嗎? – teachableMe 2010-07-31 04:15:47
「是的,我做了setContentView(R.layout.main); onCreate之後。」你的意思是你在你的onCreate方法中,在調用super.onCreate之後? – MatrixFrog 2010-07-31 05:20:39