簡體   English   中英

嘗試在滾動后自動將新項目加載到ListView時,AbsListView中的setOnScrollListener(…)不適用於(new Runnable(){})

[英]setOnScrollListener(…) in AbsListView not applicable for (new Runnable(){}) when trying to automatically load new items into ListView after scroll

我要顯示的前10個項目到達列表末尾時繼續上傳到10個項目。 錯誤:行list=getListView().setOnScrollListener(this);的錯誤:“ AbsListView類型中的方法setOnScrollListener(AbsListView.OnScrollListener)不適用於參數(new Runnable(){} list=getListView().setOnScrollListener(this); 怎么修?

公共類CustomizedListView擴展了ListActivity實現的OnScrollListener {

private ProgressDialog pDialog;
// All static variables
static final String URL = "https://api.api2cart.com/v1.0/product.list.xml?api_key=6aed775211e8c3d556db063d12125d2d&store_key=ed58a22dfecb405a50ea3ea56979360d&start=0&count=19&params=id,u_model,name,price,images,short_description";
// XML node keys
static final String KEY_SONG = "product"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "u_model";
static final String KEY_ARTIST = "name";
static final String KEY_DURATION = "price";
static final String KEY_THUMB_URL = "http_path";
static final String KEY_SHORT_DESCRIPTION = "short_description";

ListView list;
LazyAdapter adapter;
ArrayList<HashMap<String, String>> songsList;

//@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    songsList = new ArrayList<HashMap<String, String>>();
    new LoadCatalog().execute();
}

public void onScroll(AbsListView view, int firstVisible, int visibleCount, int totalCount) {

    boolean loadMore = /* maybe add a padding */
        firstVisible + visibleCount >= totalCount;

    if(loadMore) {
        adapter.count += visibleCount; // or any other amount
        adapter.notifyDataSetChanged();
    }
}

public void onScrollStateChanged(AbsListView v, int s) { }

class LoadCatalog extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(CustomizedListView.this);
        pDialog.setMessage("Загрузка каталога ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
}
    protected String doInBackground(String... args) {
        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_SONG);
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key =&gt; value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
            map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
            map.put(KEY_SHORT_DESCRIPTION, parser.getValue(e, KEY_SHORT_DESCRIPTION));
            map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

            // adding HashList to ArrayList
            songsList.add(map);
        }
        return null;
    }

    public void onScroll(AbsListView view, int firstVisible, int visibleCount, int totalCount) {

        boolean loadMore = /* maybe add a padding */
            firstVisible + visibleCount >= totalCount;

        if(loadMore) {
            adapter.count += visibleCount; // or any other amount
            adapter.notifyDataSetChanged();
        }
    }

    public void onScrollStateChanged(AbsListView v, int s) { }

    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {

                list=getListView().setOnScrollListener(this);
                adapter=new LazyAdapter(CustomizedListView.this, songsList);
                list.setAdapter(adapter);
            }
        });

}}

將CustomthisListView.this的“ this”更改為“ this”,實際上是指您在runOnUiThread方法的參數內部創建的Runnable對象,您應該指向使用“ CustomizedListView.this”實現滾動偵聽器的類(在Case CustomizedListView實現Scroll Listener接口)

希望這可以幫助。

問候

@Override
    public void onStart() {
        super.onStart();
        getListView().setOnScrollListener(this);
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM