두 개의 TextView로 구성된 행의 ListView (스크롤 가능 목록)를 만들려고합니다. LinearLayout-> ListView-> TextView에 채울 데이터베이스의 항목 목록을 가지고 있지만 ID를 얻을 수는 없습니다 ...중첩 TextView (ListView 내)에 대해 FindById를 시도하지 못했습니다.
레이아웃은이 지침 링크와 다소 비슷하지만 멀리 떨어져 있습니다. RelativeLayout 및 LinearLayout을 사용하여 작동 시키십시오. 그것이 아직 어떻게 보이는지에 대해서조차 걱정하지 않는다; 아직 연결되지 않습니다.
http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html
두 개의 XML 파일 (매우 약식 자세한 내용은 아래 참조) main.xml에와 stuff.xml을 가지고
main.xml에가 ...
텍스트 뷰
을 을 가지고 ... ListViewstuff.xml은
,... 로이드 : ID = "@ + ID/firstLine의"
... 로이드 : ID = "@ + ID/secondLine"
YES, 내가 할 setContentView (R.layout.main); onCreate 직후.
findViewByID (firstLine) 및 findViewID (secondLine)에서 null을 얻습니다.
나는 stuffView를 부 풀리는 ArrayAdapter가 있습니다. 다른 예제의 생각과 이해는 의도적으로 팽창시킬 때까지 팽창하지 않습니다 (중첩 된 stuffView). 그 모든 잘 작동하지만 findViewById 할 때 null을 반환하고 따라서 setText() 수 없습니다.
서사시 내 부분의 완전한 무지 또는 신입으로 인해 실패했습니다. 참고 : 내가 Reto의 책에서 할 수있는 것, 특히 simliar 예를 들었지만 실패 실패는 실패했습니다.
어떤 종류의 영혼이 올바른 방향으로 나를 가리킬 수 있습니까?
이 중첩 된보기를 팽창해야합니까? (레토의 예는 그렇다). 그렇다면 무엇을 놓치고 있습니까? 나는 누군가가 나를 더 나은 모범으로 바라 보길 바란다. 내 코드는이 시점에서 게시하고 약간 독점적이라고 할 수 있습니다.
감사
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를 복사하지 - 매우 간단)
주에게 class : 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()를 트리거하지 않습니다. 그건 내가 배제해야 할 2 차적 문제이다. 더 중요한 것은 findViewById 오류에 대한 도움과 XML을 올바르게 사용하면 도움이 될 것입니다.
main.xml의 stuff.xml을 포함하고 있습니까? 예 : http://www.curious-creature.org/2009/02/25/android-layout-trick-2-include-to-reuse/ – I82Much
"당신은 main.xml 내에 stuff.xml에 대한 참조를 포함하고 있습니까? "? 아니. 그렇게 간단 할까? – teachableMe
"예, setContentView (R.layout.main); onCreate 직후입니다." superCondate를 호출 한 직후에 onCreate 메소드에서 수행한다는 것을 의미합니까? – MatrixFrog