2012-08-17 43 views
1

我得到了無盡的加載和我的gridview沒有顯示。我的XML網址正在工作,一切似乎都很好。我的代碼有問題嗎?我只是修改了一些代碼,我試着在這裏學習Asynctask。請幫我Android無盡循環進度對話框AsyncTask

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(); 
      } 


      } 

     } 

回答

1

你必須使用

適配器=新LazyAdapter(Activityname.this,songsList);

您根本沒有使用doInbackground的功能。因爲你再次調用runOnuithread。去掉它。

移動到UI線程適配器相關的呼叫可以在OnpostExecute

+0

只是這樣做,我還是得到了android.os.NetworkOnMainThreadException。我不想使用StrictMode.setThreadPolicy(policy); – 2012-08-17 13:28:10

+0

檢查編輯。 – nandeesh 2012-08-17 13:32:28

+0

我有無盡的加載進度對話框,我的gridview沒有顯示。我已根據您的建議編輯我的代碼 – 2012-08-17 13:40:46

0

使用上下文

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()); 
    } 

,並把上下文引用您的AsyncTask構造函數。 我想最好的地方填寫你的適配器的方法

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

代替doInBackground();

+0

Yahor10,你能幫我解決這個問題嗎?我的進度對話框從不停止,我的gridview沒有顯示。 – 2012-08-17 14:17:00