9

I want to display Arraylist items in Gridview. My Arraylist is like this :

1 Hello Hello

2 Hello Hello

If I bind it to a gridview control like this :

  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, event_list);

  gridview.setAdapter(adapter);

event_List is my Arraylist. Through this approach I get a complete Arraylist row or record in a cell of gridview . I want to display each item like "Hello" of Arraylist in each cell of gridview. Like 1 in one cell , "Hello" in another cell and so on.

Thanks in advance

5
  • Why not use BaseAdapter? It would give You full control in small cost of 3-4 additional functions. Is Your ArrayList item complete string "1 Hello Hello"? Commented Jul 2, 2013 at 4:18
  • It is like this row = cn.getID()+" , " + cn.getMessage() + " , " + cn.getTime(); event_list.add(row); Commented Jul 2, 2013 at 4:21
  • I can't figure out , how BaseAdapter works ? Commented Jul 2, 2013 at 4:29
  • use custom adapter for grid view stackoverflow.com/questions/12952265/… reference Commented Jul 2, 2013 at 4:46
  • You have to use custom Grid view. refer this link saurabhsharma123k.blogspot.in/2012/09/android-grid-view.html Commented Jul 2, 2013 at 4:56

2 Answers 2

14

Seems You need to use BaseAdapter, because default ArrayAdapter is not able to accomplish dividing of ArrayList element into number of elements.

So, it would look like the following:

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final GridView grid = (GridView) findViewById(R.id.gridView);
        final ArrayList<String> items = new ArrayList<String>();

        items.add("1 , Hello11 , Hello12");
        items.add("2 , Hello21 , Hello22");

        grid.setAdapter(new GridAdapter(items));
    }

    // Assume it's known
    private static final int ROW_ITEMS = 3;

    private static final class GridAdapter extends BaseAdapter {

        final ArrayList<String> mItems;
        final int mCount;

        /**
         * Default constructor
         * @param items to fill data to
         */
        private GridAdapter(final ArrayList<String> items) {

            mCount = items.size() * ROW_ITEMS;
            mItems = new ArrayList<String>(mCount);

            // for small size of items it's ok to do it here, sync way
            for (String item : items) {
                // get separate string parts, divided by ,
                final String[] parts = item.split(",");

                // remove spaces from parts
                for (String part : parts) {
                    part.replace(" ", "");
                    mItems.add(part);
                }
            }
        }

        @Override
        public int getCount() {
            return mCount;
        }

        @Override
        public Object getItem(final int position) {
            return mItems.get(position);
        }

        @Override
        public long getItemId(final int position) {
            return position;
        }

        @Override
        public View getView(final int position, final View convertView, final ViewGroup parent) {

            View view = convertView;

            if (view == null) {
                view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
            }

            final TextView text = (TextView) view.findViewById(android.R.id.text1);

            text.setText(mItems.get(position));

            return view;
        }
    }
}

Will produce grid with six items. See more in corresponding Android Guide for Grid View.

Sign up to request clarification or add additional context in comments.

3 Comments

Why declare this adapter subclass as private final?
@learnenburn there's no reason that anybody outside might need to access it, so private. Also, this class is not designed for inheritance - so final. Usually it's a good practice, partially based on Effective Java (don't remember exact items from the book).
@RonakUpadhyay set new data and call notifyDataSetChanged
2

Or you maybe use this:

MainActivity.java:

import android.os.Bundle;
import android.app.Activity;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    List<String> values=new ArrayList<String>();
    values.add("Home");values.add("About");values.add("Contact");values.add("Help");values.add("Index");
    values.add("Home");values.add("About");values.add("Contact");values.add("Help");values.add("Index");
    GridView myGrid=(GridView)findViewById(R.id.grid);

    myGrid.setAdapter(new ArrayAdapter<String>(this,R.layout.cell,values));

   }
}

Your activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/mySelection" />
<GridView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/grid"
    android:columnWidth="75dip"
    android:gravity="center"
    android:horizontalSpacing="5dip"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="75dip">

</GridView>

Create a new xml file in layout folder and name it cell.xml and put this in it:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp">

Now run and enjoy. This is output:

Hooray

3 Comments

It is maybe not what you exactly want. but it is an answer to the title of your question.
It's working like this. But i wonder why. Where is the array bound to the textview ? Are Textviews automatically chosen vor Strings ?
it is bound here: [myGrid.setAdapter(new ArrayAdapter<String>(this,R.layout.cell,values));]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.