1

我正在尝试解析 xml 文件并尝试加载图像和文本视图并将其显示在列表视图中,但是每当我尝试在 getView 方法中加载图像时,即使尝试快速滚动,它也会强制关闭应用程序。我厌倦了在线程和异步任务中做 5 个小时。如果有人可以解决它,请提供帮助。这是我的两个类文件。

class NewsRowAdapter

public class NewsRowAdapter extends ArrayAdapter<Item> 
{
    LoadingImage loadingImage;

    Bitmap bitmap = null;

    private Activity activity;

    private List<Item> items;

    private Item objBean;

    private int row;

    public NewsRowAdapter(Activity act, int resource, List<Item> arrayList) 
    {

        super(act, resource, arrayList);

        this.activity = act;

        this.row = resource;

        this.items = arrayList;


    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        View view = convertView;

        final ViewHolder holder;

        if (view == null) 
        {

            LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            view = inflater.inflate(row, null);

            holder = new ViewHolder();

            view.setTag(holder);

        } else 
        {
            holder = (ViewHolder) view.getTag();
        }

        if ((items == null) || ((position + 1) > items.size()))

            return view;

        objBean = items.get(position);

        holder.tvTitle = (TextView) view.findViewById(R.id.tvtitle);

        holder.tvDesc = (TextView) view.findViewById(R.id.tvdesc);

        holder.tvDate = (TextView) view.findViewById(R.id.tvdate);

        holder.imgView = (ImageView) view.findViewById(R.id.image);

        holder.pbar = (ProgressBar) view.findViewById(R.id.pbar);

        if (holder.tvTitle != null && null != objBean.getTitle() && objBean.getTitle().trim().length() > 0) 
        {
            holder.tvTitle.setText(Html.fromHtml(objBean.getTitle()));
        }
        if (holder.tvDesc != null && null != objBean.getDesc() && objBean.getDesc().trim().length() > 0)
        {
            holder.tvDesc.setText(Html.fromHtml(objBean.getDesc()));
        }
        if (holder.tvDate != null && null != objBean.getPubdate() && objBean.getPubdate().trim().length() > 0)
        {
            holder.tvDate.setText(Html.fromHtml(objBean.getPubdate()));
        }
        if (holder.imgView != null) 
        {
            if (null != objBean.getLink() && objBean.getLink().trim().length() > 0) 
            {
                final ProgressBar pbar = holder.pbar;

                pbar.setVisibility(View.INVISIBLE);


                //---------CHANGES MADE FOR LOADING IMAGE----------//

                Log.d("IMAGE NULL----------", objBean.getLink());


                //loadBitmap(objBean.getLink());

                /*new Thread() 
                {
                    public void run() 
                    {*/
                        try 
                        {
                            URL linkurl = new URL(objBean.getLink());

                            bitmap = BitmapFactory.decodeStream(linkurl.openConnection().getInputStream());

                            holder.imgView.setImageBitmap(bitmap);

                        } catch (MalformedURLException e) 
                        {
                            e.printStackTrace();

                        } catch (IOException e) 
                        {
                            e.printStackTrace();
                        }
                    /*}
                }.start();*/



            } else 
            {
                holder.imgView.setImageResource(R.drawable.ic_launcher);
            }
        }

        return view;
    }


    //------LOADING IMAGE FROM URL------//


    public static Bitmap loadBitmap(String url) 
    {
        Bitmap bitmap = null;

        final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
        final HttpGet getRequest = new HttpGet(url);

        try 
        {
            HttpResponse response = client.execute(getRequest);
            final int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) 
            { 
                Log.d("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url); 
                return null;
            }

            final HttpEntity entity = response.getEntity();
            if (entity != null)
            {
                InputStream inputStream = null;
                try 
                {
                    inputStream = entity.getContent(); 

                    bitmap = BitmapFactory.decodeStream(inputStream);



                } finally
                {
                    if (inputStream != null)
                    {
                        inputStream.close();  
                    }
                    entity.consumeContent();
                }
            }
        } catch (Exception e) 
        {
            // Could provide a more explicit error message for IOException or IllegalStateException
            getRequest.abort();

            Log.d("Error while retrieving bitmap from " + url, e.toString());
        } finally 
        {
            if (client != null)
            {
                client.close();
            }
        }


        return bitmap;
    }




