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

7
  • You're saying that the textview isn't appearing properly? If so in what way? Snapshots of the same would help Commented Jul 10, 2014 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). Commented Jul 10, 2014 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. Commented Jul 10, 2014 at 17:47
  • @Luksprog - I changed the value to false, but no cells are displayed Commented Jul 10, 2014 at 17:57
  • If you use false, you'd need to manually add the cells: trackingActivityRow.addView(trackingActivityCell) in the for loop. Commented Jul 10, 2014 at 17:59

1 Answer 1

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));
    }
Sign up to request clarification or add additional context in comments.

Comments

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.