0

So I'm trying to create a GridView style layout in Android without actually using a custom Gridview adapter, since I don't want it to scroll. I've tried just turning scrolling off, but it ruins my layout since I'm adding other elements around it in a vertical LinearLayout.

My next experiment was to use a TableLayout and then just add inflated layouts as table cells, but I'm also having an issue with this. Here is a test that I am running for a brief proof of concept:

TableRow trackingActivityRow = new TableRow(getActivity());
        for(int j = 0; j < trackingActivities.size(); j ++) {

            TrackingActivity trackingActivity = trackingActivities.get(j);
            View trackingActivityCell = getActivity().getLayoutInflater()
                    .inflate(R.layout.table_cell_tracking_activity, trackingActivityRow, true);
            TextView txtDescription = (TextView)trackingActivityCell.findViewById(R.id.txtDescription);
            txtDescription.setText(trackingActivity.getDescription());



        }
        tableLayout.addView(trackingActivityRow);

It seems to create the number of cells correctly, but it doesn't want to set the text like it should be. Furthermore, I'm having an issue of logic when it comes to creating a new row for every 4 TrackingActivities. If anyone has any input it would be appreciated.

/Update/ Here is a graphic of the issue. The cell with "Walk" in it is displaying correctly, but the other cells only display the placeholder text inside the textview which should have been replaced.

enter image description here

jwBurnside
  • 849
  • 4
  • 31
  • 66
  • You're saying that the textview isn't appearing properly? If so in what way? Snapshots of the same would help – humblerookie Jul 10 '14 at 17:25
  • *but it doesn't want to set the text like it should be.* - meaning what? *...creating a new row for every 4 TrackingActivities..* - you'd need to create the row inside the for loop for every `j` multiple of `4`(also add the row to the table in the for loop). – user Jul 10 '14 at 17:25
  • When you inflate that layout the last parameter is set to true, which means that the layout should be inflated **and** also added to the parent(`trackingActivityRow`). In this case the returned view will actually be the parent(`trackingActivityRow`), meaning that you look for the `TextView` in the `TableRow`, always(and only) setting the text in the first cell(`findViewById()` returns the first view occurrence). Use `false` for the `inflate()` method. – user Jul 10 '14 at 17:47
  • @Luksprog - I changed the value to false, but no cells are displayed – jwBurnside Jul 10 '14 at 17:57
  • If you use `false`, you'd need to manually add the cells: `trackingActivityRow.addView(trackingActivityCell)` in the for loop. – user Jul 10 '14 at 17:59
  • I attempted this in another SO question: http://stackoverflow.com/questions/24661688/ – jwBurnside Jul 10 '14 at 18:00
  • What I said above should work with no problems. – user Jul 10 '14 at 18:11

1 Answers1

0

I created a customized GridView based on a tablelayout with a listadapter.

Simplified version of the setAdapterMethod in the extended TableLayout:

    int itemPos = -1;
    int numRows = (int) Math.ceil((double) adapter.getCount() / (double) mNumColumns);
    for (int yPos = 0; yPos < numRows; yPos++) {
        //Create new row
        TableRow tableRow = new TableRow(this.getContext());

        for (int xPos = 0; xPos < mNumColumns; xPos++) {
            itemPos++;
            View itemView = adapter.getView(itemPos, null, tableRow);
            tableRow.addView(itemView);
        }
        this.addView(tableRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
    }