2012-03-08 23 views
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> 

我不知道为什么我看不到结果在我的模拟器/设备! 如果有人能帮助我,我会非常感激。

回答

2

我认为这是因为getCount()返回0;不应该是itemList.size()?在新闻NewsAdapter。

+0

Yap,是正确的。上,我的上帝,2小时后,你做到了。谢谢。 – Hesam 2012-03-08 07:48:05