當前位置:首頁 » 安卓系統 » androidlistview原理

androidlistview原理

發布時間: 2022-06-19 10:42:34

Ⅰ android 怎麼樣在listview的adapter.notifyDataSetChanged 結束後執行一些操作

恩,原理是這樣的,notifyDataSetChanged()其實是非同步的,因為設計的時候如果同步的話如果數據量大或者有耗時操作主線程會出現ANR。可惜的是notifyDataSetChanged並沒有監聽,所以你不知道是否執行完了。我的解決方式是使用postdelay來延遲主線程的執行,達到假同步的效果。

Ⅱ android中如何實現文件的讀取並用listview或者gridview顯示出來!

根據文件的格式決定,和java一樣其實 ,只是拿到數據以後適配到屏幕才牽扯到android 的知識,這里只需要用適配器就可以了

Ⅲ 怎麼給listview item 設置監聽事件

給Listview裡面Item里的每一個控制項怎麼設定點擊事件

adpter用的是繼承的baseadapter,給整個listview
上的
Item
設置點擊事件就用listview.setOnItemClickListener(listener)就可以,
下面介紹
如果

I
tem裡面某一
控制項設置點擊事件。方法是重寫
Adapter。

ListView工作原理(針對下面代碼):

1.
給ListView設置數據適配器,此處程序是自己重寫的Adapter,創建Adapter 的時候主要做下面的工作:

(1)把ListView需要顯示的數據傳給Adapter

(2)把ListView顯示Item的界面傳給Adapter

(3)把上下文對象傳給Adapter,主要用來得到LayoutInflater對象來得到Item界面

2. 給ListView設置當點擊Item對象的時候執行的操作,此處要實現給Item中的各項也就是該程序的三個TextView設置監聽器,也就是調用Adapter中的getView函數。

3. getView函數實現的操作是:第一要將Item要顯示的數據顯示,然後設置監聽器,為監聽器設置操作。

代碼如下:

ListView中Item的布局文件

activity_my_goods_listview.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<TextView

android:id = "@+id/goodBarcode"

android:layout_width="150dip"

android:layout_height="wrap_content"

android:textColor="#000"

android:textSize="16sp"

android:paddingTop="10dip"

android:clickable="true"//設置可點擊

/>

<TextView

android:id="@+id/goodName"

android:layout_width="150dip"

android:layout_height="wrap_content"

android:textColor="#000"

android:textSize="16sp"

android:paddingTop="10dip"

android:clickable="true"

/>

<TextView

android:id="@+id/goodProvider"

android:layout_width="150dip"

android:layout_height="wrap_content"

android:textColor="#000"

android:textSize="16sp"

android:paddingTop="10dip"

android:clickable="true"

/>

</LinearLayout>

ListView控制項所在的布局文件

activity_my_goods.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#ffffffff"

android:gravity="center"

android:orientation="vertical">

<LinearLayout //標題

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:paddingLeft="15px"

android:layout_marginLeft="5dip"

android:layout_marginTop="93dip">

<TextView

android:layout_width="150dip"

android:layout_height="wrap_content"

android:textColor="#000"

android:textSize="16sp"

android:text="商品條碼"

android:paddingTop="10dip"

/>

<TextView

android:layout_width="150dip"

android:layout_height="wrap_content"

android:textColor="#000"

android:textSize="16sp"

android:text="商品名稱"

android:paddingTop="10dip"

/>

<TextView

android:layout_width="150dip"

android:layout_height="wrap_content"

android:textColor="#000"

android:textSize="16sp"

android:text="供應商"

android:paddingTop="10dip"

/>

</LinearLayout>

<ScrollView

android:id="@+id/feedbacklayout01"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_marginTop="103dip"

android:paddingTop="20.0dip" >

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

android:paddingLeft="20px">

<ListView

android:id="@+id/goodsinfoListView"

android:layout_width="wrap_content"

android:layout_height="354dp"

android:layout_marginBottom="5.0dip"

android:layout_marginRight="5.0dip"

android:textColor="#000"

android:textSize="16.0dip">

</ListView>

</ScrollView>

</RelativeLayout>

Ⅳ Android 如何按時間先後顯示ListView數據

解決方法:

  1. lisrview綁定的是哪種adapter?我假設是最簡單的arrayAdapter,其實原理都一樣。

  2. 綁定adapter的數據大多數是數組。

  3. 數組的排序總會把?我記得是sort。

  4. 排序好再將數據綁定到adapter,然後adapter綁定到listview

Ⅳ android中listview的數據的同步與非同步載入有什麼區別,效果有什麼不同

目前沒有同步載入數據這種做法,如果網路延遲主界面UI就卡死了,
之後用戶不耐煩就只能強行關閉了,卡死的時候按鍵都沒反應的。
一個簡單的的多線程
class updatelocationTask extends AsyncTask<String, Integer, Response> {
protected void onPreExecute() {
//這里寫執行doInBackground方法之前要做的什麼,比如說彈出ProgressDialog
}
}

@Override

