2011-12-06 123 views
0

每當我慢慢想到代碼時,我會在結果選項卡上得到預期的結果,但是當我正常運行代碼時,結果選項卡中沒有結果,任何幫助都將不勝感激。android異步不更新listview?

這裏是類Hummmedia.java類,最主要的一個:

package edu.byu.hummedia; 

import android.app.ActionBar; 

import android.app.ActionBar.Tab; 
import android.app.Activity; 
import android.app.FragmentTransaction; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.Toast; 

public class Hummedia extends Activity implements ActionBar.TabListener { 
    /** Called when the activity is first created. */ 
    ListView myListView = null; 
    public ArrayAdapter<String> adapter; 
    Button basicSubmitButton; 
    Button advancedSubmitButton; 
    EditText myBasicEditText; 
    EditText myAdvancedEditText; 
    EditText title; 
    EditText language; 
    EditText category; 
    EditText keyword; 
    ActionBar bar; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     bar = getActionBar(); 
     bar.addTab(bar.newTab().setText("Basic\nSearch").setTabListener(this)); 
     bar.addTab(bar.newTab().setText("Advanced\nSearch").setTabListener(this)); 
     bar.addTab(bar.newTab().setText("Results").setTabListener(this)); 
     //bar.addTab(bar.newTab().setText("Exit").setTabListener(this)); 

     bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_USE_LOGO); 
     bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
     bar.setDisplayShowHomeEnabled(true); 
     // remove the activity title to make space for tabs 
     bar.setDisplayShowTitleEnabled(false); 
     bar.setDisplayUseLogoEnabled(false); 
    } 

    public void onTabReselected(Tab tab, FragmentTransaction ft) { 
     // TODO Auto-generated method stub 
     if (tab.getText().equals("Basic\nSearch")) 
     { 
      Toast.makeText(this, "Searching for: " + "basic" + "...", Toast.LENGTH_SHORT).show(); 
     } 
     if (tab.getText().equals("Advanced\nSearch")) 
     { 
      Toast.makeText(this, "Searching for: " + "Advanced" + "...", Toast.LENGTH_SHORT).show(); 
     } 
     if (tab.getText().equals("Results")) 
     { 
      Toast.makeText(this, "Searching for: " + "Results" + "...", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    public void onTabSelected(Tab tab, FragmentTransaction ft) { 
     // TODO Auto-generated method stub 
     if (tab.getText().equals("Basic\nSearch")) 
     { 
      setContentView(R.layout.basic); 
      basicSubmitButton = (Button)findViewById(R.id.myBasicSubmitbutton); 
      myBasicEditText = (EditText)findViewById(R.id.myBasicEditText); 
     } 
     if (tab.getText().equals("Advanced\nSearch")) 
     { 
      setContentView(R.layout.advanced); 
      advancedSubmitButton = (Button)findViewById(R.id.myAdvancedSubmitbutton); 
      title = (EditText)findViewById(R.id.title); 
      language = (EditText)findViewById(R.id.language); 
      category = (EditText)findViewById(R.id.category); 
      keyword = (EditText)findViewById(R.id.keyword); 
      //Toast.makeText(this, "Searching for: " + "Advanced" + "...", Toast.LENGTH_SHORT).show(); 

     } 
     if (tab.getText().equals("Results")) 
     { 
      setContentView(R.layout.results); 

      //Toast.makeText(this, "Searching for: " + "Results" + "...", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    public void basicSubmitButton(View view) { 
     String query = myBasicEditText.getText().toString(); 
     bar.setSelectedNavigationItem(2); 
     ReadWebpageAsyncTask basicSubmission = new ReadWebpageAsyncTask(); 
     basicSubmission.query(query); 
     myListView = (ListView) findViewById (R.id.lv); 
     myListView.setTextFilterEnabled(true); 
     myListView.clearChoices(); 


     adapter = new ArrayAdapter<String>(Hummedia.this, 
       android.R.layout.simple_list_item_1, 
       basicSubmission.mediaList); 
     myListView.setAdapter(adapter); 
     adapter.notifyDataSetChanged(); 

    } 


    public void advancedSubmitButton(View view) { 
     String mytitle = title.getText().toString(); 
     String mylanguage = language.getText().toString(); 
     String mycategory = category.getText().toString(); 
     String mykeyword = keyword.getText().toString(); 
     String query=""; 
     if (mytitle.isEmpty() != true) 
     { 
      query+="title="+mytitle; 
     } 
     if (mylanguage.isEmpty() != true) 
     { 
      query+="language="+mylanguage; 
     } 
     if (mycategory.isEmpty() != true) 
     { 
      query+="subject="+mycategory; 
     } 
     if (mykeyword.isEmpty() != true) 
     { 
      query+="keyword="+mykeyword; 
     } 
     //Toast.makeText(this, "I was called...", Toast.LENGTH_SHORT).show(); 
     bar.setSelectedNavigationItem(2); 
     ReadWebpageAsyncTask advancedSubmission = new ReadWebpageAsyncTask(); 
     advancedSubmission.advquery(query); 
     myListView = (ListView) findViewById (R.id.lv); 
     myListView.setTextFilterEnabled(true); 
     myListView.clearChoices(); 


     adapter = new ArrayAdapter<String>(Hummedia.this, 
       android.R.layout.simple_list_item_1, 
       advancedSubmission.mediaList); 
     myListView.setAdapter(adapter); 
     adapter.notifyDataSetChanged(); 

    } 


    public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
     // TODO Auto-generated method stub 

    } 
} 

,這裏是我的異步類:

package edu.byu.hummedia; 

import java.io.BufferedReader; 

import android.widget.Toast; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.HttpResponse; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONException; 
import org.json.JSONObject; 
import org.json.JSONArray; 

import android.app.Activity; 
import android.app.ListActivity; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.EditText; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.Toast; 
public class ReadWebpageAsyncTask extends ListActivity { 
    private TextView textView; 
    public ListView myListView = null; 
    List<String> mediaList = new ArrayList(); 
    public ArrayAdapter<String> adapter; 
    String[] newArray; 
/** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //setContentView(R.layout.main); 
     String[] values = new String[] { "Android", "iPhone", "WindowsMobile", 
       "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", 
       "Linux", "OS/2" }; 
     newArray = new String[mediaList.size()];mediaList.toArray(newArray); 
     adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, newArray); 
     setListAdapter(adapter);  
     readWebpage(myListView); 



    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     String item = (String) getListAdapter().getItem(position); 
     Toast.makeText(this, item + " selected", Toast.LENGTH_LONG); 
    } 



    private class DownloadWebPageTask extends AsyncTask<String, Void, String> { 
     @Override 
     protected String doInBackground(String... urls) { 
      String response = ""; 
      String responseBuilder; 
      String responseString = ""; 
      JSONArray jsonArray; 
      JSONObject jObject = new JSONObject(); 

      for (String url : urls) { 
       DefaultHttpClient client = new DefaultHttpClient(); 
       HttpGet httpGet = new HttpGet(url); 
       try { 
        HttpResponse execute = client.execute(httpGet); 
        InputStream content = execute.getEntity().getContent(); 

        BufferedReader buffer = new BufferedReader(
          new InputStreamReader(content)); 
        String s = ""; 
        while ((s = buffer.readLine()) != null) { 
         response += s; 
        } 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
      try { 
       jsonArray = new org.json.JSONArray(response); 
       jObject = jsonArray.toJSONObject(jsonArray); 

       for (int i=0;i<jObject.length();i++) 
       { 
        responseBuilder="\n"; 
        responseBuilder+= "\n title: "+((JSONObject) jsonArray.getJSONObject(i).get("videoinfo")).get("title"); 
        responseBuilder+= "\n subject: "+ ((JSONObject) jsonArray.getJSONObject(i).get("videoinfo")).get("subject"); 
        responseBuilder+= "\n language: "+ ((JSONObject) jsonArray.getJSONObject(i).get("videoinfo")).get("language"); 


        mediaList.add(responseBuilder); 
        responseString += responseBuilder; 
       } 



      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return responseString; 
     } 

     @Override 
     protected void onPostExecute(String result) { 

     } 
    } 


    public void readWebpage(View view) { 
     DownloadWebPageTask task = new DownloadWebPageTask(); 
     task.execute(new String[] { "http://hummedia.byu.edu/mediainfo/search/?title=Harry" }); 

    } 

    public void query(String query) { 
     DownloadWebPageTask task = new DownloadWebPageTask(); 
     task.execute(new String[] { "http://hummedia.byu.edu/mediainfo/search/?keyword="+query}); 

    } 

    public void advquery(String query) { 
     DownloadWebPageTask task = new DownloadWebPageTask(); 
     task.execute(new String[] { "http://hummedia.byu.edu/mediainfo/search/?"+query}); 

    } 

} 

最後,這裏是我沒有問題我可能logcat的堆棧跟蹤添加:

12-05 22:58:18.766: W/ActivityThread(2138): Application edu.byu.hummedia is waiting for the debugger on port 8100... 
12-05 22:58:18.826: I/System.out(2138): Sending WAIT chunk 
12-05 22:58:18.838: I/dalvikvm(2138): Debugger is active 
12-05 22:58:19.026: I/System.out(2138): Debugger has connected 
12-05 22:58:19.026: I/System.out(2138): waiting for debugger to settle... 
12-05 22:58:19.226: I/System.out(2138): waiting for debugger to settle... 
12-05 22:58:19.436: I/System.out(2138): waiting for debugger to settle... 
12-05 22:58:19.636: I/System.out(2138): waiting for debugger to settle... 
12-05 22:58:19.837: I/System.out(2138): waiting for debugger to settle... 
12-05 22:58:20.036: I/System.out(2138): waiting for debugger to settle... 
12-05 22:58:20.236: I/System.out(2138): waiting for debugger to settle... 
12-05 22:58:20.479: I/System.out(2138): waiting for debugger to settle... 
12-05 22:58:20.736: I/System.out(2138): waiting for debugger to settle... 
12-05 22:58:20.936: I/System.out(2138): waiting for debugger to settle... 
12-05 22:58:21.176: I/System.out(2138): waiting for debugger to settle... 
12-05 22:58:21.448: I/System.out(2138): waiting for debugger to settle... 
12-05 22:58:21.705: I/System.out(2138): waiting for debugger to settle... 
12-05 22:58:21.917: I/System.out(2138): waiting for debugger to settle... 
12-05 22:58:22.136: I/System.out(2138): debugger has settled (1485) 
12-05 22:58:25.196: D/gralloc_goldfish(2138): Emulator without GPU emulation detected. 
12-05 22:58:38.736: D/dalvikvm(2138): GC_CONCURRENT freed 211K, 4% free 10169K/10503K, paused 20ms+15ms 
12-05 22:58:39.075: D/dalvikvm(2138): GC_CONCURRENT freed 456K, 7% free 10166K/10823K, paused 25ms+15ms 
12-05 22:58:39.336: D/dalvikvm(2138): GC_CONCURRENT freed 403K, 6% free 10194K/10823K, paused 14ms+14ms 
12-05 22:58:39.527: D/dalvikvm(2138): GC_CONCURRENT freed 425K, 7% free 10161K/10823K, paused 14ms+5ms 
12-05 22:58:39.790: D/dalvikvm(2138): GC_CONCURRENT freed 389K, 7% free 10164K/10823K, paused 14ms+13ms 

回答

1

其因您顯示來自的AsyncTask未完成任務的數據,所以或者等待它完成(顯示進度對話框)或者我認爲你必須實現從doINBackground()更新您的AsyncTask onProgressUpdate()publishProgress(..);您的列表在一定的時間間隔

喜歡的東西,(這例如)

protected void onProgressUpdate(Integer... progress) { 
     setProgressPercent(progress[0]); 
    } 
0

在嘗試結果選項卡之前,可能無法完成異步任務。爲您的異步任務添加進度並阻止屏幕,直到完成。然後點擊結果。