目前,我正在制作我的第一个Android应用程序,并遇到了一些我似乎无法解决的问题。
我目前正在为我的应用程序的1页开发搜索片段,
它包含一个SearchView,一个Spinner和一个ListView。 Spinner和SearchView用于过滤ListView中的先前搜索结果(尚未实现),此过滤适用于硬编码值。
但是,当我使用JSON获取数据(Logs显示它已成功获取)并尝试使用适配器填充listview时,它不会被填充。我假设在ListViewAdapter创建/实例化之前,ListView中显示的硬编码值的原因是它们被提供。但是对于JSONAdapter,这实际上是不可能的,因为它仅在提交搜索时从服务器获取,因此当给ListViewAdapter时ArrayList为null,但更新ArrayList不会导致填充ListView。
在我的项目中,我将User类用于硬编码值,因为它只有2个字段,平台和用户名,而GeneralObject类用于JSONAdapter来存储所有JSON数据。
搜索片段(Search.java)
SearchView searchView;
ListView listView;
ListViewAdapter listViewAdapter;
String[] platformList;
String[] userNameList;
ArrayList<User> userList = new ArrayList<User>();
ArrayList<GeneralObject> searchResults = new ArrayList<>();
ArrayList<GeneralObject> filteredSearchResults = new ArrayList<>();
ArrayAdapter<CharSequence> spinnerAdapter;
private RequestQueue mQueue;
String currPlatformSelected = "";
String currUserNameSelected = "";
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.search_fragment, container, false);
Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
spinnerAdapter = ArrayAdapter.createFromResource(getContext(), R.array.platform_array, android.R.layout.simple_spinner_item);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//spinner.setPrompt("Platform");
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(this);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) view.findViewById(R.id.searchView);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
setupSearchView();
userNameList = new String[]{"sallad_", "fork__", "KonoAma", "Darksubi_", "XboxTest", "PlaystationTest"};
platformList = new String[]{"PC", "PC", "PC", "PC", "Xbox", "PS4"};
listView = (ListView) view.findViewById(R.id.listview);
mQueue = Volley.newRequestQueue(getContext());
// just for populating the arrays before implementing previous history
if (userNameList.length == 0 || platformList.length == 0) // if no hardcoded values given
{
User previousSearches = new User("No Results", "Please Search Usernames to populate");
userList.add(previousSearches);
currUserNameSelected = "Please Search Usernames to populate";
currPlatformSelected = "No Results";
}
else if (userNameList.length == platformList.length) // if there are equal matching pairs in the hardcoded string arrays
{
for (int i = 0; i < userNameList.length; i++) {
User user = new User(platformList[i], userNameList[i]);
userList.add(user);
}
}
//if i replace the below userList with searchResults it doesn't work as it isnt populated yet
listViewAdapter = new ListViewAdapter(getContext(), userList);
listView.setAdapter(listViewAdapter);
return view;
}
private void setupSearchView()
{
searchView.setIconified(false);
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(this);
searchView.setOnSearchClickListener(this);
searchView.setOnCloseListener(this);
searchView.setQueryHint("Enter Username...");
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) // Spinner Select
{
currPlatformSelected = (String) adapterView.getItemAtPosition(i);
listViewAdapter.filter(currUserNameSelected, currPlatformSelected);
Log.v("item", (String) currPlatformSelected);
}
@Override
public boolean onQueryTextSubmit(String query) // on SearchView text submit
{
//searchView.setIconified(true);
currUserNameSelected = query;
Log.v("JSON", "starting json import with username = " + currUserNameSelected + " and platform = " + currPlatformSelected);
//R6StatsJSONAdapter r6StatsJsonAdapter = new R6StatsJSONAdapter(currUserNameSelected, currPlatformSelected, mQueue);
R6DBJSONAdapter r6DBJsonAdapter = new R6DBJSONAdapter(currUserNameSelected, currPlatformSelected, mQueue);
r6DBJsonAdapter.ParseGeneral();
searchResults = r6DBJsonAdapter.getSearchResults();
Log.v("JSON", "finished json import");
return true;
}
@Override
public boolean onQueryTextChange(String newText) // on SearchView text change
{
//searchView.setIconified(false);
//searchView.setIconifiedByDefault(false);
currUserNameSelected = newText;
listViewAdapter.filter(currUserNameSelected, currPlatformSelected);
return false;
}
ListViewAdapter
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context mContext;
LayoutInflater inflater;
private List<User> userList = null;
private ArrayList<User> arraylist;
public ListViewAdapter(Context context, List<User> userList) {
mContext = context;
this.userList = userList;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<User>();
this.arraylist.addAll(userList);
}
public class ViewHolder {
TextView userName;
TextView platformName;
}
@Override
public int getCount() {
return userList.size();
}
@Override
public User getItem(int position) {
return userList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.list_view_items, null);
holder.userName = (TextView) view.findViewById(R.id.userName);
holder.platformName = (TextView) view.findViewById(R.id.platformName);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
}
holder.userName.setText(userList.get(position).getPlayerName());
holder.platformName.setText(userList.get(position).getPlatform());
return view;
}
// Filter Class
public void filter(String userName, String platformName)
{
userName = userName.toLowerCase(Locale.getDefault());
platformName = platformName.toLowerCase(Locale.getDefault());
userList.clear();
for (User currPrevSearch : arraylist)
{
if (userName.length() == 0 && currPrevSearch.getPlatform().toLowerCase(Locale.getDefault()).contains(platformName))
{
userList.add(currPrevSearch);
}
else if (currPrevSearch.getPlayerName().toLowerCase(Locale.getDefault()).contains(userName) && currPrevSearch.getPlatform().toLowerCase(Locale.getDefault()).contains(platformName))
{
userList.add(currPrevSearch);
}
}
notifyDataSetChanged();
}
}
任何帮助将不胜感激,我可以清除任何困惑。
答案 0 :(得分:0)
修正了它,使用适配器onDataSetChanged()
方法,它更新了数据但没有立即显示它,这是因为onSubmit
数据是使用Volley异步获取的,所以它不是可以显示(虽然关闭键盘使其显示)。
仅在JSONAdapter
完成解析时才更新数据。