我正在尝试构建一个包含行的自定义视图的列表,每行包含一个图像视图和两个文本视图.
为了做到这一点,我扩展了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 ArrayAdapterlogo;
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;
}
}
}
这是我正在使用的列表视图布局:
这是我正在使用的列表元素布局:
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">
最佳答案