Answer the question
In order to leave comments, you need to log in
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" ); }
}
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;
}
}
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);
}
}
};
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question