MainActivity.java
public class MainActivity extends ListActivity implements OnGesturePerformedListener {
static SQLiteDatabase db;
public static String TABEl_CREATE="CONTACTS";
public static String DATA_BASE_TABLE_CREATE = "create table if not exists " +TABEl_CREATE+ "(Name text not null ,Number number unique not null)";
GestureLibrary lib;
private ArrayList<String> results = new ArrayList<String>();
ArrayAdapter<String> adapter;
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = openOrCreateDatabase("contacts", MODE_PRIVATE, null);
db.execSQL(DATA_BASE_TABLE_CREATE);
lv = (ListView) findViewById (android.R.id.list);
populatelv();
lib = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!lib.load()) {
finish();
}
GestureOverlayView gesture = (GestureOverlayView)
findViewById(R.id.gestures);
gesture.addOnGesturePerformedListener(this);
}
private void populatelv() {
// TODO Auto-generated method stub
Cursor c = db.rawQuery("SELECT Name FROM " +TABEl_CREATE, null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String Name = c.getString(c.getColumnIndex("Name"));
results.add( Name );
}while (c.moveToNext());
}
}
adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1,android.R.id.text1,results);
lv.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
// TODO Auto-generated method stub
ArrayList<Prediction> predictions = lib.recognize(gesture);
if (predictions.size() > 0)
{
String action = predictions.get(0).name;
if (predictions.get(0).score > 1.0)
{
if ("ADD".equals(action))
{
Toast.makeText(this, "Adding a contact",
Toast.LENGTH_SHORT).show();
Intent i = new Intent(MainActivity.this,Database.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
else
if ("delete".equals(action))
{
Toast.makeText(this, "Removing a contact",Toast.LENGTH_SHORT).show();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
// Here,how can i get the position of the specific listitem where gesture action took place..?
// String str = lv.getItemAtPosition(arg2).toString();
//Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
// String delete = "delete from contacts where Name = '"+str+"'";
//db.execSQL(delete);
//Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
}
}) ;
}
else if ("refresh".equals(action))
{
Toast.makeText(this, "Reloading contacts",
Toast.LENGTH_SHORT).show();
}
}
}
}
}
Database.java
public class Database extends Activity {
Button add;
EditText name,number;
SQLiteDatabase db;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.database);
db= MainActivity.db;
db = openOrCreateDatabase("contacts", MODE_PRIVATE, null);
add = (Button) findViewById(R.id.button1);
name = (EditText) findViewById(R.id.editText1);
number = (EditText) findViewById(R.id.editText2);
add.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String nval,nbval;
nval = name.getText().toString();
nbval = number.getText().toString();
if(nval.length()>0 && nbval.length()>0)
{
ContentValues cv = new ContentValues();
cv.put("Name", nval);
cv.put("Number", nbval);
db.insert("CONTACTS","title", cv);
Toast.makeText(getApplicationContext(), "Inserted sucessfully",Toast.LENGTH_SHORT ).show();
}
else
{
Toast.makeText(getApplicationContext(), "Please fill all fields!!!", Toast.LENGTH_SHORT).show();
}
}
});
}
public void onBackPressed()
{
Intent intent=new Intent(Database.this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
};
}
在这里,我试图通过从Database.java中完成的数据库检索来显示listview中的联系人。我可以使用手势将联系人添加到数据库中但是当我尝试删除特定的联系人时从使用手势的列表中,我无法做到。我对Android应用程序的开发很新。请建议我......我的代码也是。