如何在Android中驱动gridview?
我写了以下内容,将数据显示到网格视图中。这是我的程序代码 在onCreate()中 最后,这是我的“新闻适配器”: “网格新闻”的格式是这样的如何在Android中驱动gridview?,android,gridview,adapter,Android,Gridview,Adapter,我写了以下内容,将数据显示到网格视图中。这是我的程序代码 在onCreate()中 最后,这是我的“新闻适配器”: “网格新闻”的格式是这样的 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/news_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/menu_contentdescription"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/news_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="15sp"
android:textStyle="bold"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingBottom="10dip" />
</RelativeLayout>
我不知道为什么在模拟器/设备中看不到结果。我想这是因为getCount()返回0;它不应该是itemList.size()吗?在新闻适配器中。Yap,完全正确。天哪,两个小时后你就成功了。非常感谢。
@Override
protected void onStart() {
super.onStart();
String strNewsContent = "***";
...
newsAdapter.setData(parsedList);
newsAdapter.notifyDataSetChanged();
}
public class NewsAdapter extends BaseAdapter {
private LayoutInflater myInflater;
private NewsFeedItemList itemList;
public NewsAdapter(Context c) {
myInflater = LayoutInflater.from(c);
}
public void setData(NewsFeedItemList newsFeedItemList){
itemList = newsFeedItemList;
Log.i("NewsAdapter", "Parsed list passed to the adapter.");
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
if (convertView == null) {
convertView = myInflater.inflate(R.layout.grid_news, null);
holder = new ViewHolder();
holder.ivIcon = (TextView) convertView.findViewById(R.id.news_icon);
holder.tvLabel = (TextView) convertView.findViewById(R.id.news_label);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ivIcon.setText(itemList.getImage().get(position));
holder.tvLabel.setText(itemList.getTitle().get(position));
Log.i("Position is>>>>:", Integer.toString(position));
return convertView;
}
static class ViewHolder {
TextView ivIcon;
TextView tvLabel;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/news_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/menu_contentdescription"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/news_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="15sp"
android:textStyle="bold"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingBottom="10dip" />
</RelativeLayout>