Essayant d'avoir une ListView (liste déroulante) de lignes composée de deux TextViews. J'ai une liste d'éléments d'une base de données que je veux peupler dans LinearLayout-> ListView-> TextView, mais ne peut pas obtenir à l'id ...Échec lors de la tentative de recherche de ViewById pour TextView imbriqué (dans ListView)
Mise en page un peu comme ce lien d'instruction, mais ont reculé de RelativeLayout et en utilisant LinearLayout pour le faire fonctionner. Pas même inquiet de la façon dont il semble encore; Je n'arrive tout simplement pas à le brancher ensemble.
http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html
deux fichiers XML (détails ci-dessous) très abrégés main.xml et stuff.xml
main.xml a
... TextView
... ListView
stuff.xml possède
... android: id = "@ + id/firstLine"
... android: id = "@ + id/seconde ligne"
OUI, Je fais setContentView (R.layout.main); juste après onCreate.
Obtention de null sur findViewByID (firstLine) et findViewID (secondLine).
J'ai un ArrayAdapter où je gonfle le stuffView. Ma pensée et ma compréhension des autres exemples sont que ce n'est pas gonflé (ce truc imbriqué) jusqu'à ce que je le gonfle volontairement. Tout cela fonctionne bien mais quand je fais le findViewById il renvoie null et donc je ne peux pas setText().
epic Échec dû à l'ignorance complète/newbieness de ma part. Note: J'ai passé en revue ce que je peux du livre de Reto, en particulier un exemple simliar sur la page 163, mais l'échec échouera ...
Est-ce qu'une âme aimable peut me diriger dans la bonne direction?
Dois-je gonfler cette vue imbriquée? (L'exemple de Reto fait). Si oui, qu'est-ce qui me manque? J'espère que quelqu'un peut me montrer un meilleur exemple. Mon code est probablement trop impliqué à ce stade pour poster et un peu propriétaire.
Merci
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>
Un POJO appelé Thingy (ne pas copier Thingy.java ici - très simple)
La principale 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();
}
}
L'adaptateur remplace la classe 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;
}
}
Donc, ce code est essentiellement ce que je cherche de l'aide sur; neutralisé les noms pour l'appeler Thingy .... Cet exemple ne déclenche pas le getView(). C'est un problème secondaire que je dois régler. Plus important encore, votre aide sur l'échec de findViewById et si j'ai le bon XML aiderait beaucoup.
Incluez-vous le stuff.xml de main.xml? par exemple. http://www.curious-creature.org/2009/02/25/android-layout-trick-2-include-to-reuse/ – I82Much
Si vous voulez dire "est-ce que vous incluez une référence à stuff.xml dans main.xml "? Nan. Cela pourrait-il être aussi simple? – teachableMe
"OUI, je fais setContentView (R.layout.main), juste après onCreate." Voulez-vous dire que vous le faites dans votre méthode onCreate, juste après l'appel à super.onCreate? – MatrixFrog