0

So I realised that I cant put other elements into an android gridview and I cant put the gridview into a scrollview, so I attempted to stick a gridview into an android listview with only 1 listview column to see if I could make that work, this was successful, however when I try to inflate the gridview that is inside the list I keep getting a nullpointerexception where I set the actual grid adapter, Is there another way around this hack?

package com.listtest.testit;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

public class ListTestActivity extends Activity {

ViewGroup footer;
LayoutInflater inflater;
List<String> theStrings = new ArrayList<String>();
ListAdapter adapter;
ListView list;
GridView grid;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    theStrings.add("");

    String[] numbers = new String[] { 
            "A", "B", "C", "D", "E",
            "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O",
            "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z"};

    list = (ListView) findViewById(R.id.list);

    inflater = getLayoutInflater();
    footer = (ViewGroup) inflater.inflate(R.layout.footer, list, false);
    list.addFooterView(footer, null, false);

    adapter = new ListAdapter(this, theStrings);
    list.setAdapter(adapter);

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

    grid.setAdapter(gridAdapter);

    list.setOnScrollListener(new OnScrollListener() {
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // Do nothing
        }

        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {

            final int lastItem = firstVisibleItem + visibleItemCount;

            // threshold being indicator if bottom of list is hit
            if (lastItem == totalItemCount) {

                //addMoreToArray();
                //adapter.notifyDataSetChanged();
            }
        }
    });
}

public class ListAdapter extends BaseAdapter {

    private Activity activity;
    private List<String> data;
    private LayoutInflater inflater=null;


    public ListAdapter(Activity a, List<String> theStrings) {
        activity = a;
        data=theStrings;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.item, null);

        grid=(GridView)vi.findViewById(R.id.gridView1);

        return vi;
    }
}

private void addMoreToArray() {

    theStrings.add("");

}

}
Edmund Rojas
  • 6,376
  • 16
  • 61
  • 92
  • This is interesting, but I'm curious to know why you would want to put a gridview inside a listview? – Jade Byfield Oct 20 '12 at 17:31
  • I want to add a header to the gridview and a footer, but this cant be done with the gridview from what I see so Im trying to make a workaround, I notice many apps such as instagram for example have figured out a way to do this but this is the closest thing to it I can think of – Edmund Rojas Oct 20 '12 at 18:17
  • you should be doing it like this. if you are trying to make a grid of things inside a listview, you should use something like a GridLayout. always avoid nesting scrollable containers – toadzky Oct 20 '12 at 18:36
  • @toadzky the problem is if you place it in a linear layout the entire screen doesnt scroll, you get a half page of one view and then the gridview takes up the other half, if you use instagram for example you will notice that on the profile page that the all the views on this screen scroll including the gridview, similar to the effect you would get by putting things into a scrollview – Edmund Rojas Oct 20 '12 at 18:40
  • you of course wrap the linear layout in a scrollview. scrollviews only take a single child, which is often a linear layout that is filled with the actual content. – toadzky Oct 20 '12 at 18:56
  • Oh my god you have got to be kidding me! of course it would be that simple, thats is literally the only thing I did not try wow lol – Edmund Rojas Oct 20 '12 at 19:08
  • @toadzky that solution didnt work either, the gridview wont show up at all – Edmund Rojas Oct 20 '12 at 20:26
  • can you post a screen shot that shows what you are trying to do? – toadzky Oct 21 '12 at 01:01
  • Here is what I'm attempting to do, I'd prefer to do it in just xml but I figured this might be something that I'd have to hack stackoverflow.com/questions/12992853/how-to-add-a-scrollable-header-to-a-gridview-in-android#comment17626126_12992853 – Edmund Rojas Oct 21 '12 at 02:21

1 Answers1

1

It says what you are trying is not good approach, although he introduces how he managed gird view in list view.

Here's it says.... The problem here is that, both GridView and ListView are scrollable themselves, and it is not a good approach to embed a scrollable container inside another scrollable container.


Google's Market App and Google Music App is using List View only. No Scroll view!(I checked with hierarchy view). Using only a list view would be a little bit bothering. Because getView() would be more complicated. But it certainly works well as you wish(I finally succeed with this way).

theWook
  • 843
  • 9
  • 22