android數據顯示
㈠ 安卓開發 如何拿到數據列表 把size顯示到界面上
Android中載入list列表數據主要是通過Adapter實現,可用顯示列表的控制項如下: Listview GridView ExpandListview 顯示具體的數據需要通過Adapter實現,Android目前有4種Adapter: ArrayAdapter SimpleAdapter SimpleCursorAdapter BaseAdapter ( 自定義Adapter) 具體操作步驟 ( 以自定義Adapter為例): 在xml中定義Listview布局 在代碼中通過ID找到Listview控制項 構建Adapter對象,新建一個類繼承自BaseAdapter,重寫它的四個方法,具體如下代碼 構造好適配器後設置Listview的adapter對象為新建的適配器,界面即可顯示數據 在數據變動的地方,只需要調用adapter的notifyDataSetChanged方法即可刷新界面 package com.beryl.gougou; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import java.util.List; /** * Created by yt on 16/11/14. */ public class MyAdapter extends BaseAdapter { private List<String> datalist; private LayoutInflater inflater; public MyAdapter(Context context ,List<String> datalist){ this.datalist = datalist; inflater = LayoutInflater.from(context); } @Override public int getCount() { return 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) { //此處參考網上的view緩存機制,示例demo不多說明 return null; } }
㈡ android listView的數據如何清空,在添加進去顯示啊!
滿意答案熱心問友2011-06-08android.widget.SimpleAdapter.SimpleAdapter(Context context, List<? extends Map<String, ? data, int resource, String[] from, int[] to),你把SimpleAdapter中存數據的(List<? extends Map<String, ?data這個參數) data.clear()掉,這樣應該能把之前ListView中的數據清空吧追問: 將數據源的獲取進行獨立,在數據源變動前進行data.clear(); 然後在數據源變動後,採用Adapter.notifyViewChanged();進行通知 程序,數據源已經變動,simpleAdapter須要更新。系統將會自動處理,並實現更新數據。緣丶之尐黙的感言:
㈢ android中怎麼將SQLite中的數據顯示在Listview中(用Cursor)
public List<String> findAllDate(){//我的一個方法
List<String> list = new ArrayList<String>();
SQLiteDatabase database = dbopenhelper.getWritableDatabase();
Cursor cursor = database.rawQuery("select date from health", new String[]{});//這邊寫上你的查詢語句
while(cursor.moveToNext()){
list.add(cursor.getString(0));
}
cursor.close();
database.close();
return list;
}
之後就是把這個LIST顯示在Listview中;
如:SimpleAdapter adapter=new SimpleAdapter(Listview.this,list
,R.layout.viewdate,new String[]{"date"},
new int[]{R.id.tdate});
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { //事件
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
....
}
});
希望對你有用
㈣ android 怎listview一行顯示3個數據
一般的SimpleAdapter就可以啊
private ListView lv1;//布局中的ListView
lv1 = (ListView) findViewById(R.id.sim_list);
List<Map<String,String>> mLists = new ArrayList<Map<String,String>>();
//為列表添加數據(為了方便數據都做成一樣的了)
for(int i=0;i<21;i++) {
Map<String, String> map = new HashMap<String, String>();
map.put("title", "小宗");
map.put("info", "電台DJ");
map.put("age", "21");
mLists.add(map);
}
/**
* SimpleAdapter信息配置
* R.layout.simple_item對應一個ListView顯示的內容布局
* String對應map中放置的鍵值對名稱
* int對應內容布局的控制項id
*/
SimpleAdapter adapter = new SimpleAdapter(Context, mLists,
R.layout.simple_item, new String[] { "title", "info", "age"},
new int[] {R.id.title, R.id.info, R.id.age});
//為ListView添加對應的Adapter
lv1.setAdapter(adapter);
xml布局文件
<?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="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp" />
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp" />
<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp" />
</LinearLayout>
想加多少,添加就行了
復雜點的就用BaseAdapter,不過單純想加數據的話,用SimpleAdapter簡單