我正在尝试使用Autogrowin / Endless ListView,但在下载数据后我遇到ArrayAdapter = update问题。
public class ListShowActivity extends ListActivity {
private ShowsAdapter m_adapter;
private ArrayList<ShowsList> showList = new ArrayList<ShowsList>();
// Adapter
private class ShowsAdapter extends ArrayAdapter<ShowsList>
{
private ArrayList<ShowsList> items = new ArrayList<ShowsList>();
public ShowsAdapter(Context context, int textViewResourceId, ArrayList<ShowsList> items)
{
super(context, textViewResourceId, items);
this.items = items;
}
public void appendList(ArrayList<ShowsList> results)
{
if((results != null) && (results.size() != 0))
{
m_adapter.add(results.get(0));
m_adapter.notifyDataSetChanged();
Log.i("test", "appendList: "+results.get(0).originalTitle);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
TextView tt = null;
TextView bt = null;
ImageView iv = null;
if (v == null){
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_show_item, null);
tt = (TextView) v.findViewById(R.id.showOriginalTitle);
bt = (TextView) v.findViewById(R.id.showTitle);
iv = (ImageView) v.findViewById(R.id.showPoster);
}
ShowsList item = this.items.get(position);
if (item != null) {
if(tt != null){
tt.setText(item.originalTitle);
}
if(bt != null){
bt.setText(item.title);
}
if(iv != null)
{
new DownloadImage().downloadImage(item.poster, iv);
}
}
return v;
}
}
// Implement new OnScrollListener
public class onScrollLoad implements OnScrollListener {
private int lastOffset = 10;
private int limit = 10;
private int mPrevTotalItemCount = 0;
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (((firstVisibleItem + visibleItemCount) >= totalItemCount) && totalItemCount != mPrevTotalItemCount) {
lastOffset = totalItemCount;
mPrevTotalItemCount = lastOffset;
limit += 1;
ArrayList<ShowsList> results = new Api().getShows(lastOffset, limit);
m_adapter.appendList(results);
//new PagingRequest(m_adapter).execute(lastOffset, limit);
Toast.makeText(getApplicationContext(), lastOffset+" / "+limit, Toast.LENGTH_SHORT).show();
}
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
Log.i("test", "onScrollStateChange()");
}
}
// Implement AsyncTask for download items?
/*
private static class PagingRequest extends AsyncTask<Integer, Void, ArrayList<ShowsList>> {
private ShowsAdapter mAdapter;
public PagingRequest(ShowsAdapter adapter) {
this.mAdapter = adapter;
}
protected ArrayList<ShowsList> doInBackground(Integer... params) {
return new Api().getShows(params[0], params[1]);
}
protected void onPostExecute(ArrayList<ShowsList> result) {
this.mAdapter.appendList(result);
}
}
*/
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.list_shows);
TextView tv = (TextView) findViewById(R.id.headerbarText);
tv.setText("List Shows");
// Get Items
Api a = new Api();
// First 10 items
showList = a.getShows(0, 0);
if(showList.isEmpty() == false)
{
//showList = new Api().getShows(0,0);
m_adapter = new ShowsAdapter(this, R.layout.list_show_item, showList);
setListAdapter(m_adapter);
ListView lv = getListView();
lv.setOnScrollListener(new onScrollLoad());
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent intent = new Intent(ListShowActivity.this, ShowActivity.class);
//intent.putExtra("showID", getShowsList.get(position).ID);
startActivity(intent);
}
});
}else{
Toast.makeText(getApplicationContext(), "NoData", Toast.LENGTH_SHORT).show();
}
}
}
我没有使用Asynctask就粘贴了代码。 在“理论”^^一切正常(appendList日志返回正确的值),但我说我有更新ShowsAdapter(下载新图像+并将内容插入ListView)的问题