2

我有一个简单的活动,它通过 ArrayAdapter 将一些数据绑定到 listView。我在我的 onCreate 中敬酒。如果我让手机静置几秒钟,吐司就会再次出现。我完全不知道这是怎么发生的。我在下面发布我的代码。也许我错过了一些我不知道的应用程序不可或缺的东西。

package com.eghdk.myapp.gui;

import java.util.ArrayList;

import android.annotation.TargetApi;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;
import com.eghdk.myapp.R;
import com.eghdk.myapp.adapters.MyAdapter;
import com.eghdk.myapp.util.AppUtil;
import com.eghdk.myapp.util.MyPost;

public class ActivityMyBlog extends ListActivity {
    MyAdapter adapter;
    ArrayList<MyPost> myPostArrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_blog);
        // Show the Up button in the action bar.
        setupActionBar();

        
        if (!AppUtil.isNetworkAvailable(this)) {
            Log.d("", "No network.");

        } else {
            Log.d("", "Connection detected. Will try to load from web.");
            loadDataFromWeb(1, 40);
        }

        if (adapter == null) {

            Toast.makeText(this, "adapter is null", 0).show();

        } else {
        }

    }

    @Override
    public Object getLastNonConfigurationInstance() {
        return (getListAdapter());
    }

    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    // @Override
    // public boolean onCreateOptionsMenu(Menu menu) {
    // // Inflate the menu; this adds items to the action bar if it is present.
    // getMenuInflater().inflate(R.menu.activity_my_blog, menu);
    // return true;
    // }
    //
    // @Override
    // public boolean onOptionsItemSelected(MenuItem item) {
    // switch (item.getItemId()) {
    // case android.R.id.home:
    // // This ID represents the Home or Up button. In the case of this
    // // activity, the Up button is shown. Use NavUtils to allow users
    // // to navigate up one level in the application structure. For
    // // more details, see the Navigation pattern on Android Design:
    // //
    // //
    // http://developer.android.com/design/patterns/navigation.html#up-vs-back
    // //
    // NavUtils.navigateUpFromSameTask(this);
    // return true;
    // }
    // return super.onOptionsItemSelected(item);
    // }

    public void loadDataFromWeb(int page, int count) {

        myPostArrayList = new ArrayList<MyPost>();

        Ion.with(
                ActivityMyBlog.this,
                "http://myblog.com/api/get_posts/?page=" + page + "&count="
                        + count).asJsonObject()
                .setCallback(new FutureCallback<JsonObject>() {
                    @Override
                    public void onCompleted(Exception e, JsonObject result) {

                        //Edited this json code out for easier reading

                            myPost.setAttachmenUrl("http://myblog.com/img.png");
                            myPost.setPostTitle("Title");
                            myPost.setPostContent("Content");
                            myPost.setPostUrl("http://myblog.com/mypost");
                            myPost.setAuthorName("EGHDK");

                            myPostArrayList.add(myPost);

                        }
                        // END OF EVERY POST LOOP
                        Log.d("DEBUG", "4");
                        adapter = new MyAdapter(ActivityMyBlog.this,
                                R.layout.row, myPostArrayList);
                        setListAdapter(adapter);
                    }
                });

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        Intent intent = new Intent(this, ActivityMyBlogDetail.class);
        intent.putExtra("atUrl", myPostArrayList.get(position)
                .getAttachmenUrl());
        intent.putExtra("content", myPostArrayList.get(position)
                .getPostContent());
        intent.putExtra("title", myPostArrayList.get(position)
                .getPostTitle());
        intent.putExtra("url", myPostArrayList.get(position).getPostUrl());

        startActivity(intent);
    }
}

这是我的isNetworkAvailable方法:

    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();

        boolean isAvailable = false;

        if (networkInfo != null && networkInfo.isConnected()) {

            isAvailable = true;
        }

        return isAvailable;
    }

这是我的 ArrayAdapter 中的 getView 方法:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.row, parent, false);
        }
        ((TextView) convertView.findViewById(R.id.titleTextView)).setText(Html
                .fromHtml(postsArrayList.get(position).getPostTitle()));
        ImageView image = (ImageView) convertView
                .findViewById(R.id.postImageView);
            //Library to help with loading of images
        UrlImageViewHelper.setUrlDrawable(image, postsArrayList.get(position)
                .getAttachmenUrl());
        ((TextView) convertView.findViewById(R.id.subTextView)).setText(Html
                .fromHtml(postsArrayList.get(position).getAuthorName()));

        return convertView;
    }

更新:

我有另一部手机用作秒表并尝试了两次。

两次我都强制关闭了应用程序。

  1. 我打开应用程序,屏幕在 2 分 45 秒后变暗,我敬酒。

  2. 我打开应用程序和屏幕 48 秒后我得到了祝酒词。

这个不对。我确定 android 应用程序生命周期不会像这样工作。一定是另一个问题。我在我的 Android 4.4 LG G2 上使用它。

更新 2:

插入日志

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    Log.d("STOP", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
}

结果:

01-20 04:42:51.792: D/STOP(4817): XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
01-20 04:42:51.852: D/(4817): No network. Will use values from database.
01-20 04:42:51.922: I/ActivityManager(4817): Timeline: Activity_idle id: android.os.BinderProxy@420465c0 time:15096283
01-20 04:42:52.432: D/STOP(4817): XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
01-20 04:42:52.512: D/(4817): No network. Will use values from database.
4

0 回答 0