在 SearchView.onQueryTextSubmit() 和 AsyncTask 上更新 ListView

Update ListView on SearchView.onQueryTextSubmit() and AsyncTask

因此,当我搜索 summonerName 然后提交它时,我创建了新的 FetchMatchListTask extends AsyncTask,我在其中发送了 summonerName,然后将他的所有匹配项加载到 ListView 并且它有效,但是当用户搜索不同的名称时,我想重新加载 ListView。这是我的代码:

public class MatchListActivity extends MenuActivity {

    private TextView numOfGamesText;
    int matchLimitCounter = 0;
    CircularImageView circularImageView;
    TextView summNameTv;
    TextView soloDuoQue;
    TextView summLvl;
    SearchView mySearchView;

    MatchListAdapter adapter;
    ListView matchList;
    ArrayList<MatchDetails> matchArrayList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.match_list_activity);

        matchLimitCounter = 0;
        numOfGamesText = findViewById(R.id.txtNumOfGames);
        matchList = findViewById(R.id.matchListView);
        circularImageView = findViewById(R.id.summonerProfileIcon);
        summNameTv = findViewById(R.id.summonerName);
        soloDuoQue = findViewById(R.id.soloDuoQue);
        summLvl = findViewById(R.id.summLevel);
        mySearchView = findViewById(R.id.searchView);

        adapter = new MatchListAdapter(this, R.layout.match_adapter, matchArrayList);
        matchList.setAdapter(adapter);

        mySearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String summName) {
                Toast toast = Toast.makeText(MatchListActivity.this, "Wait a few seconds for loading...", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                if(summName.isEmpty()){
                    Toast.makeText(MatchListActivity.this, "Enter summoner name!", Toast.LENGTH_LONG);
                }else {
                    FetchMatchListTask summonerTask = new FetchMatchListTask();
                    summonerTask.execute(summName);
                }
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                return false;
            }
        });

    }

    class Wrapper
    {
        Summoner summoner;
        RiotApi api;
        MatchList matchList;
        Match match;
        Participant participant;
        ParticipantStats stats;
    }

    @SuppressLint("StaticFieldLeak")
    public class FetchMatchListTask extends AsyncTask<String, Void, Wrapper> {

        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        protected Wrapper doInBackground(String... params) {

            ApiConfig config = new ApiConfig().setKey("API-KEY");
            Wrapper w = new Wrapper();
            w.api = new RiotApi(config);
            Platform platform;

            try {
                w.summoner = w.api.getSummonerByName(Platform.EUNE, params[0]);
                w.matchList = w.api.getMatchListByAccountId(platform, w.summoner.getAccountId());

                if (w.matchList.getMatches() != null) {
                    for (MatchReference matchRef: w.matchList.getMatches()) {
                        if(matchLimitCounter == 10) break;
                        else{
                            matchLimitCounter++;

                            long gameID = matchRef.getGameId();
                            w.match = w.api.getMatch(platform, gameID);
                            long gameDuration = w.match.getGameDuration();
                            int champKey = matchRef.getChampion();

                            MatchDetails matchDetails = new MatchDetails(champKey);
                            matchArrayList.add(matchDetails);
                        }
                    }
                }

                return w;
            } catch (RiotApiException e) {
                Log.d("RiotMSG", "Error: "+ RiotApiException.getMessage(e.getErrorCode()));
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Wrapper wrapp) {
            super.onPostExecute(wrapp);
            if(wrapp != null) {
                numOfGamesText.setText(String.valueOf(wrapp.matchList.getTotalGames()));
                summNameTv.setText(wrapp.summoner.getName());
                summLvl.setText(String.valueOf(wrapp.summoner.getSummonerLevel())+" LvL");

            }

            //Not working
            //((MatchListAdapter) matchList.getAdapter()).notifyDataSetChanged();

            if (wrapp == null) { throw new NullPointerException("wrapp object is null, means smth go wrong doInBack"); }
        }
    }
}

我尝试在 onPostExecute 中使用它,但是没有用:

MatchListActivity.this.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        adapter.notifyDataSetChanged();
    }
});

adapter.notifyDataSetChanged();

((MatchListAdapter) matchList.getAdapter()).notifyDataSetChanged();

已添加 MatchListAdapter

public class MatchListAdapter extends ArrayAdapter<MatchDetails> {

private Context mContext;
private int mResource;
private ArrayList<MatchDetails> myMatchList; // added with updateMyMatchList

MatchListAdapter(Context context, int resource, ArrayList<MatchDetails> objects){
    super(context, resource, objects);
    mContext = context;
    mResource = resource;
    myMatchList = objects;  // added with updateMyMatchList
}

//Method to change list but not working
 public void updateMyMatchList(ArrayList<MatchDetails> objects){
    myMatchList.clear();
    myMatchList.addAll(objects);
    this.notifyDataSetChanged();
}

public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = LayoutInflater.from(mContext);
    convertView = inflater.inflate(mResource, parent, false);

    //Get the champion information and other stuff...
    int champKey = getItem(position).getChampionKey();
    chamKeyTextView.setText(champKey);
}}

所以我要做的是:添加 matchLimitCounter = 0; matchArrayList.clear();在执行 AsynTask 之前,非常喜欢 "i_A_mok" asnwered。然后我在 onPostExecute():

中使用
adapter.updateMyMatchList(matchArrayList);

并在 MatchListAdapter 中声明 ArrayList myMatchList 并且:

updateMyMatchList(ArrayList<MatchDetails> objects){
     myMatchList.addAll(objects);
     this.notifyDataSetChanged();
}

并在 getView() 中:

MatchDetails d = myMatchList.get(position);
int champKey = d.getChampionKey();
...