在我的基于android数据库的应用程序中。当我从数据库中检索数据时,它一次又一次获得相同的值(第一个值插入数据库)。有什么问题?
插入数据类
public class MainActivity extends AppCompatActivity {
public String dataBase_Name="database";
public static DatabaseHelper database;
private TextView text;
private GestureDetector gestureDetector;
private View.OnTouchListener gestureListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DatabaseHelper database1=new DatabaseHelper(this,dataBase_Name);
database=database1;
Button add=(Button)findViewById(R.id.button);
text=(TextView)findViewById(R.id.editText);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String temp=text.getText().toString();
database.insert(temp);
text.setText("");
}
});
Button next=(Button) findViewById(R.id.button6);
Button prev=(Button) findViewById(R.id.button4);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Delete.class);
MainActivity.this.startActivity(intent);
}
});
prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, com.example.partha.app4.View.class);
MainActivity.this.startActivity(intent);
}
});
}
}
检索课程
public class View extends MainActivity {
private GestureDetector gestureDetector;
private android.view.View.OnTouchListener gestureListener;
ArrayAdapter<String> adapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
listView = (ListView) findViewById(R.id.listView);
String[] load = database.get_all_entry();
String[] temp=database.get_all_entry();
Toast.makeText(this,"Length "+temp.length,Toast.LENGTH_LONG);
for(int i=0;i<temp.length;i++)
{
Toast.makeText(this,temp[i],Toast.LENGTH_SHORT);
}
if (load != null) {
adapter = new ArrayAdapter<String>(this, R.layout.item,R.id.textView, database.get_all_entry());
listView.setAdapter(adapter);
} else {
Toast.makeText(this, "Data cant found", Toast.LENGTH_SHORT);
}
Button next = (Button) findViewById(R.id.button11);
Button prev = (Button) findViewById(R.id.button10);
next.setOnClickListener(new android.view.View.OnClickListener() {
@Override
public void onClick(android.view.View v) {
clear();
Intent intent = new Intent(View.this, MainActivity.class);
View.this.startActivity(intent);
}
});
prev.setOnClickListener(new android.view.View.OnClickListener() {
@Override
public void onClick(android.view.View v) {
clear();
Intent intent = new Intent(View.this, Update.class);
View.this.startActivity(intent);
}
});
} catch (Exception e) {
Log.e("view", e.toString());
}
}
void clear()
{
listView.setAdapter(null);
}
}
Databasehelper Class
public class DatabaseHelper extends SQLiteOpenHelper {
public static String databaseName;
public static final String CONTACTS_TABLE_NAME = "contacts";
public static final String CONTACTS_COLUMN_ID = "id";
public static final String CONTACTS_COLUMN_NAME = "name";
Context context1;
DatabaseHelper(Context context,String databaseName)
{
super(context,databaseName,null,1);
this.databaseName=databaseName;
context1=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table contacts" + "(" + CONTACTS_COLUMN_ID + " integer primary key,name text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
public boolean insert(String name)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("name", name);
db.insert("contacts", null, contentValues);
return true;
}
public Cursor getData(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from contacts where id="+id+"", null );
return res;
}
public boolean update (Integer id, String name)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
db.update("contacts", contentValues, "id = ? ", new String[]{Integer.toString(id)});
return true;
}
public int find(String name)
{
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor=db.query("contacts", new String[]{"name"}, "name = ?", new String[]{name}, null, null, null);
cursor.moveToFirst();
if((cursor!=null)&&(cursor.getCount()!=0)) {
int id = cursor.getInt(cursor.getColumnIndex(CONTACTS_COLUMN_ID));
return id;
}
else
{
return -1;
}
}
public Integer delete (Integer id)
{
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("contacts",
"id = ? ",
new String[] { Integer.toString(id) });
}
public String[] get_all_entry()
{
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor=db.query("contacts",null,null,null,null,null,null);
cursor.moveToFirst();
if((cursor!=null)&&(cursor.getCount()!=0))
{
String []name=new String[cursor.getCount()];
for(int i=0;i<cursor.getCount();i++)
{
name[i]=(cursor.getString(cursor.getColumnIndex(CONTACTS_COLUMN_NAME)));
}
return name;
}
else
{
return null;
}
}
}
答案 0 :(得分:0)
您在cursor.moveToNext();
的循环中遗漏了get_all_entry()
所以你在调用cursor.moveToFirst();
修改你的for循环如下:
for(int i=0;i<cursor.getCount();i++){
name[i]=(cursor.getString(cursor.getColumnIndex(CONTACTS_COLUMN_NAME)));
cursor.moveToNext(); // <---- ADD THIS
}
P.S:考虑使用while(cursor.moveToNext()){}
来读取光标记录,我认为这比使用for loop
更好