我正在开发一个应用程序,我需要把我的报警时间放在这样的列表视图中
12时45分
13点11分
我的问题是我知道为什么
我只得到一个这样的科尔曼
四十五
十一
我想从数据库中获取所有行,而不仅仅是一个列项,如何才能从数据库中获取所有行?有什么简单的解决办法吗?
包装无线电调频;
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;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";
private static final String TABLE_NAME = "people_table";
private static final String COL1 = "_id";
private static final String COL2 = "Hour";
private static final String COL3 = "Symbol";
private static final String COL4 = "Minutes";
public DatabaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (" +
COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 + " TEXT, " +
COL3 + " TEXT, " +
COL4 + " TEXT " +
");";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item1,String item2,String item3) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item1);
contentValues.put(COL3, item2);
contentValues.put(COL4, item3);
Log.d(TAG, "addData: Adding " + item1 + " to " + TABLE_NAME);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
public Cursor getListContents(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("SELECT Hour, Symbol, Minutes FROM " + TABLE_NAME, null);
return data;
}
}
块引用
package radiofm.arabel;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class Add_Alarm extends AppCompatActivity {
ListView listalarm;
DatabaseHelper myDB;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add__alarm);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
listalarm =(ListView) findViewById(R.id.listalarm);
myDB = new DatabaseHelper(this);
button1 = (Button) findViewById(R.id.addalarm);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent Intent = new Intent(view.getContext(), Alarm.class);
view.getContext().startActivity(Intent);}
});
//populate an ArrayList<String> from the database and then view it
ArrayList<String> theList = new ArrayList<>();
Cursor data = myDB.getListContents();
if(data.getCount() == 0){
Toast.makeText(this, "There are no contents in this list!",Toast.LENGTH_LONG).show();
}else{
while(data.moveToNext()){
theList.add(data.getString(2));
ListAdapter listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,theList);
listalarm.setAdapter(listAdapter);
}
}
}
}
最佳答案
您可能错过了数据库中的Hour
列:
更改:
if(data.getCount() == 0){
Toast.makeText(this, "There are no contents in this list!",Toast.LENGTH_LONG).show();
}else{
while(data.moveToNext()){
theList.add(data.getString(2));
ListAdapter listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,theList);
listalarm.setAdapter(listAdapter);
}
}
致:
if(data.getCount() == 0){
Toast.makeText(this, "There are no contents in this list!",Toast.LENGTH_LONG).show();
}else{
while(data.moveToNext()){
theList.add(data.getString(0) + ":" + data.getString(2));
}
ListAdapter listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,theList);
listalarm.setAdapter(listAdapter);
}