2012-05-26 3 views
0

SomeActivity 클래스가 BaseListActivity를 확장했습니다. BaseListActivity는 ListActivity와 그 모든 작업을 확장합니다. BaseListActivity를 상속하고 AsyncTask를 사용할 때 ListActivity가 업데이트되지 않습니다.

그러나 목록 업데이트가있을 때, 나는 다음과 같이 선언 AsyncTask를 사용하여 원격 서버 호출을 사용

: 거기에

public class DownloadWebPageTask extends AsyncTask<String, Void, String> 

,이 같은 목록을 업데이트 할 수있는 라인이

adapter.notifyDataSetChanged(); 

하지만이 행은 아무 것도하지 않는 것 같습니다.

작동하게하려면 어떻게해야할까요? 여기

는 BaseListActivity

public class BaseListActivity extends ListActivity { 

    public ListView getListView() { 
     return (ListView)findViewById(android.R.id.list); 
    } 

    public ListAdapter getListAdapter() { 
     return getListView().getAdapter(); 
    } 

    @Override 
    public void setContentView(int layoutResID) {  
     super.setContentView(layoutResID); 
     { 
      setListAdapter(this.getListAdapter()); 

      Button home_header = (Button) findViewById(R.id.home_header); 
      Button questions_header = (Button) findViewById(R.id.questions_header); 

      home_header.setOnClickListener(new Button.OnClickListener() { 
       public void onClick(View v) { 
        Intent myIntent = new Intent(BaseListActivity.this, 
          ProblemioActivity.class); 
        BaseListActivity.this.startActivity(myIntent); 
       } 
      }); 

      questions_header.setOnClickListener(new Button.OnClickListener() { 
       public void onClick(View v) { 
        Intent myIntent = new Intent(BaseListActivity.this, 
          MyQuestionsActivity.class); 
        BaseListActivity.this.startActivity(myIntent); 
       } 
      }); 
     } 
    } 
} 

이 전체 DownloadWebPageTask 코드입니다 : AsyncTask를 {
@Override

공용 클래스 DownloadWebPageTask이 확장 보호 문자열 doInBackground (문자열 ... theParams) { 문자열 myUrl = theParams [0]; 최종 문자열 user_id = theParams [1];

 String charset = "UTF-8";   
     String response = null; 

     try 
     {       
      String query = String.format("user_id=%s", 
        URLEncoder.encode(user_id, charset)); 

      final URL url = new URL(myUrl + "?" + query); 

      Log.d("QuestionUserURL" , url+ ""); 

      final HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

      conn.setDoOutput(true); 
      conn.setRequestMethod("POST"); 

      conn.setDoOutput(true); 
      conn.setUseCaches(false); 

      conn.connect(); 

      final InputStream is = conn.getInputStream(); 
      final byte[] buffer = new byte[8196]; 
      int readCount; 
      final StringBuilder builder = new StringBuilder(); 
      while ((readCount = is.read(buffer)) > -1) 
      { 
       builder.append(new String(buffer, 0, readCount)); 
      } 

      response = builder.toString();  
     } 
     catch (Exception e) 
     { 
       Log.d("EXCEPTIONNNN: " , "Yup" + e.getMessage()); 
       e.printStackTrace(); 
     } 

     Log.d("MyQuestionsActivity" , "*** Response: " + response); 
     return response; 
    } 

    @Override 
    protected void onPostExecute(String result) 
    { 
     Log.d("MyQuestionsActivity" , "In the post-execute method: " + result); 

     if (result != null && result.trim().equals("no_user_id")) 
     { 
      Log.d("Post execute: " , "NOOOT OKKKK - no user id");  
      // Show the user a message that they did not enter the right login 

      Toast.makeText(getApplicationContext(), "Could not get your questions. We are working to fix this. ", Toast.LENGTH_LONG).show();         

     sendEmail("Error in My Questions", "The questions by user did not load because on the server the validation for user id said the user id was empty.");  
     } 
     else 
     if (result != null && result.trim().equals("database_error")) 
     { 
      Log.d("Post execute: " , "NOOOT OKKKK - no user database error");  
      // Show the user a message that they did not enter the right login 

      Toast.makeText(getApplicationContext(), "Could not get your questions. We are working to fix this. ", Toast.LENGTH_LONG).show();         

     sendEmail("Error in My Questions", "The questions by user did not load because on the server there " + 
       "was a database error.");     
     } 
     else 
     if (result != null && result.trim().equals("no_questions_by_user")) 
     {   
      // Show the user a message that they did not enter the right login 

      //Toast.makeText(getApplicationContext(), "You have not asked any questions. Ask your business questions! ", Toast.LENGTH_LONG).show();         

     //sendEmail("No Questions so far in My Questions", "User got message that they do not have questions.");   

     TextView question_label = (TextView) findViewById(R.id.question_label); 
     question_label.setText("You have not asked any business questions. Ask your question."); 
     } 
     else 
     { 
      Log.d("MyQuestionsActivity" , "In JSON parsing area.."); 

       try 
       { 
        JSONArray obj = new JSONArray(result); 

        if (obj != null) 
        { 
         questions.clear(); 

         for (int i = 0; i < obj.length(); i++) 
         { 
          JSONObject o = obj.getJSONObject(i); 

          String question_id = o.getString("question_id"); 
          String question = o.getString("question"); 
          String questioner_id = o.getString("member_id"); 
          String first_name = o.getString("first_name"); 
          String last_name = o.getString("last_name"); 

          Log.d("Question: " , question);        
          Log.d("Question_id: " , question_id);  

          Question q = new Question (); 
          q.setQuestion(question); 
          q.setQuestionId(question_id); 
          q.setQuestionByMemberId(questioner_id); 

          q.setAuthorName(first_name + " " + last_name); 

          questions.add(q); 
         } 
        } 

        adapter.notifyDataSetChanged(); 

        TextView question_label = (TextView) findViewById(R.id.question_label); 
        question_label.setText("The questions you asked are below. Choose one or ask a new question."); 
       }     
       catch (Exception e) 
       { 
        Log.d("MyQuestionsActivity: " , "some crap happened 1: " + e.getMessage()); 
        e.printStackTrace(); 
       } 
      } 

      //adapter.notifyDataSetChanged();      
     } 
    }   

