Listview nicht immer aufgefüllt, getView() nicht immer aufgerufen
Ich habe da ein paar Probleme meine listview zu zeigen, bis in eine einfache Anwendung, die ich Schreibe. Für einige Grund, die getView()
Methode meiner adapter-Klasse ist nicht immer genannt. Jedoch, wenn, wenn getCount()
heißt in meiner adapter-Klasse ist es nicht 0 zurückgeben, sondern ist in der Tat die Rückgabe der richtige Wert. Ich bin wirklich verwirrt, warum dies nicht funktioniert.
Hier ist meine Aktivität:
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class CardListActivity extends Activity {
protected ListView lv;
protected int nCards = 12;
protected String[] cardList = new String[nCards];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cardlist);
this.loadCardStrings();
lv = (ListView) findViewById(R.id.CardList_ListView);
EditorListAdapter adapter = new EditorListAdapter(this, this.cardList);
lv.setAdapter(adapter);
}
protected void loadCardStrings(){
cardList = getResources().getStringArray(R.array.card_list);
Util.logD("Created the string arrays with:" + Integer.toString(cardList.length) + " items.");
}
public void onStart(){
super.onStart();
}
public void onResume(){
super.onResume();
}
public void onPause(){
super.onPause();
}
public void onStop(){
super.onStop();
}
public void onDestroy(){
super.onDestroy();
}
}
Hier ist das layout von der Aktivität verwendet:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/CardList_LinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hello"/>
<ListView android:id="@+id/CardList_ListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Schließlich ist hier meine adapter-Klasse:
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class EditorListAdapter extends BaseAdapter
{
Activity context;
String names[];
public EditorListAdapter(Activity context, String[] title) {
super();
this.context = context;
this.names = title;
}
public int getCount() {
Util.logD("EditorListAdapter.getCount() ret:" + Integer.toString(names.length));
return names.length;
}
public Object getItem(int position) {
Util.logD("EditorListAdapter.getItem()");
return null;
}
public long getItemId(int position) {
Util.logD("EditorListAdapter.getItemId()");
return 0;
}
public View getView(int position, View convertView, ViewGroup parent)
{
TextView t;
if (convertView == null)
t = new TextView(parent.getContext());
else
t = (TextView) convertView;
t.setText(this.names[position]);
convertView = t;
Util.logD("EditorListAdapter.getView() + " + t.getText().toString());
return convertView;
}
//Added per K-Ballo suggestion
public int getViewTypeCount(){
return 1;
}
public int getItemViewType(){
return 0;
}
}
InformationsquelleAutor der Frage slayton | 2011-09-12
Du musst angemeldet sein, um einen Kommentar abzugeben.
Versuchen Sie, die
android:layout_height="fill_parent"
für den textview zuwrap_content
statt. Ich glaube, die listview wird nie angezeigt, also es gibt nie eine Notwendigkeit zu nennen getView()InformationsquelleAutor der Antwort Sashi Kolli
Ich weiß, es gibt bereits eine Antwort auf diese Frage.
Aber ich hatte eine andere Ursache und Grund, mit ähnlichen symptom, um die ursprüngliche Frage (getView() nicht aufgerufen), daher denke ich, dass es vorteilhaft ist, zu dokumentieren, hier für andere Menschen mit einer anderen Ursache.
Als gegeben von http://hissain.in/wordpress/?p=659
“Manchmal scheint es notifyDataSetChanged() funktioniert nicht für Sie (und damit getView() wird nicht aufgerufen). Der Grund dafür könnte sein, dass Ihr adapter verliert Referenz auf Ihrer Liste. Beim ersten initialisieren des Adapters dauert es eine Referenz des array-Liste und gehen zu seinen Superklasse. Aber wenn Sie initialisieren Sie Ihre vorhandene array-Liste verliert die Referenz daher der Kommunikationskanal mit Adapter"
Daher nie neu initialisieren Ihrer Liste, sondern mit clear() auf der Liste.
InformationsquelleAutor der Antwort Lawrence Teo