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

4

2 回答 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.

于 2013-07-02T04:50:31.167 回答
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

于 2016-05-11T15:44:39.817 回答