程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> listview-求大神,Android代碼優化

listview-求大神,Android代碼優化

編輯:編程解疑
求大神,Android代碼優化

public class MyListView4 extends ListActivity {


    private List<Map<String, Object>> mData;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mData = getData();
        MyAdapter adapter = new MyAdapter(this);
        setListAdapter(adapter);
    }

    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", "G1");
        map.put("info", "google 1");
        map.put("img","url1" );
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", "G2");
        map.put("info", "google 2");
        map.put("img", "url2");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", "G3");
        map.put("info", "google 3");
        map.put("img", "url3");
        list.add(map);

        return list;
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        Log.v("MyListView4-click", (String)mData.get(position).get("title"));
    }

    public void showInfo(){
        new AlertDialog.Builder(this)
                .setTitle("我的listview")
                .setMessage("介紹...")
                .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .show();

    }




    public class MyAdapter extends BaseAdapter{

        private LayoutInflater mInflater;


        public MyAdapter(Context context){
            this.mInflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mData.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view = mInflater.inflate(R.layout.vlist2, null);
            ImageView img = (ImageView)view.findViewById(R.id.img);
            TextView title = (TextView)view.findViewById(R.id.title);
            TextView info = (TextView)view.findViewById(R.id.info);
            Button viewBtn = (Button)view.findViewById(R.id.view_btn);

            img.setBackgroundResource((Integer)mData.get(position).get("img"));
            title.setText((String)mData.get(position).get("title"));
            info.setText((String)mData.get(position).get("info"));

            viewBtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    showInfo();
                }
            });

            return view;
        }

    }


最佳回答:


listview的優化,主要是在adapter的getview中1.復用view減少findViewById的次數 下面只供參考、、、

public View getView(int position, View convertView, ViewGroup parent) {
MyHolder holder;
if(convertView==null){
convertView =
LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);
holder = new MyHolder();
holder.artist =
(TextView) convertView.findViewById(R.id.item_artist);
holder.duration =
(TextView) convertView.findViewById(R.id.item_duration);
convertView.setTag(holder);
}else{
holder = (MyHolder) convertView.getTag();
}
Music m = getItem(position);
holder.artist.setText(m.getArtist()); holder.duration.setText(dateFormat.format(new
Date(m.getDuration())));
return convertView;
}}
public class MyHolder{
private TextView title;
private TextView artist;
private TextView duration;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved