Android列表組件ListView使用詳解之動(dòng)態(tài)加載或修改列表數(shù)據(jù)
在使用ListView組件來(lái)顯示列表數(shù)據(jù)時(shí),有的時(shí)候我們需要改變列表中的數(shù)據(jù),有以下方法:
1、重新給ListView組件設(shè)置適配器
這種方法重新創(chuàng)建了ListView,效率不好。
2、使用適配器中的方法
/**
* Notifies the attached observers that the underlying data has been changed
* and any View reflecting the data set should refresh itself.
*/
public void notifyDataSetChanged() {
mDataSetObservable.notifyChanged();
}
這種方法旨在告知適配器,ListView中的數(shù)據(jù)源發(fā)生變化,需要重新加載新的數(shù)據(jù),不會(huì)重新創(chuàng)建ListView。使用此方法時(shí),需要確保使用的是同一數(shù)據(jù)存儲(chǔ)對(duì)象,只是存儲(chǔ)對(duì)象中的值發(fā)生變化,才能使改動(dòng)生效。關(guān)鍵代碼如下:
listViewDemoAdapter.notifyDataSetChanged();
效果圖:

當(dāng)前界面顯示的代碼:
package net.oschina.git.zhaikun.androiddeveloped.activitys;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import net.oschina.git.zhaikun.androiddeveloped.R;
import net.oschina.git.zhaikun.androiddeveloped.adapter.ListViewDemoAdapter;
import java.util.ArrayList;
import java.util.List;
/**
* Created by zhaikun68 on 2018/3/5.
* <p>
* ListView演示Demo
*/
public class ListViewDemoActivity extends AppCompatActivity implements View.OnClickListener {
private ListView testLv;//ListView組件
private Button updateDataBtn;//動(dòng)態(tài)加載數(shù)據(jù)組件
private List<String> dataList = new ArrayList<>();//存儲(chǔ)數(shù)據(jù)
private ListViewDemoAdapter listViewDemoAdapter;//ListView的數(shù)據(jù)適配器
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview_demo);
initView();//初始化組件
initData();//初始化數(shù)據(jù)
}
/**
* 初始化組件
*/
private void initView() {
testLv = (ListView) findViewById(R.id.test_lv);
updateDataBtn = (Button) findViewById(R.id.update_data_btn);
updateDataBtn.setOnClickListener(this);
}
/**
* 初始化數(shù)據(jù)
*/
private void initData() {
//初始化10項(xiàng)數(shù)據(jù)
for (int i = 1; i <= 20; i++) {
dataList.add("顯示內(nèi)容" + i);
}
//設(shè)置ListView的適配器
listViewDemoAdapter = new ListViewDemoAdapter(this, dataList);
testLv.setAdapter(listViewDemoAdapter);
testLv.setSelection(4);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.update_data_btn://動(dòng)態(tài)加載列表數(shù)據(jù)
dataList.add("動(dòng)態(tài)加載的數(shù)據(jù)項(xiàng)");
//通知ListView更改數(shù)據(jù)源
if (listViewDemoAdapter != null) {
listViewDemoAdapter.notifyDataSetChanged();
testLv.setSelection(dataList.size() - 1);//設(shè)置顯示列表的最后一項(xiàng)
} else {
listViewDemoAdapter = new ListViewDemoAdapter(this, dataList);
testLv.setAdapter(listViewDemoAdapter);
testLv.setSelection(dataList.size() - 1);
}
break;
}
}
}
界面布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/update_data_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:text="動(dòng)態(tài)加載數(shù)據(jù)"/>
<ListView
android:id="@+id/test_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/update_data_btn"
android:divider="@color/colorPrimaryDark"
android:dividerHeight="3dp"
android:listSelector="#ff0000"
android:scrollbars="none"/>
</RelativeLayout>
適配器代碼:
package net.oschina.git.zhaikun.androiddeveloped.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import net.oschina.git.zhaikun.androiddeveloped.R;
import java.util.List;
/**
* Created by zhaikun68 on 2018/3/5.
* <p>
* ListView演示Demo中的數(shù)據(jù)適配器
*/
public class ListViewDemoAdapter extends BaseAdapter {
private Context context;//上下文對(duì)象
private List<String> dataList;//ListView顯示的數(shù)據(jù)
/**
* 構(gòu)造器
*
* @param context 上下文對(duì)象
* @param dataList 數(shù)據(jù)
*/
public ListViewDemoAdapter(Context context, List<String> dataList) {
this.context = context;
this.dataList = dataList;
}
@Override
public int getCount() {
return dataList == null ? 0 : dataList.size();
}
@Override
public Object getItem(int position) {
return dataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
//判斷是否有緩存
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_demo, null);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
//得到緩存的布局
viewHolder = (ViewHolder) convertView.getTag();
}
//設(shè)置圖片
viewHolder.pictureImg.setImageResource(R.mipmap.ic_launcher);
//設(shè)置內(nèi)容
viewHolder.contentTv.setText(dataList.get(position));
return convertView;
}
/**
* ViewHolder類
*/
private final class ViewHolder {
ImageView pictureImg;//圖片
TextView contentTv;//內(nèi)容
/**
* 構(gòu)造器
*
* @param view 視圖組件(ListView的子項(xiàng)視圖)
*/
ViewHolder(View view) {
pictureImg = (ImageView) view.findViewById(R.id.picture_img);
contentTv = (TextView) view.findViewById(R.id.content_tv);
}
}
}
列表子項(xiàng)的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:id="@+id/picture_img"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="5dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/content_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="顯示內(nèi)容"/>
</LinearLayout>
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- Android使用RecyclerView實(shí)現(xiàn)列表數(shù)據(jù)選擇操作
- kotlin android extensions 插件實(shí)現(xiàn)示例詳解
- 一文讀懂Android?Kotlin的數(shù)據(jù)流
- Android使用ViewBinding的詳細(xì)步驟(Kotlin簡(jiǎn)易版)
- Android入門之使用RecyclerView完美實(shí)現(xiàn)瀑布流界面詳解
- Android studio listview實(shí)現(xiàn)列表數(shù)據(jù)顯示 數(shù)據(jù)循環(huán)顯示效果
- Android kotlin RecyclerView遍歷json實(shí)現(xiàn)列表數(shù)據(jù)的案例
相關(guān)文章
Android 使用手機(jī)NFC的讀取NFC標(biāo)簽數(shù)據(jù)的方法
這篇文章主要介紹了Android 使用手機(jī)NFC的讀取NFC標(biāo)簽數(shù)據(jù)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Android實(shí)現(xiàn)設(shè)置APP灰白模式效果
大家好,本篇文章主要講的是Android實(shí)現(xiàn)設(shè)置APP灰白模式效果,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
Android開發(fā)教程之獲取系統(tǒng)輸入法高度的正確姿勢(shì)
這篇文章主要給大家介紹了關(guān)于Android開發(fā)教程之獲取系統(tǒng)輸入法高度的正確姿勢(shì),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Android具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10
Android中的webview監(jiān)聽(tīng)每次URL變化實(shí)例
這篇文章主要介紹了Android中的webview監(jiān)聽(tīng)每次URL變化實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
如何自己實(shí)現(xiàn)Android View Touch事件分發(fā)流程
這篇文章主要介紹了如何自己實(shí)現(xiàn)Android View Touch事件分發(fā)流程,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03
Android 屬性動(dòng)畫原理與DataBinding
這篇文章主要介紹了Android 屬性動(dòng)畫原理與DataBinding的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android單一實(shí)例全局可調(diào)用網(wǎng)絡(luò)加載彈窗
這篇文章主要為大家詳細(xì)介紹了Android單一實(shí)例全局可調(diào)用網(wǎng)絡(luò)加載彈窗,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android 實(shí)現(xiàn)云知聲版離線語(yǔ)音合成
這篇文章主要介紹了Android 實(shí)現(xiàn)云知聲版離線語(yǔ)音合成,目前云知聲提供免費(fèi)的離線TTS,功能也比較簡(jiǎn)單,合成的語(yǔ)音也比較生硬,如果對(duì)合成的語(yǔ)音要求不高的話可以考慮接入。具體合成需要的小伙伴可以參考下面文章內(nèi)容2022-06-06

