我的应用中有Custom list view
,显示parsed JSON content
。现在我必须从list view
搜索数据。我看到了一些类似我的问题。
Android parsed json data and add a search functionality
Implementing SearchView in action bar
但我不知道如何利用它。所以我看到Official Docs并在我的Activity中更改了一些,并在清单中添加了Meta data
并创建了一个可搜索的xml。
但我不知道如何从Listview或解析的JSON数据中获取数据。
我在我的应用程序中使用Material Design。如果搜索界面中有任何新内容。请告诉我。
提前致谢!
public class MainActivity extends ActionBarActivity implements NavigationDrawerCallbacks{
private static final String TAG = "Mine";
private Toolbar mToolbar;
private NavigationDrawerFragment mNavigationDrawerFragment;
public ListView listView;
private FeedListAdapter listAdapter;
private List<FeedItem> feedItems;
public String mvideoid;
public String mstatus;
ProgressBarCircularIndetermininate progressBar;
private String URL_FEED = "http://arulnadhan.hol.es/vid.json";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
listView = (ListView) findViewById(R.id.feed_list);
feedItems = new ArrayList<FeedItem>();
listAdapter = new FeedListAdapter(this, feedItems);
listView.setAdapter(listAdapter);
// making fresh volley request and getting json
GsonRequest<FeedResult> gsonRequest = new GsonRequest<FeedResult>(URL_FEED, FeedResult.class,
new Response.Listener<FeedResult>() {
@Override
public void onResponse(FeedResult response) {
feedItems = response.getFeedItems();
listAdapter.setData(feedItems);
listAdapter.notifyDataSetChanged();
progressBar.setVisibility(View.INVISIBLE);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error: " + error.getMessage());
}
});
// Adding request to volley request queue
AppController.getInstance().addRequest(gsonRequest, TAG);
}
private void parseJsonFeed(JSONObject response) {
try {
JSONArray feedArray = response.getJSONArray("feed");
for (int i = 0; i < feedArray.length(); i++) {
JSONObject feedObj = (JSONObject) feedArray.get(i);
FeedItem item = new FeedItem();
item.setId(feedObj.getInt("id"));
String name = feedObj.isNull("name") ? null : feedObj
.getString("name");
item.setName(name);
// Image might be null sometimes
String image = feedObj.isNull("image") ? null : feedObj
.getString("image");
item.setImageUrl(image);
mstatus=feedObj.getString("status");
item.setStatus(mstatus);
// videoid might be null sometimes
mvideoid = feedObj.isNull("videoid") ? null : feedObj
.getString("videoid");
item.setVideoid(mvideoid);
feedItems.add(item);
}
// notify data changes to list adapater
listAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView search = (SearchView) menu.findItem(R.id.search).getActionView();
search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
@Override
public void onNavigationDrawerItemSelected(int position) {
if (position == 1){
FeedFragment Frag = new FeedFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, Frag);
transaction.commit();
}else if (position == 2){
VideoFragment Frag = new VideoFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, Frag);
transaction.commit();
}
}