当前位置: 首页 > 知识库问答 >
问题:

继承BaseListActivity并使用AsyncTask时,ListActivity不更新

高德水
2023-03-14

我有一个类SomeActivity扩展了BaseListActivity。BaseListActivity扩展了ListActivity和所有工作的功能。

但当列表必须更新时,我使用AsyncTask使用远程服务器调用,该调用声明如下:

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

在那里,有一行可以像这样更新列表:

adapter.notifyDataSetChanged();

但这条线似乎起不到什么作用。

有什么不同之处可以让它发挥作用吗?

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);
                }
            });
        }
    }
}
        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之前,这段代码可以正常工作。

谢谢!

共有1个答案

叶景龙
2023-03-14

ListAdapter重新定位到ListView将与调用Adapter.NotifyDataSetChanged()相同;,我认为NotifyDataSetChanged();仅在使用数据Cursor对象作为数据提供程序时有效。而不是Adapter.NotifyDataSetChanged();只需调用SetListAdapter();,该实习生将重新生成ListView

 类似资料:
  • 我有一个非规范化的数据库和一个包含多个类的领域模型。例如 我想为EngineConfiguration、ModelMetadata和BodyConfiguration编写MapStruct映射程序,然后将它们合并到一个映射程序中。实例 问题是我的映射器默认情况下不工作,BMWMapper没有对其他映射器的调用,我尝试了调用的表达式,如,但映射器不包含在实现中。我如何实现这一点?附言:MapperC

  • 问题内容: 偏重于继承而不是继承 是非常流行的短语。我读了几篇文章,最后每篇文章都说 当类之间存在纯IS-A关系时,请使用继承。 本文中的一个示例: 在 Apple 和 Fruit 之间存在明显的IS-A关系,即Apple IS-A Fruit,但作者也将其显示为Apple HAS-A Fruit(组成),以显示通过继承实现时的陷阱。 我在这里变得有些困惑,声明的含义是什么 当类之间存在纯IS-A

  • webresources定义如下。这个类是由NetBeans自动添加的。

  • 将所有服务器都放在一个篮子里的系统管理员是勇敢的(或者说是愚蠢的)。 比方说,你将服务器托管在三个不同的提供商:WreckSpace、GoDodgy 和 VerySlow。 他们有不同的数据中心且分布在不同的地理位置,所以你需要对服务器配置做一些小的改动, 以适应每个提供商。你有几种类型的服务器,它们随机分布在三个不同的提供商。 使用 Puppet 的一种实现方法是在节点定义中设置一个变量用于告知

  • 问题内容: 我正在使用Flask和SQLAlchemy。我使用了自己的抽象基类和继承。当我尝试在python shell中使用模型时,出现以下错误: 我该如何解决? 码: manage.py: init.py: database.py: models.py: controllers.py: 问题答案: 尝试添加 在你的User类下

  • 问题内容: 我是刚开始学习Python的Java人。举个例子: 我肯定有很多冗余代码(我知道在Java中,上面的代码有很多冗余)。 对于已经从父类继承了哪些属性,哪些部分是多余的? 问题答案: 在python中为类编写函数时,应始终调用其超类的函数。我们可以使用它直接将相关属性传递给超类,因此您的代码如下所示: 正如其他人指出的那样,您可以替换该行 与 并且代码将执行相同的操作。这是因为pytho