4

I am working on a sqlite database project on android. And it's my first sqlite project. I read lots of article and created my app but there's a problem that I can't find a way to solve it. I hope you can show a way for me. The problem is when I call activity by clicking a button, the device fire up a message ("unfortunately app stopped").

I am trying to get strings from sqlite database to autocompletetextview.

Sqlite databasehelper class's codes

package com.example.matik;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class Veritabani extends SQLiteOpenHelper {

        private static final String VERITABANI="KAYISGDATA.db";
        private static final int SURUM=1;

         public static final String TABLE_TODO = "nace";
         public static final String COLUMN_ID = "_id";
         public static final String COLUMN_CATEGORY = "KOD";
         public static final String COLUMN_SUMMARY = "ACK";
         public static final String COLUMN_DESCRIPTION = "TEH";

         private static final String DB_DROP = "DROP TABLE IF EXISTS nace";
         private static final String DATABASE_CREATE = "create table " 
                  + TABLE_TODO
                  + "(" 
                  + COLUMN_ID + " integer primary key autoincrement, " 
                  + COLUMN_CATEGORY + " text not null, " 
                  + COLUMN_SUMMARY + " text not null," 
                  + COLUMN_DESCRIPTION
                  + " text not null" 
                  + ");";

    public Veritabani(Context con, String name, CursorFactory factory,
            int version) {
        super(con, VERITABANI, null, SURUM);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(DATABASE_CREATE);
        db.execSQL("INSERT INTO nace (KOD,ACK,TEH) VALUES('01.11.07','Baklagillerin yetiştirilmesi','Tehlikeli')");}
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(DB_DROP);
        onCreate(db);
    }


}

Activity Class's codes

package com.example.matik;
import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;

public class Matik extends Activity {
     AutoCompleteTextView auto;
     EditText nc;
     EditText th;
     Veritabani VB;
     private ArrayAdapter<String> arrayAdapter;
     ArrayList<String> Liste = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_matik);
        Liste = new ArrayList<String>();
        doldur();
        nc = (EditText) findViewById(R.id.editText2);
        th = (EditText) findViewById(R.id.editText3);
        auto = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
        arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_item, Liste);
        auto.setThreshold(1);
        auto.setAdapter(arrayAdapter);
    }

    private void doldur() {
        SQLiteDatabase Db = VB.getReadableDatabase();
        Cursor c = Db.rawQuery("Select ACT From nace", null);
        Db.isOpen();
        while(c.moveToNext()){
            Liste.add(c.getString(c.getColumnIndex("ACT")));
        };


    }

}
AADProgramming
  • 6,077
  • 11
  • 38
  • 58
  • Use LogCat to examine the Java stack trace generated when the "application stopped" dialog appears. – CommonsWare Sep 21 '13 at 00:39
  • Fatal Exception main error on LogCat – Oğuzhan Ahmet Arık Sep 21 '13 at 00:50
  • I would recommend you read up on how to interpret Java stack traces, as that will be important for you in your Java development: http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors http://stackoverflow.com/questions/12688068/how-to-read-and-understand-the-java-stack-trace If you have difficulty understanding what the error message(s) mean, copy and paste the *entire* stack trace into your StackOverflow question. – CommonsWare Sep 21 '13 at 00:52
  • thanks for your nicely comments, the problem is existing because of null expection of doldur method.09-21 00:53:50.974: E/AndroidRuntime(16022): at com.example.isgmatik.Matik.doldur(Matik.java:35) I think the database could not been created or empty. – Oğuzhan Ahmet Arık Sep 21 '13 at 01:01

1 Answers1

4

Use a SimpleCursorAdapter, as shown in this article.

_descriptionText = (AutoCompleteTextView) findViewById(R.id.description);
final int[] to = new int[]{android.R.id.text1};
final String[] from = new String[]{VehicleDescriptionsTable.DESCRIPTION};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
        android.R.layout.simple_dropdown_item_1line,
        null,
        from,
        to);

// This will provide the labels for the choices to be displayed in the AutoCompleteTextView
adapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() {
    @Override
    public CharSequence convertToString(Cursor cursor) {
        final int colIndex = cursor.getColumnIndexOrThrow(VehicleDescriptionsTable.DESCRIPTION);
        return cursor.getString(colIndex);
    }
});

// This will run a query to find the descriptions for a given vehicle.
adapter.setFilterQueryProvider(new FilterQueryProvider() {
    @Override
    public Cursor runQuery(CharSequence description) {
        String vehicle = getSelectedVehicle();
        Cursor managedCursor = _helper.getDescriptionsFor(vehicle, description.toString());
        Log.d(TAG, "Query has " + managedCursor.getCount() + " rows of description for " + vehicle);
        return managedCursor;
    }
});

_descriptionText.setAdapter(adapter);

From here, you just need to add a function to return a cursor with your database, and add the adapter to your AutoTextCompleteTextView. You will need to get _id from your logbook query, in fact, you might just consider getting the entire row (SELECT * FROM table). If you don't have a column _id, try SELECT data, rowid AS _id FROM table.

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142