简体   繁体   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

I want to display first 10 item continue to upload to 10 item when they reach the end of the list. 我要显示的前10个项目到达列表末尾时继续上传到10个项目。 The error: "The method setOnScrollListener(AbsListView.OnScrollListener) in the type AbsListView is not applicable for the arguments (new Runnable(){})" in line list=getListView().setOnScrollListener(this); 错误:行list=getListView().setOnScrollListener(this);的错误:“ AbsListView类型中的方法setOnScrollListener(AbsListView.OnScrollListener)不适用于参数(new Runnable(){} list=getListView().setOnScrollListener(this); . How to fix? 怎么修?

public class CustomizedListView extends ListActivity implements OnScrollListener{ 公共类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);
            }
        });

} }} }}

Change "this" for CustomizedListView.this, with "this" you are actually refering to the Runnable Object you created inside the parameters of runOnUiThread method, you should point to the class that implements the Scroll Listener by using "CustomizedListView.this" (in case CustomizedListView implements the Scroll Listener interface) 将CustomthisListView.this的“ this”更改为“ this”,实际上是指您在runOnUiThread方法的参数内部创建的Runnable对象,您应该指向使用“ CustomizedListView.this”实现滚动侦听器的类(在Case CustomizedListView实现Scroll Listener接口)

Hope this Helps. 希望这可以帮助。

Regards 问候

@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