android ListView常用知識總結


先來看下項目主要內容:


wKioL1MG6dnTgOurAAEoUfGWu_c852.jpg


ListView中填充數據:

  1. 重現添加數據後置頂,具體闡明瞭決解方案,如下:

    wKiom1MG69bjhFitAAK1pxNefks445.jpg


  2. 刷新適配器後沒有響應的錯誤現象,具體闡明瞭決解方案,如下:

    wKioL1MG7C7R7xIjAAJwpVyuO-M060.jpg

    wKioL1MG7C_CQKyCAASFjBTVk44098.jpg



  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
    /**
    * 正確示範一(正確運用,修改原始對象<Activity裏面>對應引用<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{
    //做一些耗時的操作後添加數據,之後刷新adapter
    Thread.sleep(6000);
    } catch(InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    //可能這個時候才重網絡上獲取到了新數據,,現在開始添加數據
    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();//沒有重現設置adapter,只做了刷新數據,listview不會到頂。
    ToastUtils.show(getApplicationContext(), "adapter共有"+adapter.getAllList().size()+"個數據,activity共有"+arrayList.size()+"個數據,刷新結束!");
    };
    };
    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
    /**
    * 正確示範二(正確運用,Activity裏面對象不用作Adapter原始對象,只做數據的備份。
    * 這也是本人比較喜歡的寫法,原因:很多時候我們會不經意間改變Activity裏面的數據源後導致ListView的改變,
    * 導致出錯後排除錯誤難解<正確示範一,當然也有好處,比如:改變Activity數據後,不需要再做多餘的Adapter數據的更改,方便>)
    *
    * @注意 這裏只是數據添加方案本人贊成寫法,具體完整結合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{
    //做一些耗時的操作後添加數據,之後刷新adapter
    Thread.sleep(6000);
    } catch(InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    ArrayList<HashMap<String, String>> arrayList=newArrayList<HashMap<String,String>>();
    //可能這個時候才重網絡上獲取到了新數據,,現在開始添加數據
    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", "北京");
    }
    //這裏添加的只是在臨時數據存儲的對象,adapter裏面不屬於同一個對象
    arrayList.add(hashMap);
    }
    //和正確範例一區別在於此,每次都需要把數據copy到adapter裏面
    adapter.setAddList(arrayList);
    handler.sendEmptyMessage(1);
    }
    }).start();
    }
    Handler handler = newHandler(){
    publicvoidhandleMessage(android.os.Message msg) {
    adapter.notifyDataSetChanged();//沒有重現設置adapter,只做了刷新數據,ListView不會到頂。
    ToastUtils.show(getApplicationContext(), "adapter共有"+adapter.getAllList().size()+"個數據。");
    };
    };
    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,這是每個要做複用的都要寫的
      if(v == null) {
      v = inflater.inflate(R.layout.lv_item, null);
      }
      //相比之下
      //寫法太集中,沒有細化
      /*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方法更清晰簡潔,只需要做數據的賦值和複雜的邏輯,沒有混爲一灘
      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,這是每個要做複用的都要寫的
      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中疑難雜症:

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


ewKioL1MG8vTAT3GCAADv2ZjA1Bo813.jpg

wKiom1MG8xzAihOqAAD0qsXBE8M380.jpg


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

wKiom1MG9sWSrpJ5AASZloojMbc746.jpg

地址:點擊進入


源碼下載:

ListView中常用知識總結


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章