我在这里有一个简单的应用程序,可以在模拟器上完美运行。但是显示应用程序在移动设备或标签页上运行时已停止。
我的主动就是这样。
public class MainActivity extends Activity {
EditText animalName;
DBController controller = new DBController(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
animalName = (EditText) findViewById(R.id.editText1);
final ListView lv=(ListView)findViewById(R.id.listView1);
final TextView tv=(TextView)findViewById(R.id.textView1);
final ArrayList<String> your_array_list = new ArrayList<String>();
final ArrayAdapter<String> arrayAdapter;
Button b2=(Button)findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setText(controller.getInfo());
}
});
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
String name=animalName.getText().toString();
controller.createEntry(name);
this.callHomeActivity(view);
}
private void callHomeActivity(View view) {
// TODO Auto-generated method stub
Intent objIntent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(objIntent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
和DBController.java
package com.example.dbapp;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Toast;
public class DBController extends SQLiteOpenHelper {
private static final String LOGCAT = null;
private static String DB_TABLE = "Animals";
private static String KEY_ROW = "animalId";
private static String KEY_NAME = "animalName";
public DBController(Context applicationcontext) {
super(applicationcontext, "androidsqlite.db", null, 1);
Log.d(LOGCAT,"Created");
}
@Override
public void onCreate(SQLiteDatabase database) {
String query;
//query = "CREATE TABLE" +DB_TABLE+"(" +KEY_ROW+" INTEGER PRIMARY KEY AUTOINCREMENT, " +KEY_NAME+ " TEXT NOT NULL);"
database.execSQL("CREATE TABLE" +DB_TABLE+"(" +KEY_ROW+" INTEGER PRIMARY KEY AUTOINCREMENT, " +KEY_NAME+ " TEXT NOT NULL);");
Log.d(LOGCAT,"animals Created");
}
@Override
public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
String query;
query = "DROP TABLE IF EXISTS animals";
database.execSQL(query);
onCreate(database);
}
public String getInfo()
{
SQLiteDatabase myDataBase = this.getReadableDatabase();
String s = null;
Cursor c=myDataBase.rawQuery("SELECT * FROM animals",null);
if(c.moveToFirst())
{
s=c.getString(c.getColumnIndex("animalName"));
}
return s;
}
public void createEntry(String name)
{
String s= "Insert success";
SQLiteDatabase database = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
database.insert("DB_TABLE",null,cv);
database.close();
}
}
应用必须从editText中获取值,并应插入到表中。点击显示时,它应显示第一行元素。