dimanche 27 décembre 2015

How to retrieve data from sqlite database by clicking button and display in a listview

Hey guys im actually confused instead of looking for any new thing to add or correct it confused me and now i dont know what to add nor correct

I want to retrieve data from the sqlite database from clicking the button SEARCH TRIP then i want it to display in a listview by which i would also be able to click one of the listview and click it to direct me to another page displaying the info but im still having doubts in that

in my logcat there are no errors upon running and when i click the button SEARCH TRIP nothing happens there is no error im having these classes: DATABASEHELPER, LISTVIEWCURSORADAPTERACTIVITY, TRIPSDBADAPTER

The button is in the fragment since im having tabs in one of them the button is situated on the fragment when i implement the View.onclicklistener the on the method when i specify one of the classes such as LISTVIEWCURSORADAPTERACTIVITY or TRIPSDBADAPTER its shpowing error where should i put the connection of the button

If possible can anyone please help me with the code what have i done wrong?

DatabaseHelper.java

import android.content.Contect;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
public class DatabaseHelper extends SQLiteOpenHelper {


public static final String DATABASE_NAME = "stallion_db";
public static final String TABLE_NAME = "one-way";
public static final String COL_1 = "depart from";
public static final String COL_2 = "arrive_to";
public static final String COL_3 = "report_time";
public static final String COL_4 = "depart_on";
public static final String COL_5 = "departure time";
public static final String COL_6 = "price";


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME + "" + " (NO INTEGER PRIMARY KEY AUTOINCREMENT," +
            " depart_from TEXT, arrive_to TEXT, report_time VARCHAR, depart_on DATE, departure_time VARCHAR," +
            " price INTEGER); ");
    db.execSQL("INSERT INTO oneway values (1, mombasa, nairobi, 10:30AM, 25-12-2015, 11:00AM, 900)");
db.execSQL("SELECT FROM oneway WHERE depart_from = mombasa and arrive_to = nairobi");
}       


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

/**
 * All CRUD(Create, Read, Update, Delete) Operations
 */

// Getting All Contacts
public List<ConstructorOneWay> getAlltrips() {
    List<ConstructorOneWay> tripList = new ArrayList<ConstructorOneWay>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_NAME;



    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            ConstructorOneWay trip = new ConstructorOneWay();
            trip.setID(Integer.parseInt(cursor.getString(0)));
            trip.setDepart(cursor.getString(1));
            trip.setArrive(cursor.getString(2));
            trip.setReport(cursor.getString(3));
            trip.setdeparting(cursor.getString(4));
            trip.setprice(Integer.parseInt(cursor.getString(5)));
            // Adding trips to list
            tripList.add(trip);
        } while (cursor.moveToNext());
    }

    // return contact list
    return tripList;
}



}

ListViewCursorAdapterActivity.java

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ListViewCursorAdaptorActivity extends Activity {
private TripsdbAdapter dbHelper;
private SimpleCursorAdapter dataAdapter;  

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.trip_listview);

    dbHelper = new TripsdbAdapter(this);
    dbHelper.open();

    //Generate ListView from SQLite Database
    displayListView();
}

private void displayListView() {
    Cursor cursor = dbHelper.fetchallTrips();

    // The desired columns to be bound
    String[] columns = new String[] {
            TripsdbAdapter.Depart_from,
            TripsdbAdapter.Arrive_to,
            TripsdbAdapter.Depart_on,
            TripsdbAdapter.Departure_time,
            TripsdbAdapter.Price,
    };

    // the XML defined views which the data will be bound to
    int[] to = new int[] {
            R.id.tv1,
            R.id.tv2,
            R.id.tv3,
            R.id.tv4,
            R.id.tv5,

    };

    // create the adapter using the cursor pointing to the desired data
    //as well as the layout information
    dataAdapter = new SimpleCursorAdapter(
            this, R.layout.display_one_way_trip,
            cursor,
            columns,
            to,
            0);

    ListView listView = (ListView) findViewById(R.id.listView1);
    // Assign adapter to ListView
    listView.setAdapter(dataAdapter);


    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> listView, View view,
                                int position, long id) {
            // Get the cursor, positioned to the corresponding row in the result set
            Cursor cursor = (Cursor) listView.getItemAtPosition(position);
        }
    }); }}

TripsdbAdapter.java

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class TripsdbAdapter {



//FOR DISPLAYING AFTER SEARCH

public static final String ID = "id";
public static final String Depart_from = "Depart from";
public static final String Arrive_to = "Arrive to";
public static final String Depart_on = "Depart on";
public static final String Departure_time = "Departure time";
public static final String Price = "Price";

private static final String TAG = "TripsdbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;

private static final String DATABASE_NAME = "stallion_db";
private static final String SQLITE_TABLE = "oneway";
private static final int DATABASE_VERSION = 1;

private Context mCtx;

public TripsdbAdapter(Context mContext) {
    this.mCtx = mContext;
}


public void close() {
    if (mDbHelper != null) {
        mDbHelper.close();
    }
}

public Cursor fetchCountriesByName(String inputText) throws SQLException {
    Log.w(TAG, inputText);
    Cursor mCursor = null;
    if (inputText == null  ||  inputText.length () == 0)  {
        mCursor = mDb.query(SQLITE_TABLE, new String[] {Depart_from,
                        Arrive_to, Depart_on, Departure_time, Price},
                null, null, null, null, null);
    }
    else {
        mCursor = mDb.query(true, SQLITE_TABLE, new String[] {Depart_from,
                         Arrive_to, Depart_on, Departure_time, Price},
                Depart_from + " like '%" + inputText + "%'", null,
                null, null, null, null);
    }
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;

}

public Cursor fetchallTrips() {

    Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {Depart_from,
                    Arrive_to, Depart_on, Departure_time, Price},
            null, null, null, null, null);

    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}


public void open() {
} }

Aucun commentaire:

Enregistrer un commentaire