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 Kotlin環(huán)境使用ButterKnife的方法
本篇文章主要介紹了Android Kotlin環(huán)境使用ButterKnife的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Android開(kāi)發(fā)實(shí)現(xiàn)繪制淘寶收益圖折線效果示例
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)繪制淘寶收益圖折線效果,涉及Android canvas圖形繪制及布局控制相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
Android實(shí)現(xiàn)png轉(zhuǎn)jpg圖片的方法
在 Android 應(yīng)用開(kāi)發(fā)中,圖像處理是非常常見(jiàn)的需求,本文介紹了如何在 Android平臺(tái)上實(shí)現(xiàn)一個(gè) PNG 轉(zhuǎn) JPG 的模塊,用戶可以從相冊(cè)或文件中選取 PNG 圖片,一鍵將其轉(zhuǎn)換為 JPG 并保存到本地,需要的朋友可以參考下2025-04-04
Android 中SQLite技術(shù)實(shí)例詳解
這篇文章主要介紹了Android 中SQLite技術(shù)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android中自定義ContentProvider實(shí)例
應(yīng)用A(TestBaidu)調(diào)用另外一個(gè)應(yīng)用(TestContentProvider)即自定義ContentProvider的使用,其它應(yīng)用調(diào)用該ContentProvider,具體如下,感興趣的朋友可以參考下哈2013-06-06
使用Android自定義控件實(shí)現(xiàn)滑動(dòng)解鎖九宮格
最近由于Android項(xiàng)目需要,要求做一個(gè)類似于支付寶的九宮格解鎖組件,下面小編給大家分享了具體實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-10-10
Android實(shí)現(xiàn)底部對(duì)話框BottomDialog彈出實(shí)例代碼
本篇文章主要介紹了Android實(shí)現(xiàn)底部對(duì)話框BottomDialog代碼。這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2017-03-03
Android實(shí)現(xiàn)淘寶購(gòu)物車
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)淘寶購(gòu)物車,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05

