Search code examples
androidgridviewadapter

How to drive gridview in Android?


I have written following to show my data into grid view. This is the procedure of my code.

In 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();
            }
        });

Then in onStart() I'll get XML data from server then parse it and final parsed data will be stored in "parsedList" (100% I have data in this variable because I saw it in logcat).

@Override
    protected void onStart() {
        super.onStart();
        String strNewsContent = "***";

        ...

        newsAdapter.setData(parsedList);
        newsAdapter.notifyDataSetChanged();
    }

Finally this is my "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;
    }

}

The format of "grid_news" is like this

<?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>

I have no idea why I can't see the result in my emulator/device.


Solution

  • I think it is because of getCount() returning 0; shouldn't it be itemList.size()? in news NewsAdapter.