이것은 내가 지금 로그에서 볼 수있는 예외 :

05-26 19:59:55.774: W/System.err(3103): java.net.UnknownHostException: Unable to resolve host "www.problemio.com": No address associated with hostname 
05-26 19:59:55.777: W/System.err(3103):  at java.net.InetAddress.lookupHostByName(InetAddress.java:426) 
05-26 19:59:55.777: W/System.err(3103):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242) 
05-26 19:59:55.777: W/System.err(3103):  at java.net.InetAddress.getAllByName(InetAddress.java:220) 
05-26 19:59:55.777: W/System.err(3103):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71) 
05-26 19:59:55.777: W/System.err(3103):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50) 
05-26 19:59:55.777: W/System.err(3103):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351) 
05-26 19:59:55.777: W/System.err(3103):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86) 
05-26 19:59:55.777: W/System.err(3103):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 
05-26 19:59:55.777: W/System.err(3103):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308) 
05-26 19:59:55.777: W/System.err(3103):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:303) 
05-26 19:59:55.777: W/System.err(3103):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282) 
05-26 19:59:55.777: W/System.err(3103):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232) 
05-26 19:59:55.797: W/System.err(3103):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80) 
05-26 19:59:55.807: W/System.err(3103):  at utils.SendEmail.doInBackground(SendEmail.java:38) 
05-26 19:59:55.807: W/System.err(3103):  at utils.SendEmail.doInBackground(SendEmail.java:1) 
05-26 19:59:55.819: W/System.err(3103):  at android.os.AsyncTask$2.call(AsyncTask.java:264) 
05-26 19:59:55.827: W/System.err(3103):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
05-26 19:59:55.827: W/System.err(3103):  at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
05-26 19:59:55.837: W/System.err(3103):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 
05-26 19:59:55.837: W/System.err(3103):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
05-26 19:59:55.847: W/System.err(3103):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
05-26 19:59:55.847: W/System.err(3103):  at java.lang.Thread.run(Thread.java:856) 
05-26 19:59:55.956: W/System.err(3103): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname) 
05-26 19:59:56.177: D/dalvikvm(3103): GC_CONCURRENT freed 406K, 6% free 9330K/9863K, paused 32ms+6ms 

하지만 예외가 정확하지 않습니다. 해당 호스트를 문제없이 해결할 수 있습니다. 그리고 BaseListActivity를 추가하기 전에 동일한 코드가 사용되었습니다.

감사합니다.

+0

어떤 종류의 어댑터를 사용하고 있습니까? 당신은'DownloadWebPageTask' 코드를 보여줄 수 있습니까? – Chopin

+0

@Chopin 덕분에 코드와 예외가 추가되었고 그 아래에 메모가 추가되었습니다. – GeekedOut

+0

Android Manifest.xml 파일에서 인터넷 액세스 권한을 부여 했습니까? – SALMAN

답변

1

ListViewListAdapteradapter.notifyDataSetChanged();를 호출과 동일 할 것입니다 reasigning, 난 당신이 데이터 공급자로 데이터베이스 Cursor 개체를 사용하는 경우 notifyDataSetChanged();에만 작동 생각합니다. adapter.notifyDataSetChanged(); 대신 setListAdapter();으로 전화하면 인턴 직원이 ListView을 다시 작성합니다.

+0

잘못되었습니다. 어댑터가 기반으로하는 데이터 구조의 종류에 관계없이 어댑터 notifyDataSetChanged() 메서드가 적합합니다. 그것은 커서 데이터에 대해서만 그렇다면 그것은 CursorAdapter에서 선언 될 것입니다, 그렇지 않습니까? :) –

+0

'notifyDataSetChanged' 메소드는'SimpleCursorAdapter'에서 가져 왔고'SimpleCursorAdapter'는'BaseAdapter'에서 가져온 것입니다. 그러나'ListAdapter'는'Base Adapter'가 아닌'Adapter'에서 왔습니다. 운영 체제 나는 틀렸다고 생각하지 않습니다. – Mayank

+0

당신이 틀리다 :),이 메소드는 BaseAdapter 클래스에서 가져옵니다. –

관련 문제