androidlistview資料庫
㈠ android資料庫更新後怎樣刷新listview數據
0、本質上是listview綁定Adapter,Adapter關聯List,因此List變化後導致Adapter同步變化;再通過調用adapter.notifyDataSetChanged();方法使得listview界面自動更新。
操作:
1、List<String> strName = new ArrayList<String>();//實際的數據源
2、Adapter adapter = new ArrayAdapter<String>(context,
R.layout.xmlforitem, strName);//適配器,其中 R.layout.xmlforitem是列表中每一項的布局,可以用默認的也可自建,strName則是將數據源綁定到適配器
3、listView.setAdapter(adapter);//將適配器綁定到列表顯示控制項ListView;
4、strName.add("TEST ONE");//增加一項,引起list變化,Adapter也隨之變化;
5、adapter.notifyDataSetChanged();//調用notifyDataSetChanged();更新適配器,ListView會自動刷新,notifyDataSetChanged()方法可能需要在UI線程中調用,建議自行測試;
6、數據增加可能引起內存變化,需防內存溢出;
7、其他。
以上僅供參考,建議多搜索相關文檔。
㈡ android如何將從資料庫讀取的數據顯示在listview中
final SimpleAdapter adapter = new SimpleAdapter(this, getData(),
R.layout.mainlayout, new String[] { "text", "time", "image" },
new int[] { R.id.PL_TextView01, R.id.PL_TextView02,
R.id.PL_ImageView01 });
lv.setAdapter(adapter);
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
Cursor cur = DR.query("DB", new String[] { "text", "time" }, null,
null, null, null, "time desc");
while (cur.moveToNext()) {
for (int i = 0; i < cur.getCount(); i++) {
cur.moveToPosition(i);
String text = cur.getString(0);
String time = cur.getString(1);
map = new HashMap<String, Object>();
map.put("text", text);
map.put("time", time);
map.put("image", R.drawable.ic_menu_close_clear_cancel);
list.add(map);
}
}
return list;
}
我是使用SimpleAdapter 作為listview的適配器 通過數據的query方法 將數據放入listview
㈢ android,怎麼得到listview里的項目在資料庫里的_id
ListView 通常都是用自定義的Adapter 然後將對象與Adapter進行綁定
在Adapter的getView方法中處理視圖的顯示內容操作
資料庫的_id應該是保存到Adapter綁定的對象中
㈣ android中怎麼將資料庫中值顯示在listview中
假設從資料庫獲取到的數據為list
自定義Adapter繼承自BaseAdapter,重寫getView方法,實例化列表項並且綁定數據就可以了
/**新建一個類繼承BaseAdapter,實現視圖與數據的綁定*/
{
;//得到一個LayoutInfalter對象用來導入布局
privateListlist;
/**構造函數*/
publicMyAdapter(Contextcontext,Listlist){
this.mInflater=LayoutInflater.from(context);
this.list=list;
}
@Override
publicintgetCount(){
returnlist.size();//返回數組的長度
}
@Override
publicObjectgetItem(intposition){
returnlist.get(position);
}
@Override
publiclonggetItemId(intposition){
return0;
}
@Override
publicViewgetView(finalintposition,ViewconvertView,ViewGroupparent){
ViewHolderholder;
if(convertView==null){
convertView=mInflater.inflate(R.layout.item,null);
holder=newViewHolder();
/**得到各個控制項的對象*/
holder.title=(TextView)convertView.findViewById(R.id.ItemTitle);
holder.text=(TextView)convertView.findViewById(R.id.ItemText);
holder.bt=(Button)convertView.findViewById(R.id.ItemButton);
convertView.setTag(holder);//綁定ViewHolder對象
}
else{
holder=(ViewHolder)convertView.getTag();//取出ViewHolder對象
}
holder.title.setText(list.get(position).getId().toString());
holder.text.setText(list.get(position).getName().toString());
returnconvertView;
}
/**存放控制項*/
classViewHolder{
publicTextViewtitle;
publicTextViewtext;
}
}
㈤ android中listview控制項顯示資料庫的東西
CursorAdapter
繼承於BaseAdapter是個虛類,它為cursor和ListView提供了連接的橋梁。
public abstract class
CursorAdapter
extends BaseAdapter
直接子類只有ResourceCursorAdapter
Class Overview
Adapter that exposes data from a Cursor to a ListView widget.
The Cursor must include a column named "_id" or this class will not work.
注意cursor的必須要有個命名為"_id"的列。比如Contacts._ID就為"_id"
必須實現以下函數:
abstract View newView(Context context, Cursor cursor, ViewGroup parent)
Makes a new view to hold the data pointed to by cursor.
abstract void bindView(View view, Context context, Cursor cursor)
Bind an existing view to the data pointed to by cursor
注意:
newView該函數第一次回調用後,如果數據增加後也會再調用,但是重繪是不會調用的。
數據增加後,回調用該函數來生成與新增數據相對應的view。
bindView函數第一次回調用後,如果數據更新也會再調用,但重繪會再次調用的。
【總的來說應該是在調用bindView如果發現view為空會先調用newView來生成view】
i
[java]
view plain
<span style="font-size:16px;">mport java.util.List;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CursorAdapter;
import android.widget.TextView;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.RawContacts;
import android.view.View.OnClickListener;
import android.widget.Button;
public class HelloCursor extends ListActivity {
private static String[] PROJECTION = new String[] { Contacts._ID,
Contacts.DISPLAY_NAME };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Cursor c = getContentResolver().query(Contacts.CONTENT_URI, PROJECTION,
null, null, Contacts.DISPLAY_NAME + " COLLATE NOCASE");
startManagingCursor(c);
MyCursorAdapter adapter = new MyCursorAdapter(this, R.layout.list_row,
c);
this.setListAdapter(adapter);
Button button = (Button)findViewById(R.id.Button01);
OnClickListener listener=new OnClickListener(){
@Override
public void onClick(View v) {
doAction();
}
};
button.setOnClickListener(listener);
mHandler = new Handler();
}
private String[] mStrings = { "hubin", "hudashi", "robin" };
int cnt = 0;
private Handler mHandler;
class AddContactThread implements Runnable {
public void run() {
int nStringLength = mStrings.length;
int randomNumber = 0;
ContentValues newValues = new ContentValues();
String tempString = null;
randomNumber = (int) (Math.random() % 10);
for (int i = 0; i < nStringLength; i++) {
tempString = mStrings + cnt + randomNumber;
newValues.put(Contacts.DISPLAY_NAME, tempString);
getContentResolver().insert(RawContacts.CONTENT_URI, newValues);
newValues.clear();
}
cnt++;
}
}
AddContactThread addContact=new AddContactThread();
void doAction()
{
mHandler.post(addContact);
}
}
class MyCursorAdapter extends CursorAdapter {
Context context=null;
int viewResId;
public MyCursorAdapter(Context context, int resource, Cursor cursor) {
super(context,cursor);
viewResId=resource;
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
TextView view =null;
LayoutInflater vi = null;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view =(TextView)vi.inflate(viewResId, parent, false);
//v =(TextView)vi.inflate(textViewResourceId,null);
Log.i("hubin","newView"+view);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
Log.i("hubin","bind"+view);
TextView nameView = (TextView) view;
// Set the name
nameView.setText(cursor
.getString(cursor.getColumnIndex("DISPLAY_NAME")));
}
}
㈥ android 怎麼把存入資料庫的信息顯示在ListView中
你可以從資料庫中獲取數據,通過查找你所需的數據返回的是一個cursor,然後把cursor轉化成一個list<map<>>或者 list<對象> ,然後通過適配器,把這些數據設置在listview上面。
㈦ android控制項listview怎樣顯示資料庫數據呢
使用SimpleAdapter 作為listview的適配器 通過數據的方法 將數據放入listview:
finalSimpleAdapteradapter=newSimpleAdapter(this,getData(),
R.layout.mainlayout,newString[]{"text","time","image"},
newint[]{R.id.PL_TextView01,R.id.PL_TextView02,
R.id.PL_ImageView01});
lv.setAdapter(adapter);
privateList<Map<String,Object>>getData(){
List<Map<String,Object>>list=newArrayList<Map<String,Object>>();
Map<String,Object>map=newHashMap<String,Object>();
Cursorcur=DR.query("DB",newString[]{"text","time"},null,
null,null,null,"timedesc");
while(cur.moveToNext()){
for(inti=0;i<cur.getCount();i++){
cur.moveToPosition(i);
Stringtext=cur.getString(0);
Stringtime=cur.getString(1);
map=newHashMap<String,Object>();
map.put("text",text);
map.put("time",time);
map.put("image",R.drawable.ic_menu_close_clear_cancel);
list.add(map);
}
}
returnlist;
}