3min初识ListView(2)-getView和convertView

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/hxqneuq2012/article/details/52818184

今天我们进一步学习ListView,重点学习他的getView方法和其中的convertView对象。

首先我们将getCount方法里改成return 1000;然后快速滑动,快点,再快点。

这里推荐一个属性:fastScrollEnabled,设置一个垂直快速滚动条,拖动他,再快点。

android:fastScrollEnabled="true"


终于崩溃,这就对了。

查看日志,发现了传说中的OOM,

神马情况?

主要是我们快速滑动导致快速调用getView,

快速调用getView导致快速创建TextView,然后申请内存的速度>释放内存的速度。

创建虚拟机的VM Heap有限,终于导致OOM内存溢出。

我们可以log验证一下显示一个item调用一次getView():

            Log.d(TAG, "权兴权意-" + position);//显示一个调用一次




那么怎么办呢?这时就需要复用对象,我们用convertView历史缓存对象。

如果为空重新创建,如果不空则直接复用:

        //获取View,作为ListView的一个条目显示
        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            TextView tv;
            //View convertView 历史缓存对象
            if (convertView == null){
                tv = new TextView(MainActivity.this);
                Log.d(TAG, "创建ListView对象-" + position);//显示一个调用一次
            }else{
                tv = (TextView) convertView;
                Log.d(TAG, "复用ListView对象-" + position);//显示一个调用一次
            }
            //Log.d(TAG, "权兴权意-" + position);//显示一个调用一次
            //TextView tv = new TextView(MainActivity.this);
            tv.setText("权兴权意-" + position);
            return tv;
        }

这样就能避免OOM,给力的convertView。





展开阅读全文

android listview不运行getView

07-19

