在游标数据中添加值

时间:2017-08-01 13:20:12

标签: android database listview

我正在开发一个应用程序,我需要将我的警报时间放在像这样的列表视图中

  • 12:45
  • 13:11

我的问题是,我知道为什么会那样

我只得到一个像那样的colmun

  • 45
  • 11

我想从数据库获取所有行而不仅仅是一个列项,如何从数据库中获取所有行?有什么简单的解决方案吗?

包radiofm.arabel;

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);
            }
        }

    }




}

3 个答案:

答案 0 :(得分:2)

您可能错过了数据库中的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);
    }

答案 1 :(得分:2)

theList.add(data.getString(2));

在这里,您将从列索引2获取数据并将其放入列表中。此列似乎是分钟。 如果您还想要小时,则必须从Cursor获取。请记住,计算机只会执行您告诉它的操作。

或者,您可以使用CursorAdapter。您很可能需要自己创建自定义适配器类,以便从光标获取值并正确显示它们。

答案 2 :(得分:0)

尝试从光标中取出这样的内容并相应地使用...

String col1_value=data.getString(data.getColumnIndex(COL1));
String col2_value=data.getString(data.getColumnIndex(COL2));
String col3_value=data.getString(data.getColumnIndex(COL3));

并在列表中添加所有值后通知您的适配器...

 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(data.getColumnIndex(COL2)));

            }
 ListAdapter listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,theList);
                listalarm.setAdapter(listAdapter);
        }