因此,当我搜索summonerName并提交它时,我创建了一个新的FetchMatchListTask扩展了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();
ADDED 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”一样被取消。然后我在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();
...