rnpackage com.dengq.caigou;rnrnimport java.io.BufferedReader;rnimport java.io.IOException;rnimport java.io.InputStream;rnimport java.io.InputStreamReader;rnimport java.util.ArrayList;rnimport java.util.HashMap;rnimport java.util.List;rnrnimport com.deng.service.DataNewsService;rnimport com.dengq.caigou.bean.DataField;rnimport com.dengq.main.R;rnrnimport android.os.Bundle;rnimport android.app.Activity;rnimport android.content.Context;rnimport android.util.Log;rnimport android.view.LayoutInflater;rnimport android.view.Menu;rnimport android.view.View;rnimport android.view.ViewGroup;rnimport android.widget.BaseAdapter;rnimport android.widget.EditText;rnimport android.widget.ListView;rnimport android.widget.TextView;rnrnpublic class CaigouActivity extends Activity rn ListView lstCaigou = null;rn EditText edtFind;rn List> data = null;rn //将数据加载到ArrayList数组里面,再在自定义适配器里面将数据加载进去rn ArrayList stdno = new ArrayList();rn ArrayList nameList = new ArrayList();rn ArrayList qgrList = new ArrayList();rn ArrayList num = new ArrayList();rn String sSql = "select top 10 F_ZDBH,F_WareName ,F_Num,F_qgr from T_Caigou ";rnrn @Overridern protected void onCreate(Bundle savedInstanceState) rn super.onCreate(savedInstanceState);rn setContentView(R.layout.activity_caigou);rn lstCaigou = (ListView) findViewById(R.id.lstCaigou);rn edtFind = (EditText) findViewById(R.id.edtFind);rn Thread thread = new Thread(runnable);rn thread.start();rn rnrn @Overridern public boolean onCreateOptionsMenu(Menu menu) rn // Inflate the menu; this adds items to the action bar if it is present.rn getMenuInflater().inflate(R.menu.caigou, menu);rn return true;rn rnrn public void findRecord(View v) rn Thread thread = new Thread(runnable);rn thread.start();rn rnrn Runnable runnable = new Runnable() rn @Overridern public void run() rn String sFind = edtFind.getText().toString();rn if (sFind.equals("")) rn else rn sSql = sSql + " where F_warename='" + sFind + "'";rn rn //post提交数据然后获取json数据rn getDataField(sSql);rn Log.d("sql", sSql);rn rn ;rnrn public void getDataField(String sSql) rn try rn //提交数据请求rn List dataFields = DataNewsService.postJSONLastNews(sSql,rn "T_caigou");rn data = new ArrayList>();rn for (DataField daField : dataFields) rn HashMap item = new HashMap();rn item.put("stdNo", daField.getStdNo());rn item.put("cName", daField.getFieldname());rn item.put("num", daField.getfNum());rn item.put("qgr", daField.getQgr());rn data.add(item);rn Log.d("test", data.toString());rn stdno.add(daField.getStdNo());rn nameList.add(daField.getFieldname());rn num.add(daField.getfNum());rn qgrList.add(daField.getQgr());rn rn[b][color=#FF0000] ////////////错误主要在这里rn //获取数据后加载适配器rn ListAdapter listAdapter = new ListAdapter(CaigouActivity.this);rn Log.d("test", "--000--");rn lstCaigou.setAdapter(listAdapter);//有时加载到这里就不向下运行rn Log.d("test", "---1111--");//getCoun返回值一般是10,每次返回两次rn listAdapter.notifyDataSetChanged();rn CaigouActivity.this.setContentView(lstCaigou);rn//////////////////////////////////////////////////////////////////[/color][/b]rn catch (Exception e) rn rn rn rn public final class ViewHolder rn public TextView stdNo;rn public TextView name;rn public TextView num;rn public TextView qgr;rn rnrn class ListAdapter extends BaseAdapter rn private LayoutInflater mInflater;rn private ViewHolder holder = null;rnrn public ListAdapter(Context context) rn this.mInflater = LayoutInflater.from(context);rn rn rn @Overridern public int getCount() rn Log.d("testgetCount", String.valueOf(nameList.size()));rn return nameList.size();rn rnrn @Overridern public Object getItem(int position) rn Log.d("testgetItem", String.valueOf(nameList.get(position)));rn return nameList.get(position);rn rnrn @Overridern public long getItemId(int position) rn Log.d("getItemIdtest", String.valueOf(position));rn return position;rn rnrn @Overridern public View getView(int position, View convertView, ViewGroup parent) rn Log.d("test", "0001");rn if (convertView == null) rn holder = new ViewHolder();rn convertView = mInflater.inflate(R.layout.caigoulist, null);rn holder.stdNo = (TextView) convertView.findViewById(R.id.stdNo);rn holder.name = (TextView) convertView.findViewById(R.id.name);rn holder.num = (TextView) convertView.findViewById(R.id.num);rn holder.qgr = (TextView) convertView.findViewById(R.id.qgr);rn convertView.setTag(holder);rn Log.d("test", "0001-1");rn else rn holder = (ViewHolder) convertView.getTag();rn rn //将ArrayList的数据加载到适配器里rn Log.d("test", "0002");rn final String s = (String) stdno.get(position);rn holder.stdNo.setText(s);rn final String s1 = (String) nameList.get(position);rn holder.name.setText(s1);rn final String s2 = (String) qgrList.get(position);rn holder.qgr.setText(s2);rn final Integer integer = (Integer) num.get(position);rn holder.num.setText(integer);rn Log.d("test", "0003"); rn return convertView;rn rn rnrnrnpublic class DataField rn private String stdNo;rn private String fieldname;rn private int fNum;rn private String qgr;rn public DataField(String stdNo, String fieldname, int fNum, String qgr) rn super();rn this.stdNo = stdNo;rn this.fieldname = fieldname;rn this.fNum = fNum;rn this.qgr = qgr;rn rnrn public String getQgr() rn return qgr;rn rn public void setQgr(String qgr) rn this.qgr = qgr;rn rn public String getStdNo() rn return stdNo;rn rn public void setStdNo(String stdNo) rn this.stdNo = stdNo;rn rn public String getFieldname() rn return fieldname;rn rn public void setFieldname(String fieldname) rn this.fieldname = fieldname;rn rn public int getfNum() rn return fNum;rn rn public void setfNum(int fNum) rn this.fNum = fNum;rn rnrnrn 论坛

没有更多推荐了,返回首页