Refreshing a listview
I have a listview that's populated by rows that get their data from a web server. It all works totally fine except I want to have a refresh button to re-download the data. I'm getting the data through an asynctask (getting the data in doinbackground) and then setting the listadapter in onpostexecute. All I do to run the asynctask which should take care of everything is run
new PopulateListTask().execute();
However, in my optionsSelect method, creating a new asynctask just like I did in oncreate doesn't do anything. It doesn't even enter doInBackground. I've tried using listView.invalidate() and listView.invalidateViews(). Is there some special way I'm supposed to repopulate a listview?
My only guess is that since I'm using a custom adapter, my getView method is creating some kind of error, but that wouldn't explain why it's not even entering my asynctask's doInBackground method. Thoughts?
Edit: Posting some code. This is a trimmed down version.
`
private class PopulateListTask extends AsyncTask<Void, Void, ArrayList<Quote>>{
@Override
protected ArrayList<Quote> doInBackground(Void... params) {
quotes = Helper.getQuotes();
return quotes;
}
@Override
protected void onPostExecute(ArrayList<Quote> quotes){
setListAdapter(new QuoteAdapter(ctx,R.layout.quote_row,quotes));
}
}
private class QuoteAdapter extends ArrayAdapter<Quote>{
ArrayList<Quote> items;
int resource;
public QuoteAdapter(Context context, int textViewResourceId, ArrayList<Quote> items){
super(context, textViewResourceId, items);
this.items = items;
this.resource = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(resource,null);
// do stuff with v
return v;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case(MENU_REFRESH):
new PopulateListTas开发者_JS百科k().execute();
}
`
Ok hopefully this works, sorry I'm still not totally sure the best way to enter code.
Edit2:
I'm getting this in my logcat when executing my asynctask the 2nd time. Any idea what it means? I tried googling to no avail.
W/InputManagerService( 52): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43b9dee8
The error was in creating my options subMenu. I had
menu.addSubMenu( MENU_REFRESH, 0, 0, "Refresh");
isntead of
menu.addSubMenu(0, MENU_REFRESH, 0, "Refresh");
精彩评论