androidListView常用知識總結(jié)

先來看下項(xiàng)目主要內(nèi)容:

創(chuàng)新互聯(lián)專注于做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、網(wǎng)頁設(shè)計(jì)、網(wǎng)站制作、網(wǎng)站開發(fā)。公司秉持“客戶至上,用心服務(wù)”的宗旨,從客戶的利益和觀點(diǎn)出發(fā),讓客戶在網(wǎng)絡(luò)營銷中找到自己的駐足之地。尊重和關(guān)懷每一位客戶,用嚴(yán)謹(jǐn)?shù)膽B(tài)度對待客戶,用專業(yè)的服務(wù)創(chuàng)造價(jià)值,成為客戶值得信賴的朋友,為客戶解除后顧之憂。

android ListView常用知識總結(jié)

ListView中填充數(shù)據(jù):

  1. 重現(xiàn)添加數(shù)據(jù)后置頂,具體闡明了決解方案,如下:

    android ListView常用知識總結(jié)

  2. 刷新適配器后沒有響應(yīng)的錯誤現(xiàn)象,具體闡明了決解方案,如下:

    android ListView常用知識總結(jié)

    android ListView常用知識總結(jié)

  3. 正確示范一:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    /**
    * 正確示范一(正確運(yùn)用,修改原始對象<Activity里面>對應(yīng)引用<Adapter里面>也改變)
    *
    * @author johnny
    *
    */
    publicclassThreeListViewActivity extendsActivity {
    privateListView mContentLv;
    privateOneAdapter adapter;
    privateArrayList<HashMap<String, String>> arrayList=newArrayList<HashMap<String,String>>();
    @Override
    protectedvoidonCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listview);
    mContentLv = (ListView) findViewById(R.id.lv_content);
    adapter=newOneAdapter(this,arrayList);
    mContentLv.setAdapter(adapter);
    initData();
    ToastUtils.show(getApplicationContext(), "6秒后延遲添加,刷新adapter");
    //開啟異步線程
    setData();
    }
    privatevoidsetData() {
    newThread(newRunnable() {
    @Override
    publicvoidrun() {
    // TODO Auto-generated method stub
    try{
    //做一些耗時的操作后添加數(shù)據(jù),之后刷新adapter
    Thread.sleep(6000);
    } catch(InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    //可能這個時候才重網(wǎng)絡(luò)上獲取到了新數(shù)據(jù),,現(xiàn)在開始添加數(shù)據(jù)
    HashMap<String, String> hashMap;
    for(inti = 0; i < 5; i++) {
    hashMap = newHashMap<String, String>();
    if(i % 4== 0) {
    hashMap.put("name", "大海");
    hashMap.put("address", "上海");
    } elseif(i % 4== 1) {
    hashMap.put("name", "老馬");
    hashMap.put("address", "深圳");
    } elseif(i % 4== 2) {
    hashMap.put("name", "小三");
    hashMap.put("address", "東莞");
    } elseif(i % 4== 3) {
    hashMap.put("name", "老哥");
    hashMap.put("address", "北京");
    }
    arrayList.add(hashMap);
    }
    handler.sendEmptyMessage(1);
    }
    }).start();
    }
    Handler handler = newHandler(){
    publicvoidhandleMessage(android.os.Message msg) {
    adapter.notifyDataSetChanged();//沒有重現(xiàn)設(shè)置adapter,只做了刷新數(shù)據(jù),listview不會到頂。
    ToastUtils.show(getApplicationContext(), "adapter共有"+adapter.getAllList().size()+"個數(shù)據(jù),activity共有"+arrayList.size()+"個數(shù)據(jù),刷新結(jié)束!");
    };
    };
    privatevoidinitData() {
    HashMap<String, String> hashMap;
    for(inti = 0; i < 15; i++) {
    hashMap = newHashMap<String, String>();
    if(i % 4== 0) {
    hashMap.put("name", "小明");
    hashMap.put("address", "上海");
    } elseif(i % 4== 1) {
    hashMap.put("name", "老馬");
    hashMap.put("address", "深圳");
    } elseif(i % 4== 2) {
    hashMap.put("name", "小三");
    hashMap.put("address", "東莞");
    } elseif(i % 4== 3) {
    hashMap.put("name", "老哥");
    hashMap.put("address", "北京");
    }
    arrayList.add(hashMap);
    }
    }
    }


    正確示范二:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    /**
    * 正確示范二(正確運(yùn)用,Activity里面對象不用作Adapter原始對象,只做數(shù)據(jù)的備份。
    * 這也是本人比較喜歡的寫法,原因:很多時候我們會不經(jīng)意間改變Activity里面的數(shù)據(jù)源后導(dǎo)致ListView的改變,
    * 導(dǎo)致出錯后排除錯誤難解<正確示范一,當(dāng)然也有好處,比如:改變Activity數(shù)據(jù)后,不需要再做多余的Adapter數(shù)據(jù)的更改,方便>)
    *
    * @注意 這里只是數(shù)據(jù)添加方案本人贊成寫法,具體完整結(jié)合adapter請移步個人推薦一或二<有錯誤示范,才能慢慢完善改變,錯誤何嘗不是一種成長>
    * @author johnny
    *
    */
    publicclassFourListViewActivity extendsActivity {
    privateListView mContentLv;
    privateOneAdapter adapter;
    @Override
    protectedvoidonCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listview);
    mContentLv = (ListView) findViewById(R.id.lv_content);
    adapter=newOneAdapter(this);
    mContentLv.setAdapter(adapter);
    initData();
    ToastUtils.show(getApplicationContext(), "6秒后延遲添加,刷新adapter");
    //開啟異步線程
    setData();
    }
    privatevoidsetData() {
    newThread(newRunnable() {
    @Override
    publicvoidrun() {
    // TODO Auto-generated method stub
    try{
    //做一些耗時的操作后添加數(shù)據(jù),之后刷新adapter
    Thread.sleep(6000);
    } catch(InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    ArrayList<HashMap<String, String>> arrayList=newArrayList<HashMap<String,String>>();
    //可能這個時候才重網(wǎng)絡(luò)上獲取到了新數(shù)據(jù),,現(xiàn)在開始添加數(shù)據(jù)
    HashMap<String, String> hashMap;
    for(inti = 0; i < 5; i++) {
    hashMap = newHashMap<String, String>();
    if(i % 4== 0) {
    hashMap.put("name", "大海");
    hashMap.put("address", "上海");
    } elseif(i % 4== 1) {
    hashMap.put("name", "老馬");
    hashMap.put("address", "深圳");
    } elseif(i % 4== 2) {
    hashMap.put("name", "小三");
    hashMap.put("address", "東莞");
    } elseif(i % 4== 3) {
    hashMap.put("name", "老哥");
    hashMap.put("address", "北京");
    }
    //這里添加的只是在臨時數(shù)據(jù)存儲的對象,adapter里面不屬于同一個對象
    arrayList.add(hashMap);
    }
    //和正確范例一區(qū)別在于此,每次都需要把數(shù)據(jù)copy到adapter里面
    adapter.setAddList(arrayList);
    handler.sendEmptyMessage(1);
    }
    }).start();
    }
    Handler handler = newHandler(){
    publicvoidhandleMessage(android.os.Message msg) {
    adapter.notifyDataSetChanged();//沒有重現(xiàn)設(shè)置adapter,只做了刷新數(shù)據(jù),ListView不會到頂。
    ToastUtils.show(getApplicationContext(), "adapter共有"+adapter.getAllList().size()+"個數(shù)據(jù)。");
    };
    };
    privatevoidinitData() {
    ArrayList<HashMap<String, String>> arrayList=newArrayList<HashMap<String,String>>();
    HashMap<String, String> hashMap;
    for(inti = 0; i < 15; i++) {
    hashMap = newHashMap<String, String>();
    if(i % 4== 0) {
    hashMap.put("name", "小明");
    hashMap.put("address", "上海");
    } elseif(i % 4== 1) {
    hashMap.put("name", "老馬");
    hashMap.put("address", "深圳");
    } elseif(i % 4== 2) {
    hashMap.put("name", "小三");
    hashMap.put("address", "東莞");
    } elseif(i % 4== 3) {
    hashMap.put("name", "老哥");
    hashMap.put("address", "北京");
    }
    arrayList.add(hashMap);
    }
    adapter.setAddList(arrayList);
    }
    }



    ListView 適配器推薦寫法:


    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      @Override
      publicView getView(intposition, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
      View v = convertView;
      //判斷是否為null,這是每個要做復(fù)用的都要寫的
      if(v == null) {
      v = inflater.inflate(R.layout.lv_item, null);
      }
      //相比之下
      //寫法太集中,沒有細(xì)化
      /*if (null == convertView) {
      holder = new ViewHolder();
      convertView = inflater.inflate(R.layout.lv_item, null);
      holder.mNameTv = (TextView) convertView.findViewById(R.id.tv_name);
      holder.mAddressTv = (TextView) convertView.findViewById(R.id.tv_address);
      convertView.setTag(holder);
      } else {
      holder = (ViewHolder) convertView.getTag();
      }*/
      //簡潔,把初始化控件放到了ViewHolder里面,是的getView方法更清晰簡潔,只需要做數(shù)據(jù)的賦值和復(fù)雜的邏輯,沒有混為一灘
      ViewHolder holder = (ViewHolder) v.getTag();    
      if(holder == null) {
      holder = newViewHolder(v);
      v.setTag(holder);
      }
      HashMap<String, String> hashMap = getItem(position);
      holder.mNameTv.setText(hashMap.get("name"));
      holder.mAddressTv.setText(hashMap.get("address"));
      returnv;
      }



    方法二:

    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      @Override
      publicView getView(intposition, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
      View v = convertView;
      //判斷是否為null,這是每個要做復(fù)用的都要寫的
      if(v == null) {
      v = inflater.inflate(R.layout.lv_item, null);
      }
      //簡潔,方便,快速
      TextView mNameTv=ViewHolder.get(v, R.id.tv_name);  
      TextView mAddressTv=ViewHolder.get(v, R.id.tv_address);  
      HashMap<String, String> hashMap = getItem(position);
      mNameTv.setText(hashMap.get("name"));
      mAddressTv.setText(hashMap.get("address"));
      returnv;
      }



ListView中疑難雜癥:

只做截圖具體下載代碼查看:不需要豆子


eandroid ListView常用知識總結(jié)

android ListView常用知識總結(jié)

另鏈接單選刪除帖子效果如下:

android ListView常用知識總結(jié)

地址:點(diǎn)擊進(jìn)入


×××:

ListView中常用知識總結(jié)

當(dāng)前名稱:androidListView常用知識總結(jié)
路徑分享:http://muchs.cn/article18/gjgidp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)靜態(tài)網(wǎng)站、電子商務(wù)、做網(wǎng)站、品牌網(wǎng)站制作、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司