简体   繁体   English

Android无限循环进度对话框AsyncTask

[英]Android Endless loop Progress Dialog AsyncTask

I got an endless loading and my gridview is not showing. 我的负载不尽,GridView没有显示。 My xml web address is working and everything seems fine. 我的xml网址正在运行,并且一切正常。 Are there something wrong with my codes? 我的代码有问题吗? I just modified some codes, im trying to learn Asynctask here. 我刚刚修改了一些代码,我试图在这里学习Asynctask。 Please help me 请帮我

CustomizedListView.class CustomizedListView.class

    public class CustomizedListView extends Activity {
        // All static variables
        private ProgressDialog pDialog;
        static final String URL = "http://XXXXXX";
        // XML node keys
        ArrayList<HashMap<String, String>> songsList;
        static final String KEY_SONG = "song"; // parent node
        static final String KEY_ID = "id";
        static final String KEY_TITLE = "title";
        static final String KEY_ARTIST = "artist";
        static final String KEY_DURATION = "duration";
        static final String KEY_THUMB_URL = "thumb_url";
        static String IMAGE_POSITION;
        GridView grid;
        LazyAdapter adapter;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.grid_layout);
            //StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

            //StrictMode.setThreadPolicy(policy); 

             songsList = new ArrayList<HashMap<String, String>>();

             new loadGridView().execute();


            grid = (GridView) findViewById(R.id.grid_view);
            grid.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
                    String artist = ((TextView) view.findViewById(R.id.artist)).getText().toString();
                    String url = ((TextView) view.findViewById(R.id.duration)).getText().toString();
                    // Starting new intent
                    Intent in = new Intent(getApplicationContext(),SingleMenuItemActivity.class);
                    in.getIntExtra(IMAGE_POSITION, position);
                    in.putExtra(KEY_TITLE, title);
                    in.putExtra(KEY_ARTIST, artist);
                    in.putExtra(KEY_THUMB_URL,url);
                    startActivity(in);      

                }
            });
        }

        class loadGridView extends AsyncTask<String, String, String> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(
                        CustomizedListView.this);
                pDialog.setMessage("Loading websites ...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }

            @Override
            protected String doInBackground(String... args) {
                // updating UI from Background Thread


                        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);
                        // looping through all song nodes <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 => 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_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

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


                        return null;

                    }   
@Override
                    protected void onPostExecute(String args) {
                // dismiss the dialog after getting all products
                adapter=new LazyAdapter(CustomizedListView.this, songsList);        
                grid.setAdapter(adapter);
                pDialog.dismiss();
            }


            }

        }

You have to use 你必须用

adapter=new LazyAdapter(Activityname.this, songsList); adapter = new LazyAdapter(Activityname.this,songsList);

You are not at all using the functionality of doInbackground. 您根本没有使用doInbackground的功能。 Since you are calling runOnuithread again. 由于您再次调用runOnuithread。 Remove it. 去掉它。

Move the adapter related call to Ui thread may be in OnpostExecute 将适配器相关的调用移到Ui线程可能在OnpostExecute中

Use a context 使用上下文

public LazyAdapter(Context context, ArrayList<HashMap<String, String>> d) {
        context = context;
        data=d;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(context.getApplicationContext());
    }

and put Context reference to your AsyncTask Constructor. 并将Context引用放到您的AsyncTask构造函数中。 I suppose best place to fill your adapter in the method 我想最好的方法来填充您的适配器

 void onPostExecute(String args) {
// fill your adapter here
        }

instead doInBackground(); 而是doInBackground();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM