デフォルトでは要素を要素数に分割できないため、 BaseAdapterを使用する必要があるようです。ArrayAdapter
ArrayList
したがって、次のようになります。
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;
}
}
}
6 つのアイテムでグリッドを生成します。詳細については、対応するGrid View の Android ガイド を参照してください。