5
我試圖用行的自定義視圖構建一個列表,每行將包含一個圖像視圖和兩個文本視圖。ArrayAdapter數據集中元素的數量爲零
爲了做到這一點,我擴展了ArrayAdapter類(稱爲PostersArrayAdapter)並重寫了getView()方法,以便在數據和行佈局之間建立正確的連接。但是,當我嘗試用PosterData類(我的實現)的數組構造PostersArrayAdapter與一些數據結果是適配器是空的,意味着getCount()返回零,並且listView爲空。回到頂端這篇文章中的信息適用於:
任何人都可以建議我做錯了什麼? 我依靠的是我在這裏找到的代碼 - http://www.vogella.de/articles/AndroidListView/article.html
非常感謝!
這裏是有關的代碼:(PosterData類只是一個帶有兩個字符串域類)
public class PostersListActivity extends ListActivity {
final private int NUM_OF_PICS = 2;
private ContentGetter cg;
private PosterData[] posters;
private ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cg = new ContentGetter(NUM_OF_PICS);
try
{
posters = cg.parseIndexFile();
int res = cg.DownloadPosterPics(1);
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
// Use our own list adapter
listView = (ListView)findViewById(android.R.id.list);
//listView.setAdapter((ListAdapter) new ArrayAdapter<String>(this,R.layout.list_item,R.id.title,titles));
PostersArrayAdapter postersAdapter = new PostersArrayAdapter(this, posters);
Log.d("PostersListActivity.onCreate()","Number of elementes in the data set of the adapter is " + postersAdapter.getCount());
listView.setAdapter(postersAdapter);
}
public class PostersArrayAdapter extends ArrayAdapter<PosterData> {
private final Activity context;
private final PosterData[] posters;
public PostersArrayAdapter(Activity context, PosterData[] posters) {
super(context, R.layout.list_item);
this.context = context;
this.posters = posters;
}
// static to save the reference to the outer class and to avoid access to
// any members of the containing class
class ViewHolder {
public ImageView logo;
public TextView title;
public TextView names;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ViewHolder will buffer the assess to the individual fields of the row
// layout
ViewHolder holder;
// Recycle existing view if passed as parameter
// This will save memory and time on Android
// This only works if the base layout for all classes are the same
View rowView = convertView;
if (rowView == null)
{
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.list_item, null, true);
holder = new ViewHolder();
holder.title = (TextView) rowView.findViewById(R.id.title);
holder.names = (TextView) rowView.findViewById(R.id.names);
holder.logo = (ImageView) rowView.findViewById(R.id.logo);
rowView.setTag(holder);
}
else
{
holder = (ViewHolder) rowView.getTag();
}
holder.title.setText(posters[position].getTitle());
holder.names.setText(posters[position].getNames());
holder.logo.setImageResource(R.drawable.icon);
return rowView;
}
}
}
這裏是我使用的列表視圖佈局:
<?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/textView1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/selectPoster"
android:layout_gravity="center_horizontal">
</TextView>
<ListView android:id="@android:id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
這裏是我使用的列表元素佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout android:id="@+id/linearLayout1"
android:layout_height="match_parent"
android:layout_width="wrap_content" >
<ImageView android:id="@+id/logo"
android:src="@drawable/icon"
android:layout_height="wrap_content"
android:layout_width="22px"
android:layout_marginTop="4px"
android:layout_marginRight="4px"
android:layout_marginLeft="4px">
</ImageView>
<LinearLayout android:id="@+id/linearLayout2"
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/title"
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30px">
</TextView>
<TextView android:id="@+id/names"
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
那麼,現在它返回正確的數字。 更好的是,現在列表正確顯示(列表爲空之前)。 爲什麼重寫getCount方法解決了這個問題? –
我認爲這是因爲你沒有調用正確的超級構造函數 –
調用正確的函數可以防止重寫getCount? 你爲什麼認爲我沒有打電話給對方?什麼原理導致你得出這個結論? –