我正在创建一个将JSON数据解析为listview的应用程序。我还想在工具栏中添加一个搜索对话框。但我不知道如何实现这一目标。我检查了the documentation,但我仍然卡住了。所以,任何想法,任何建议都会有所帮助。
这是我的MainActivity.java
public class MainActivity extends AppCompatActivity implements LoaderCallbacks<List<PlayerDetails>> {
public static final String LOG_TAG = MainActivity.class.getName();
private static final int PLAYER_ID = 1;
private static final String PLAYER_DETAILS_URL =
"http://hackerearth.0x10.info/api/gyanmatrix?type=json&query=list_player";
private ListView playerListView;
private PlayerAdapter mAdapter;
private ProgressBar mProgressBar;
private TextView mPlayerTextView;
MaterialSearchView searchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(R.string.app_name);
toolbar.setTitleTextColor(Color.parseColor("#FFFFFF"));
searchView = (MaterialSearchView) findViewById(R.id.search_view);
mAdapter = new PlayerAdapter(this, new ArrayList<PlayerDetails>());
playerListView = (ListView) findViewById(R.id.list);
mPlayerTextView = (TextView) findViewById(R.id.empty_text_view);
playerListView.setAdapter(mAdapter);
searchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
@Override
public void onSearchViewShown() {
}
@Override
public void onSearchViewClosed() {
mAdapter = new PlayerAdapter(MainActivity.this, new ArrayList<PlayerDetails>());
playerListView.setAdapter(mAdapter);
}
});
playerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
PlayerDetails player = mAdapter.getItem(position);
String playerImage = player.getImage();
String playerName = player.getName();
String playerCountry = player.getCountry();
String playerDesc = player.getDesc();
String playerRuns = player.getTotalScore();
String playerMatches = player.getMatchesPlayed();
Intent intent = new Intent(MainActivity.this, PlayerDetailsActivity.class);
intent.putExtra("playerImage", playerImage);
intent.putExtra("playerName", playerName);
intent.putExtra("playerCountry", playerCountry);
intent.putExtra("playerDesc", playerDesc);
intent.putExtra("playerRuns", playerRuns);
intent.putExtra("playerMatches", playerMatches);
startActivity(intent);
}
});
ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
getLoaderManager().initLoader(PLAYER_ID, null, this).forceLoad();
} else {
mProgressBar = (ProgressBar) findViewById(R.id.loading_spinner);
mProgressBar.setVisibility(View.INVISIBLE);
}
}
@Override
public Loader<List<PlayerDetails>> onCreateLoader(int id, Bundle args) {
Uri baseUri = Uri.parse(PLAYER_DETAILS_URL);
Uri.Builder uriBuilder = baseUri.buildUpon();
return new PlayerLoader(this, uriBuilder.toString());
}
@Override
public void onLoadFinished(Loader<List<PlayerDetails>> loader, List<PlayerDetails> data) {
Log.v("MainActivity", "The UI is created via onLoadFinished");
mProgressBar = (ProgressBar) findViewById(R.id.loading_spinner);
mProgressBar.setVisibility(View.GONE);
mPlayerTextView = (TextView) findViewById(R.id.empty_text_view);
mPlayerTextView.setVisibility(View.GONE);
// Clear the adapter of previous earthquake data
mAdapter.clear();
// If there is a valid list of {@link Earthquake}s, then add them to the adapter's
// data set. This will trigger the ListView to update.
if (data != null && !data.isEmpty()) {
mAdapter.addAll(data);
}
}
@Override
public void onLoaderReset(Loader<List<PlayerDetails>> loader) {
Log.v("MainActivity", "The data is destroyed and the Loader is reset via onLoadReset()");
//Loader reset, so we can clear out all the existing data
mAdapter.clear();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
MenuItem searchItem = menu.findItem(R.id.search);
searchView.setMenuItem(searchItem);
return true;
}
}
和我的Adapter.java类是
public class PlayerAdapter extends ArrayAdapter<PlayerDetails> {
public PlayerAdapter(Activity context, ArrayList<PlayerDetails> players) {
super(context, 0, players);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
PlayerDetails currentPlayer = getItem(position);
ImageView playerImageView = (ImageView) listItemView.findViewById(R.id.player_image_view);
Glide.with(getContext()).load(currentPlayer.getImage())
.crossFade()
.thumbnail(0.5f)
.bitmapTransform(new CircleTransform(getContext()))
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(playerImageView);
TextView playerNameTextView = (TextView) listItemView.findViewById(R.id.player_name_text_view);
playerNameTextView.setText(currentPlayer.getName());
TextView playerCountryTextView = (TextView) listItemView.findViewById(R.id.player_country);
playerCountryTextView.setText(currentPlayer.getCountry());
return listItemView;
}
}
答案 0 :(得分:0)
在活动栏中添加菜单项搜索 膨胀它然后 在你的活动中
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
if (searchMenuItem == null) {
return;
}
SearchView searchView = (SearchView) searchMenuItem.getActionView();
searchView.setOnQueryTextListener(mSearchQueryTextListener);
}
protected SearchView.OnQueryTextListener mSearchQueryTextListener
= new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
onSearchTextChanged(s);
return false;
}
};
现在在onSearchTextChanged中使用包含关键字Arraylist.contains(s)搜索列表中的字符串并重新加载列表
希望有所帮助
答案 1 :(得分:0)
您必须创建以在适配器中实施 getFilter(),并创建一个过滤器类,该类将根据搜索条件进行过滤。 以下是一个示例:https://gist.github.com/tobiasschuerg/3554252