W
W
whoami?root root_toor2016-04-03 17:09:32
Java
whoami?root root_toor, 2016-04-03 17:09:32

Android SearchDialog How to load data into the same Activity/GridView?

There is an application that uses the standard Android Search Dialog:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gridview);

        mGridView = (GridView) findViewById(R.id.gridView);
        mProgressBar = (ProgressBar) findViewById(R.id.progressBar);

        // Search Dialog
        GridViewActivity.this.onSearchRequested();
        // Intent
        Intent intent = getIntent();
        // Check Intent type
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            // Get search string from extra
            String query = intent.getStringExtra(SearchManager.QUERY);
            // Search
            Log.d(TAG, query);
            showResults(query);
        }

        //Scroll listener
        mGridView.setOnScrollListener(ScrollListener);

        //Initialize with empty data
        mGridData = new ArrayList<>();
        mGridAdapter = new GridViewAdapter(this, R.layout.grid_item_layout, mGridData);
        mGridView.setAdapter(mGridAdapter);

       // ...
       showResults(); // здесь надо обновить GridView

      // далее добавляю данные так:
      protected void onPostExecute(Integer result) {
            // Download complete. Lets update UI
            if (result == 1) {
                mGridAdapter.setGridData(mGridData);
                mGridAdapter.notifyDataSetChanged();

                // Search Dialog 
                GridViewActivity.this.onSearchRequested();
            } else { Log.d(TAG, "result == 0" ); }
     }

The application is based on ActionBarActivity. Consists of a GridView and an adapter for adding new items. SearchDialog like works, catches requests.
BUT! Each time you enter a request into the dialog, a new "window" opens, activity ... I don't know what to call it .. i.e. the new data does not replace the items in the created GridView, but it seems like a new GridView is being created. and when the back button is pressed, the GridView with the new items closes and the old GridView with the old items is shown.
Simply put: each new search adds a window with new results, and the old data remains in memory and is shown when the back button is pressed.
How to fix it? How can I make new data show up in the same GridView by redrawing it.
Adapter code:
public class GridViewAdapter extends ArrayAdapter<GridItem> {

    private Context mContext;
    private int layoutResourceId;
    private ArrayList<GridItem> mGridData = new ArrayList<GridItem>();

    public GridViewAdapter(Context mContext, int layoutResourceId, ArrayList<GridItem> mGridData) {
        super(mContext, layoutResourceId, mGridData);
        this.layoutResourceId = layoutResourceId;
        this.mContext = mContext;
        this.mGridData = mGridData;
    }


    /**
     * Updates grid data and refresh grid items.
     *
     * @param mGridData
     */
    public void setGridData(ArrayList<GridItem> mGridData) {
        this.mGridData = mGridData;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder;

        if (row == null) {
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.titleTextView = (TextView) row.findViewById(R.id.grid_item_title);
            holder.imageView = (ImageView) row.findViewById(R.id.grid_item_image);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        GridItem item = mGridData.get(position);
        holder.titleTextView.setText(Html.fromHtml(item.getTitle()));

        Picasso.with(mContext).load(item.getImage()).into(holder.imageView);
        return row;
    }

    static class ViewHolder {
        TextView titleTextView;
        ImageView imageView;
    }
}

And another small question: I process the scroll like this:
GridView.OnScrollListener ScrollListener =  new AbsListView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }

        @Override
        public void onScroll(AbsListView view, int firstVisible, int visibleCount, int totalCount) {
            LoadMore = firstVisible + visibleCount >= totalCount;
            //need to load more items
            if (LoadMore && finished) {
                updateView(REQUEST, totalCount);
            }
        }

    };

but, as I understand it, onScroll is called both when creating the grid and when scrolling itself several times. My program should receive data from the Internet and display it in the grid, but with multiple onScroll calls, the data update function and the GridView itself are called several times, which is not good. I want only the part that should be displayed now to be loaded: firstVisible + visibleCount, without unnecessary data loads. Maybe some advice on this? Thank you all in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
whoami?root root_toor, 2016-04-03
@1q2w1q2w

I found a solution: it
is added to the manifest
and in the activity we catch the Intent:

@Override
    public void onNewIntent(Intent intent) {
        String query = null;
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            query = intent.getStringExtra(SearchManager.QUERY);
            Log.i(TAG, "Received a new search query: " + query);
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question