我的应用程序中有一个Activity,用户从列表中搜索特定项目,但他也可以按城市列表中的那些项目。我正在做微调器。在他点击微调器对项目进行排序后,他再次从服务器获取数据。
在按城市排序列表中的项目之前搜索时会出现问题。过滤工作正常,但在我们过滤列表中的项目后,我不能再使用微调器并按所选城市获取有序数据。该列表显示了以spinner的默认值开头的先前记录。无法再获取数据。我不知道为什么。
这是该活动的代码:
public class SearchGroupsActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener, SearchView.OnQueryTextListener {
Toolbar mToolbar;
SessionManager mSessionManager;
List<Group> mGroups;
RecyclerView mList;
RecyclerGroupsAdapter mAdapter;
SharedPreferences mSharedPreferences;
MyProgressDialog mDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow(); // in Activity's onCreate() for instance
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_groups);
mSessionManager = new SessionManager(this);
mSharedPreferences = new SharedPreferences(this);
mGroups = new ArrayList<>();
final SearchView searchView = (SearchView) findViewById(R.id.search);
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(this);
setAdapter();
setToolbar();
Spinner spinner = (Spinner) mToolbar.findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.group_cities, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
if (!mSessionManager.isLoggedIn()) {
startActivity(new Intent(this, LoginActivity.class));
this.finish();
}
}
private void setAdapter() {
mList = (RecyclerView) findViewById(R.id.list);
mAdapter = new RecyclerGroupsAdapter(this, mGroups);
RecyclerView.LayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mList.setLayoutManager(manager);
mList.setItemAnimator(new DefaultItemAnimator());
mList.setAdapter(mAdapter);
}
private void loadGroups(String selectedCity) {
mDialog = new MyProgressDialog(this, 1);
mDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET,
AppConfig.ORDER_GROUPS_BY_CITY + "/" + selectedCity,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
hidePDialog();
if (mGroups.size() > 0) {
mGroups.clear();
}
try {
JSONObject object = new JSONObject(response);
boolean error = object.getBoolean("error");
JSONArray groups = object.getJSONArray("groups");
if (!error) {
for (int i = 0; i < groups.length(); i++) {
JSONObject groupObject = groups.getJSONObject(i);
Group group = new Group();
group.setGroupName(groupObject.optString("group_name"));
mGroups.add(group);
}
}
// notify adapter that data has changed
mAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
hidePDialog();
Toast.makeText(SearchGroupsActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
AppController.getInstance().addToRequestQueue(stringRequest);
}
private String orderGroupsByCity() {
String cityName = "";
int selectedFilterOption = mSharedPreferences.getCityFilter();
switch (selectedFilterOption) {
case 0:
cityName = "Nis";
break;
case 1:
cityName = "Beograd";
break;
case 2:
cityName = "Paracin";
break;
}
return cityName;
}
private void setToolbar() {
mToolbar = (Toolbar) findViewById(R.id.toolbar);
if (mToolbar != null) {
setSupportActionBar(mToolbar);
}
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("NADJI EKIPU");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
this.finish();
break;
}
return false;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int selectedCityFilter = GroupsCity.NIS;
switch (position) {
case 0:
// NIS
selectedCityFilter = GroupsCity.NIS;
break;
case 1:
// BEOGRAD
selectedCityFilter = GroupsCity.BEOGRAD;
break;
case 2:
// PARACIN
selectedCityFilter = GroupsCity.PARACIN;
break;
}
mSharedPreferences.setCityFilter(selectedCityFilter);
String selectedCity = orderGroupsByCity();
loadGroups(selectedCity); // HERE WE ARE FETCHING DATA ORDERED BY SELECTED CITY
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (mDialog != null) {
mDialog.dismiss();
mDialog = null;
}
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
final List<Group> filteredModelList = filter(mGroups, newText);
mAdapter.setFilter(filteredModelList);
return true;
}
private List<Group> filter(List<Group> groups, String query) {
query = query.toLowerCase();
final List<Group> filteredModelList = new ArrayList<>();
for (Group group : groups) {
final String text = group.getGroupName().toLowerCase();
if (text.contains(query)) {
filteredModelList.add(group);
}
}
return filteredModelList;
}
}
答案 0 :(得分:1)
如果您首先搜索然后单击Spinner,您将过滤适配器(这将正常工作),然后您将向服务器执行该请求(当用户选择城市时)。
但是,当请求返回数据时,您当前只是将其添加到适配器不基于的groups
列表中(我的假设是,当您进行过滤时传递新列表) 。因此,在这种情况下,简单地调用notifyDatasetChanged()
将无用,因为适配器将无法看到新数据项。
为了解决这个问题,在你的loadGroups()方法中你需要再次通过调用filter()
方法进行过滤,并在适配器上设置结果列表(就像你目前在监听器中那样做搜索查看)。