0
我已在安卓下面的程序使用ListActivity
:如何插入表中的數據,而無需刷新它
package com.infy.dataloaderdemo;
import java.util.ArrayList;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Browser;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class DataLoaderOfDataBase extends ListActivity {
/** Called when the activity is first created. */
ListView listView;
ArrayList<String> arrayList=new ArrayList<String>();
Cursor mCursor;
Button mButton;
EditText e;
public static final String KEY_ROWID = "_id";
public static final String KEY_ISBN = "isbn";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dataloaderlist);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
mButton=(Button)findViewById(R.id.push);
mButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pushDataInList();
}
});
}
public void pushDataInList(){
String[] displayFields = new String[] {KEY_ROWID,KEY_ISBN};
int[] displayViews = new int[] { android.R.id.text1,android.R.id.text2 };
// Cursor cur = managedQuery(android.provider.Browser.BOOKMARKS_URI,projection, null, null, null);
DBAdapter dbAdapter=new DBAdapter(this);
long l=dbAdapter.insertTitle("new");
Log.e("l", ""+l);
Cursor cur=null;
//cur=dbAdapter.getTitle(2);
cur = dbAdapter.getAllTitles();
int i=cur.getCount();
Log.e("c", ""+i);
setListAdapter(new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cur,displayFields,displayViews));
}
}
每當我添加一個數據,整個名單得到更新。有沒有什麼方法可以在List中添加數據而不刷新它?
我應該在代碼中使用setNotifyOnChange?我們必須在一些對象上使用它,這裏我們沒有創建任何! – tom 2011-04-21 11:18:37
'setNotifyOnChange'與'ArrayAdapter'一起使用......似乎沒有'SimpleCursorAdapter'的等價物。也許你可以分享你添加數據的部分。 – rajath 2011-04-21 11:35:56