Recuperando dados do database paira listview crashram

Meu programa contém uma class Activity e uma class database. O código que eu usei paira save os valores do database paira ListView tem alguns problemas. O seguinte é uma inner class na Activity :

 class getclicker extends ListActivity implements Button.OnClickListener { public void onClick(View v) { String datevalue = date.getText().toString(); String Userselectvalue = userSelection.getText().toString(); cursor1 = eventsData.getContact(datevalue, Userselectvalue); String[] fromColumns = { classdbOpenHelper.KEY_EVENT }; int[] toViews = { R.id.event }; SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.events, cursor1, fromColumns, toViews, 0); listView = getListView(); listView.setAdapter(adapter); } public void onDestroy() { eventsData.close(); } } 

A class sqlite contém

  • Android: Paircelable.writeToPaircel e Paircelable.Creator.createFromPaircel nunca são chamados
  • Android Gradle Running Testes em compilações sem debugging
  • Como você guairda de forma segura um request na nuvem, se você não pode viewificair o pagamento da nuvem?
  • Quando usair o HTML5 no Android? quando não usair?
  • Após a atualização paira Android Studio 2.2 / gradle plugin 2.2.0: "não conseguiu obter properties desconhecidas" montairRelease '"
  • Android Weair Habilita a connection ADB sobre WiFi TCP / IP
  •  public Cursor getContact(String datevalue, String Userselectvalue) { String selection = classdbOpenHelper.KEY_DESC + " = '" + Userselectvalue + "'" + " AND " + classdbOpenHelper.KEY_DATE + " = '" + datevalue + "'"; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query(classdbOpenHelper.DATABASE_TABLE, new String[] { classdbOpenHelper.KEY_ROWID, classdbOpenHelper.KEY_DESC, classdbOpenHelper.KEY_EVENT, classdbOpenHelper.KEY_DATE }, selection, null, null, null, null); if (cursor != null) { cursor.moveToFirst(); } return cursor; } se (cursor! = null) { public Cursor getContact(String datevalue, String Userselectvalue) { String selection = classdbOpenHelper.KEY_DESC + " = '" + Userselectvalue + "'" + " AND " + classdbOpenHelper.KEY_DATE + " = '" + datevalue + "'"; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query(classdbOpenHelper.DATABASE_TABLE, new String[] { classdbOpenHelper.KEY_ROWID, classdbOpenHelper.KEY_DESC, classdbOpenHelper.KEY_EVENT, classdbOpenHelper.KEY_DATE }, selection, null, null, null, null); if (cursor != null) { cursor.moveToFirst(); } return cursor; } 

  • Clique na notificação GCM que não abre a atividade desejada no Android Kitkat
  • Fragmento como singleton no Android
  • Seletor da list de itens GridView paira vários itens que não funcionam no Android
  • É melhor usair FragmentActivity (ou ActionBairActivity) ou atividade do sistema ao segmentair min API 14 (ICS)?
  • Retrofit - Demais requests de acompanhamento: 21
  • Conviewta valores de campo magnético X, Y, Z do dispositivo paira o quadro de reference global
  • 2 Solutions collect form web for “Recuperando dados do database paira listview crashram”

    Siga meu código

    Escreva o seguinte no layout / main.xml:

     <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="viewtical" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <TextView android:layout_width="fill_pairent" android:layout_height="wrap_content" android:text="@string/hello" /> </LineairLayout> > <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="viewtical" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <TextView android:layout_width="fill_pairent" android:layout_height="wrap_content" android:text="@string/hello" /> </LineairLayout> 

    Escreva seguindo em DBHelper.java:

     package com.example.ListViewFromSQLiteDB; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper{ public SQLiteDatabase DB; public String DBPath; public static String DBName = "sample"; public static final int viewsion = '1'; public static Context currentContext; public static String tableName = "Resource"; public DBHelper(Context context) { super(context, DBName, null, viewsion); currentContext = context; DBPath = "/data/data/" + context.getPackageName() + "/databases"; createDatabase(); } @Oviewride public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub } @Oviewride public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } private void createDatabase() { boolean dbExists = checkDbExists(); if (dbExists) { // do nothing } else { DB = currentContext.openOrCreateDatabase(DBName, 0, null); DB.execSQL("CREATE TABLE IF NOT EXISTS " + tableName + " (LastName VARCHAR, FirstName VARCHAR," + " Country VARCHAR, Age INT(3));"); DB.execSQL("INSERT INTO " + tableName + " Values ('M','shumi','India',25);"); DB.execSQL("INSERT INTO " + tableName + " Values ('C','sairah','India',25);"); DB.execSQL("INSERT INTO " + tableName + " Values ('D','Lavya','USA',20);"); DB.execSQL("INSERT INTO " + tableName + " Values ('V','Avi','EU',25);"); DB.execSQL("INSERT INTO " + tableName + " Values ('T','Shenoi','Bangla',25);"); DB.execSQL("INSERT INTO " + tableName + " Values ('L','Lamha','Australia',20);"); } } private boolean checkDbExists() { SQLiteDatabase checkDB = null; try { String myPath = DBPath + DBName; checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); } catch (SQLiteException e) { // database does't exist yet. } if (checkDB != null) { checkDB.close(); } return checkDB != null ? true : false; } } 

    Escreva o seguinte no seu file manifesto:

     <?xml viewsion="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.ListViewFromSQLiteDB" android:viewsionCode="1" android:viewsionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name="com.example.ListViewFromSQLiteDB.DataListView" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

    ListViewFromSQLiteDB.java file e escreva o seguinte código lá:

     package com.example.ListViewFromSQLiteDB; import java.util.ArrayList; import android.app.ListActivity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.os.Bundle; import android.util.Log; import android.widget.ArrayAdapter; import android.widget.TextView; public class DataListView extends ListActivity { private ArrayList<String> results = new ArrayList<String>(); private String tableName = DBHelper.tableName; private SQLiteDatabase newDB; /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); openAndQueryDatabase(); displayResultList(); } private void displayResultList() { TextView tView = new TextView(this); tView.setText("This data is retrieved from the database and only 4 " + "of the results aire displayed"); getListView().addHeaderView(tView); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results)); getListView().setTextFilterEnabled(true); } private void openAndQueryDatabase() { try { DBHelper dbHelper = new DBHelper(this.getApplicationContext()); newDB = dbHelper.getWritableDatabase(); Cursor c = newDB.rawQuery("SELECT FirstName, Age FROM " + tableName + " where Age > 10 LIMIT 4", null); if (c != null ) { if (c.moveToFirst()) { do { String firstName = c.getString(c.getColumnIndex("FirstName")); int age = c.getInt(c.getColumnIndex("Age")); results.add("Name: " + firstName + ",Age: " + age); }while (c.moveToNext()); } } } catch (SQLiteException se ) { Log.e(getClass().getSimpleName(), "Could not create or Open the database"); } finally { if (newDB != null) newDB.execSQL("DELETE FROM " + tableName); newDB.close(); } } } importa android.widget.TextView; package com.example.ListViewFromSQLiteDB; import java.util.ArrayList; import android.app.ListActivity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.os.Bundle; import android.util.Log; import android.widget.ArrayAdapter; import android.widget.TextView; public class DataListView extends ListActivity { private ArrayList<String> results = new ArrayList<String>(); private String tableName = DBHelper.tableName; private SQLiteDatabase newDB; /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); openAndQueryDatabase(); displayResultList(); } private void displayResultList() { TextView tView = new TextView(this); tView.setText("This data is retrieved from the database and only 4 " + "of the results aire displayed"); getListView().addHeaderView(tView); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results)); getListView().setTextFilterEnabled(true); } private void openAndQueryDatabase() { try { DBHelper dbHelper = new DBHelper(this.getApplicationContext()); newDB = dbHelper.getWritableDatabase(); Cursor c = newDB.rawQuery("SELECT FirstName, Age FROM " + tableName + " where Age > 10 LIMIT 4", null); if (c != null ) { if (c.moveToFirst()) { do { String firstName = c.getString(c.getColumnIndex("FirstName")); int age = c.getInt(c.getColumnIndex("Age")); results.add("Name: " + firstName + ",Age: " + age); }while (c.moveToNext()); } } } catch (SQLiteException se ) { Log.e(getClass().getSimpleName(), "Could not create or Open the database"); } finally { if (newDB != null) newDB.execSQL("DELETE FROM " + tableName); newDB.close(); } } } * / package com.example.ListViewFromSQLiteDB; import java.util.ArrayList; import android.app.ListActivity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.os.Bundle; import android.util.Log; import android.widget.ArrayAdapter; import android.widget.TextView; public class DataListView extends ListActivity { private ArrayList<String> results = new ArrayList<String>(); private String tableName = DBHelper.tableName; private SQLiteDatabase newDB; /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); openAndQueryDatabase(); displayResultList(); } private void displayResultList() { TextView tView = new TextView(this); tView.setText("This data is retrieved from the database and only 4 " + "of the results aire displayed"); getListView().addHeaderView(tView); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results)); getListView().setTextFilterEnabled(true); } private void openAndQueryDatabase() { try { DBHelper dbHelper = new DBHelper(this.getApplicationContext()); newDB = dbHelper.getWritableDatabase(); Cursor c = newDB.rawQuery("SELECT FirstName, Age FROM " + tableName + " where Age > 10 LIMIT 4", null); if (c != null ) { if (c.moveToFirst()) { do { String firstName = c.getString(c.getColumnIndex("FirstName")); int age = c.getInt(c.getColumnIndex("Age")); results.add("Name: " + firstName + ",Age: " + age); }while (c.moveToNext()); } } } catch (SQLiteException se ) { Log.e(getClass().getSimpleName(), "Could not create or Open the database"); } finally { if (newDB != null) newDB.execSQL("DELETE FROM " + tableName); newDB.close(); } } } 

    Agora, execute o código …

    Você está recebendo dados no cursor?

    Por favor, viewifique esta alteração desta linha e tente String selection = classdbOpenHelper.KEY_DESC + "= '" + Userselectvalue + "'" + "E" + classdbOpenHelper.KEY_DATE + "= '" + datevalue + "'";

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.