protected Response doInBackground(String... params) {
//這里就是線程裡面的方法了,比如說建立連接,請求數據
}
}
protected void onPostExecute(Response result) {
//這里可以根據返回值來確定怎麼操作,比如說刷新列表或者提示用戶網路不暢,是否再次刷新
}
}
}
}

Ⅵ 解決一個android中的listview和cursor的問題

Android 連接資料庫
Android採用關系型資料庫sqlite3,它是一個支持SQL輕量級的嵌入式資料庫,在嵌入式操作上有很廣泛的,WM採用的也是SQLite3

關於過於、原理方面的東西在這篇文章里不會提到,但是如果你想能夠快速的學會操作SQLite3,那這就是你要找的文章!

首先,我們看一下api,所有資料庫相關的介面、類都在.database和android.database.sqlite兩個包下,雖然只有兩個包,但是如果你英文不好或是太懶的話也要迷茫一段時間,其實,我們真正用的到的沒有幾個!

1、SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)

這是一個抽象類,關於抽象類我們都知道,如果要使用它,一定是繼承它!

這個類的方法很少,有一個構造方法

SQLiteOpenHelper(android.content.Context context, java.lang.String name,android.database.sqlite.SQLiteDatabase.CursorFactory factory, int version);

參數不做過多的解釋,CursorFactory一般直接傳null就可以

public void onCreate(SQLiteDatabase db)

此方法在創建資料庫是被調用,所以,應該把創建表的操作放到這個方法裡面,一會兒在後面我們會再詳細的說如何創建表

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)

從方法名上我們就能知道這個方法是執行更新的,沒錯,當version改變是系統會調用這個方法,所以在這個方法里應該執行刪除現有表,然後手動調用onCreate的操作

SQLiteDatabase getReadableDatabase()

可讀的SQLiteDatabase對象

SQLiteDatabase getWritableDatabase()

獲取可寫的SQLiteDatabase對象

2、SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

關於操作資料庫的工作(增、刪、查、改)都在這個類里

execSQL(sql)

執行SQL語句,用這個方法+SQL語句可以非常方便的執行增、刪、查、改

除此之外,Android還提供了功過方法實現增、刪、查、改

long insert(TABLE_NAME, null, contentValues)添加記錄

int delete(TABLE_NAME, where, whereValue)刪除記錄

int update(TABLE_NAME, contentValues, where, whereValue) 更新記錄

Cursor query(TABLE_NAME, null, null, null, null, null, null) 查詢記錄

除此之外,還有很多方法,如:beginTransaction()開始事務、endTransaction()結束事務...有興趣的可以自己看api,這里就不多贅述了

3、Cursor(android.database.Cursor)

游標(介面),這個很熟悉了吧,Cursor里的方法非常多,常用的有:

boolean moveToPosition(position)將指針移動到某記錄

getColumnIndex(Contacts.People.NAME)按列名獲取id

int getCount()獲取記錄總數

boolean requery()重新查詢

boolean isAfterLast()指針是否在末尾

boolean isBeforeFirst()時候是開始位置

boolean isFirst()是否是第一條記錄

boolean isLast()是否是最後一條記錄

boolean moveToFirst()、 boolean moveToLast()、 boolean moveToNext()同moveToPosition(position)

4、SimpleCursorAdapter(android.widget.SimpleCursorAdapter)

也許你會奇怪了,之前我還說過關於資料庫的操作都在database和database.sqlite包下,為什麼把一個Adapter放到這里,如果你用過Android的SQLite3,你一定會知道

,這是因為我們對資料庫的操作會經常跟列表聯系起來

經常有朋友會在這出錯,但其實也很簡單

SimpleCursorAdapter adapter = new SimpleCursorAdapter(

this,

R.layout.list,

myCursor,

new String[] ,

new int[]);

my.setAdapter(adapter);

一共5個參數,具體如下:

參數1:Content

參數2:布局

參數3:Cursor游標對象

參數4:顯示的欄位,傳入String[]

參數5:顯示欄位使用的組件,傳入int[],該數組中是TextView組件的id

到這里,關於資料庫的操作就結束了,但是到目前為止我只做了翻譯的工作,有些同學可能還是沒有掌握,放心,下面我們一起順著正常開發的思路理清一下頭緒!

前面的只是幫沒做過的朋友做下普及,下面才是你真正需要的!

一、寫一個類繼承SQLiteOpenHelpe

public class DatabaseHelper extends SQLiteOpenHelper

構造方法:

DatabaseHelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

在onCreate方法里寫建表的操作

public void onCreate(SQLiteDatabase db) {

String sql = "CREATE TABLE tb_test (_id INTEGER DEFAULT '1' NOT NULL PRIMARY KEY AUTOINCREMENT,class_jb TEXT NOT NULL,class_ysbj TEXT NOT NULL,title TEXT NOT NULL,content_ysbj TEXT NOT NULL)";

db.execSQL(sql);//需要異常捕獲

}

在onUpgrade方法里刪除現有表,然後手動調用onCtreate創建表

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

String sql = "drop table "+tbname;

db.execSQL(sql);

onCreate(db);

}

對表增、刪、查、改的方法,這里用的是SQLiteOpenHelper提供的方法,也可以用sql語句實現,都是一樣的

