0
我寫下如下顯示我的數據到網格視圖。這是我的代碼的過程。Android,如何驅動gridview?
在的onCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newslist);
// Initializing NewsAdapter in order to loading the list of NEWS items
newsAdapter = new NewsAdapter(this);
// Initializing grid view to show news
gridView = (GridView) findViewById(R.id.news_gridview);
gridView.setAdapter(newsAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(NewsList.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
然後在調用onStart()我會從服務器獲取XML數據,然後分析它,並最終解析的數據將存儲在「parsedList」(100%我有硫數據因爲我在logcat中看到它)。
@Override
protected void onStart() {
super.onStart();
String strNewsContent = "***";
...
newsAdapter.setData(parsedList);
newsAdapter.notifyDataSetChanged();
}
終於這是我的 「NewsAdapter」:
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;
}
}
「grid_news」 的格式是這樣
<?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>
我不知道爲什麼我看不到結果在我的模擬器/設備! 如果有人能幫助我,我會非常感激。
Yap,是正確的。上,我的上帝,2小時後,你做到了。謝謝。 – Hesam 2012-03-08 07:48:05