1

i am developing an application and i need to put times of my alarm in a listview like that

  • 12:45
  • 13:11

my problem is and i know why is like that

that i get only one colmun like that

  • 45
  • 11

i want to get all the row from database not only one column item , how can i get all the row from database ? is there any easy solution?

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


}

Blockquote

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

    }




}
1
  • You should use a CursorAdapter instead of a ListAdapter. Commented Aug 1, 2017 at 13:25

3 Answers 3

2

You probably missed the Hour column from database :

change:

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

to:

  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);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

fetching colmn value like theList.add(data.getString(0) + ":" + data.getString(2)); is not good practice to fetch colomn try to suggest this String col2_value=data.getString(data.getColumnIndex(COL2));
@Pavneet_Singh i dont know what are you talking about ?. I posted an answer. i re-read it. i found an modification. i edited my answer ? how is that plagiarism ?
2
theList.add(data.getString(2));

Here you are getting the data from column index 2 and putting it into your list. This column appears to be the minutes. If you want the hour as well, you have to get it from the Cursor. Remember that a computer does only what you tell it to do.

Alternatively, you could use a CursorAdapter. Most likely you will need to create a custom adapter class yourself in order to get the values from the cursor and display them correctly.

Comments

0

Try fetching like this from cursor and use accordingly...

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

And also notify your adapter after adding all values in list like that...

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.