關於獲取可讀/可寫SQLiteDatabase,我不說大家也應該會想到,只有查找才會用到可讀的SQLiteDatabase

/**

* 添加數據

*/

public long insert(String tname, int tage, String ttel){

SQLiteDatabase db= getWritableDatabase();//獲取可寫SQLiteDatabase對象

//ContentValues類似map,存入的是鍵值對

ContentValues contentValues = new ContentValues();

contentValues.put("tname", tname);

contentValues.put("tage", tage);

contentValues.put("ttel", ttel);

return db.insert(tbname, null, contentValues);

}

/**

* 刪除記錄

* @param _id

*/

public void delete(String _id){

SQLiteDatabase db= getWritableDatabase();

db.delete(tbname,

"_id=?",

new String[]);

}

/**

* 更新記錄的,跟插入的很像

*/

public void update(String _id,String tname, int tage, String ttel){

SQLiteDatabase db= getWritableDatabase();

ContentValues contentValues = new ContentValues();

contentValues.put("tname", tname);

contentValues.put("tage", tage);

contentValues.put("ttel", ttel);

db.update(tbname, contentValues,

"_id=?",

new String[]);

}

/**

* 查詢所有數據

* @return Cursor

*/

public Cursor select(){

SQLiteDatabase db = getReadableDatabase();

return db.query(

tbname,

new String[],

null,

null, null, null, "_id desc");

}

關於db.query方法的參數,有很多,為了防止大家弄亂,我簡單說一下

參數1:表名

參數2:返回數據包含的列信息,String數組里放的都是列名

參數3:相當於sql里的where,sql里where後寫的內容放到這就行了,例如:tage>?

參數4:如果你在參數3里寫了?(知道我為什麼寫tage>?了吧),那個這里就是代替?的值 接上例:new String[]

參數5:分組,不解釋了,不想分組就傳null

參數6:having,想不起來的看看SQL

參數7:orderBy排序

到這里,你已經完成了最多的第一步!我們來看看都用到了那些類:

SQLiteOpenHelper我們繼承使用的

SQLiteDatabase增刪查改都離不開它,即使你直接用sql語句,也要用到execSQL(sql)

二、這里無非是對DatabaseHelper類定義方法的調用,沒什麼可說的,不過我還是對查詢再嘮叨幾句吧

Android查詢出來的結果一Cursor形式返回

cursor = sqLiteHelper.select();//是不是很簡單?

查詢出來的cursor一般會顯示在listView中,這就要用到剛才提到的SimpleCursorAdapter

SimpleCursorAdapter adapter = new SimpleCursorAdapter(

this,

R.layout.list_row,

cursor,

new String[],

new int[]

);

裡面帶有實例。自己好好學習吧!

Ⅶ 如何實現android 三個ListView實現類似省市區的聯動效果

如何在Android設備旋轉時暫存數據以保護當前的交互狀態 總結了以下幾種情況:
設備旋轉時保存Activity的交互狀態: onSaveInstanceState();
設備旋轉時保存Fragment的交互狀態: setRetainInstance(true);
設備旋轉時保存WebView的數據: android:configChanges="keyboardHidden|orientation|screenSize";
設備旋轉時保存在自定義View中繪制的圖形。
如果Fragment不包含WebView,只是簡單的UI View,像是TextView, Button, CheckBox, ImageView... 不建議使用setRetainInstance方法,只需要記住當前fragment的index,然後在設備旋轉後根據數據重新實例化一個fragment。因為不包含大量數據,旋轉的過程中幾乎可以用「無縫切換」來形容。
如果包含WebView還是採用針對WebView的方法(題主的情況)。所以要看情況而定。

Ⅷ android中listview的下拉刷新上拉載入是怎麼實現的

這是兩個分開的部分。如果你是新手,先一個一個來。

我只能跟你說一下思路,具體的東西你在網上查查,不行再問我,新手的話慢慢來。

  1. 下拉刷新,獲取listview的下拉時間顯示header,然後調用更新數據的介面就可以了。

  2. 上啦載入,是分頁獲取數據,獲取listview的是否拉到最底,如果拉倒最底,獲取數據,讓後list的數據添加獲取的數據,更新adapter就可以了。


熱點內容
sqlifnotexists 發布:2025-08-02 02:02:14 瀏覽:127
如何製作伺服器的懸空標題字 發布:2025-08-02 01:57:49 瀏覽:843
唱吧上傳撤銷 發布:2025-08-02 01:48:11 瀏覽:693
區域網伺服器不能用ip訪問 發布:2025-08-02 01:47:20 瀏覽:540
c語言日誌 發布:2025-08-02 01:39:14 瀏覽:489
詳細編程 發布:2025-08-02 01:17:13 瀏覽:349
怎麼查看wifi的密碼 發布:2025-08-02 00:46:24 瀏覽:928
linux工具開發 發布:2025-08-02 00:44:52 瀏覽:688
c語言編程我愛你 發布:2025-08-02 00:40:12 瀏覽:946
車銑復合加工編程 發布:2025-08-02 00:39:21 瀏覽:49