Grundsätzlich, In dieser App versuche ich, die Liste des Arztes eines bestimmten Pflegeheims zu finden.In meiner zweiten Aktivität gibt es zwei Schaltfläche eins ist für Benutzer eine andere ist für Admin. Der Benutzer kann die Liste des Doktors mit Spezialisierung visualisieren, was immer Admin hinzufügen wird.Können Sie bitte den Fehler mit meinem Code herausfinden?
package com.example.lenovo.finalproject;
import android.app.Activity;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity5 extends Activity {
private final String DB_NAME = "My_DB2";
private final String TB_NAME = "TB2";
SQLiteDatabase db;
Spinner sp;
ListView l;
String str[] = {"Orthopaedics", "Cardiologist"};
String special;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity5);
// open the database
db = this.openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);
sp = (Spinner) findViewById(R.id.spinner3);
l = (ListView) findViewById(R.id.listView3);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, str);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
special = parent.getItemAtPosition(position).toString().trim();
Toast.makeText(getApplicationContext(), special, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// open the database
// db = this.openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);
initListViewData();
}
private void initListViewData() {
String selectQuery = " SELECT * FROM " + TB_NAME + " where speciality " + " = " + special;// problem arising these line//
String did;
String name;
String speciality;
String time;
String fees;
Cursor c = null;
try {
ArrayList<String> listItems = new ArrayList<String>();
c = db.rawQuery(selectQuery, null);
if (c != null) {
// if (c.moveToFirst()) {
c.moveToFirst();
do {
did = c.getString(c.getColumnIndex("id"));
name = c.getString(c.getColumnIndex("name"));
speciality = c.getString(c.getColumnIndex("speciality"));
time = c.getString(c.getColumnIndex("time"));
fees = c.getString(c.getColumnIndex("fees"));
String s = did + "\n " + name + "\n " + speciality + " \n" + time + "\n " + fees;
listItems.add(s);
} while (c.moveToNext());
// }
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, listItems);
// l=(ListView)findViewById(R.id.listView1);
l.setAdapter(adapter1);
} else {
Toast.makeText(getApplicationContext(), " No records found..", Toast.LENGTH_SHORT).show();
}
} catch (SQLException se) {
se.printStackTrace();
} finally {
if (null != c) {
c.close();
}
}
}
void show() {
}
In MainActivity4.java
Daten .Welche erweitert wird auf diese Aktivität durch eine Absicht verbunden.
Das Problem tritt auf, wenn ich Spinner in dieser Aktivität verwende. Die Zeile, die die String-Abfrage verwendet, macht das Problem. Wenn ich diese Zeile mit out Spinner benutze, wird es funktionieren. Gehen Sie den Code noch einmal durch, wenn Sie irgendeinen Fehler oder irgendeine Zeile finden werden, die benötigt wird.
Ich benutze Android Studion, API 19 (Jellybean).
Immer wenn ich versuche, auf die Schaltfläche Benutzer zu klicken, wird angezeigt, dass Ihre App leider nicht mehr funktioniert. Folgende Fehler zeigt in logcat
07-14 23:42:56.945 1853-1853/com.example.lenovo.finalproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lenovo.finalproject/com.example.lenovo.finalproject.MainActivity5}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.example.lenovo.finalproject.MainActivity5.initListViewData(MainActivity5.java:90)
at com.example.lenovo.finalproject.MainActivity5.onCreate(MainActivity5.java:69)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
@Subhendu Roy froh, Ihnen zu helfen .. – Ironman
Danke, es funktioniert. Aber auch das Hinzufügen von Daten zeigt es keinen Datensatz gefunden –
@SubhenduRoy versuchen, 'debuggen' Ihren Code und überprüfen Sie es .. – Ironman