代码之家  ›  专栏  ›  技术社区  ›  Loz

使用截取从JSON获取的数据填充ListView,并在SearchView提交时触发

  •  0
  • Loz  · 技术社区  · 6 年前

    目前,我正在制作我的第一个android应用程序,遇到了一些我似乎无法修复的问题。
    我目前正在为我的应用程序的1页开发一个搜索片段,
    它包含一个SearchView、一个微调器和一个ListView。微调器和SearchView用于过滤ListView中以前的搜索结果(尚未实现),这种过滤对于硬编码的值非常有效。

    然而,当我使用JSON获取数据(日志显示它已成功获取)并尝试使用适配器填充listview时,它不会被填充。我推测,硬编码的值显示在ListView中的原因在于它们是在创建/实例化ListViewAdapter之前提供的。然而,对于JSONAdapter,这实际上是不可能的,因为它只在提交搜索时从服务器获取,所以当提供给ListViewAdapter时,ArrayList为null,但更新ArrayList不会导致填充ListView。

    在我的项目中,我将User类用于硬编码值,因为它只有2个字段,即platform和username,而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();
    }
    
    }
    

    任何帮助都将不胜感激,我可以消除任何困惑。

    1 回复  |  直到 6 年前
        1
  •  0
  •   ColdFire    6 年前

    使用适配器修复了它 onDataSetChanged() 方法,它更新了数据但没有立即显示,这是因为 onSubmit 数据是通过截取异步获取的,因此无法显示(尽管关闭键盘会显示出来)。

    仅当 JSONAdapter 已完成解析。