    public class ViewHolder
    {

        public TextView tvTitle, tvDesc, tvDate;
        private ImageView imgView;
        private ProgressBar pbar;

    }

}

主要课程是:

类 MainActivity

public class MainActivity extends Activity implements OnItemClickListener
{
    private static final String rssFeed = /*"https://www.dropbox.com/s/t4o5wo6gdcnhgj8/imagelistview.xml?dl=1"*/"http://78.46.34.27/kapapps/newparsedtransaction.xml";

    List<Item> arrayOfList;
    ListView listView;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainnewtransaction);

        listView = (ListView) findViewById(R.id.listview);
        listView.setOnItemClickListener(this);

        if (Utils.isNetworkAvailable(NewTransactionActivity.this)) 
        {
            new MyTask().execute(rssFeed);
        } else 
        {
            showToast("No Network Connection!!!");
        }

    }

    // My AsyncTask start...

    class MyTask extends AsyncTask<String, Void, Void> 
    {

        ProgressDialog pDialog;

        @Override
        protected void onPreExecute() 
        {
            super.onPreExecute();

            pDialog = new ProgressDialog(NewTransactionActivity.this);
            pDialog.setTitle("Latest Transaction");
            pDialog.setMessage("Loading... Please wait");
            pDialog.show();

        }

        @Override
        protected Void doInBackground(String... params) 
        {
            arrayOfList = new NamesParser().getData(params[0]);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) 
        {
            super.onPostExecute(result);



            if (null == arrayOfList || arrayOfList.size() == 0) 
            {

                showToast("No data found from web!!!");

                NewTransactionActivity.this.finish();

            } else 
            {

                // check data...
                /*
                 * for (int i = 0; i < arrayOfList.size(); i++) 
                 * { 
                 *  Item item = arrayOfList.get(i); System.out.println(item.getId());
                 *  System.out.println(item.getTitle());
                 *  System.out.println(item.getDesc());
                 *  System.out.println(item.getPubdate());
                 *  System.out.println(item.getLink()); 
                 * }
                 */
                for(int i = 0 ; i < arrayOfList.size() ; i++)
                {   
                    Item item = arrayOfList.get(i);
                    Log.d("ID NEWTRANSACTION ACTIVITY ------>" , item.getId());
                    Log.d("TITLE NEWTRANSACTION ACTIVITY ------>" , item.getTitle());
                    Log.d("DESC NEWTRANSACTION ACTIVITY ------>", item.getDesc());
                    Log.d("LINK NEWTRANSACTION ACTIVITY ------>", item.getLink());
                }

                setAdapterToListview();


            }


            if (null != pDialog && pDialog.isShowing()) 
            {
                pDialog.dismiss();
            }




        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent , View view , int position ,  long id) 
    {
        Item item = arrayOfList.get(position);
        Intent intent = new Intent(NewTransactionActivity.this, DetailActivity.class);
        intent.putExtra("url", item.getLink());
        intent.putExtra("title", item.getTitle());
        intent.putExtra("desc", item.getDesc());


        Log.d("IMAGE_URL------>" , item.getLink());
        startActivity(intent);
    }

    public void setAdapterToListview() 
    {
        NewsRowAdapter objAdapter = new NewsRowAdapter(NewTransactionActivity.this , R.layout.row, arrayOfList);
        listView.setAdapter(objAdapter);
    }

    public void showToast(String msg) 
    {

    }


}
4

3 回答 3

1

在另一个线程中执行图像检索逻辑。加载图像需要太多时间,这就是你得到 ANR 的原因。

于 2013-02-07T14:19:45.463 回答
0

使用单个工作线程,并使其可以在活动的 onPause() 中停止。好的方法是使用 SingleThread Executor 服务来加载图像。

这是一个示例https://stackoverflow.com/a/14579365/1366471

于 2013-02-07T14:27:26.817 回答
0

我建议使用RemoteImageViewPrime中的。它大大减少了你的工作量。

在您的布局中,替换ImageViewcom.handlerexploit.prime.widgets.RemoteImageView和 在您的代码中,更改ImageViewRemoteImageView在您的持有人类中。

getView方法中,

holder.imgView = (RemoteImageView) view.findViewById(R.id.image);
//...
holder.imgView.setImageURL(objBean.getLink());
于 2013-02-07T14:30:36.270 回答