2

私は2つの活動をしています。ユーザーがリストの薬名をクリックすると、2 番目のアクティビティに移動し、薬の詳細が表示されます。最初のアクティビティについては、以下が私のコードです。

public class MedicineView extends Activity implements AdapterView.OnItemClickListener
{
private SQLiteDatabase database;
private ArrayList<String> result = new ArrayList<String>(); 
private ArrayList<Long> idList = new ArrayList<Long>();
private ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.medicine_view);
    ListView listContent = (ListView)findViewById(R.id.listView1);

    DBHelper helper = new DBHelper(this);
    database = helper.getWritableDatabase();

    // view data

    try{
        String query = "select * from " + DBHelper.TABLE_MEDICINE;

        Cursor c = database.rawQuery(query, null);

        if(c!=null)
        {
            c.moveToFirst();
            int getIndex = c.getColumnIndex(DBHelper.MED_ID);
            int getNameIndex = c.getColumnIndex(DBHelper.MED_NAME);
            int getDosageIndex = c.getColumnIndex(DBHelper.DOSAGE);
            int getFrequencyIndex = c.getColumnIndex(DBHelper.FREQUENCY);

            if(c.isFirst())
            {
                do{
                    idList.add(c.getLong(getIndex));
                    result.add(c.getString(getNameIndex)+" | " + c.getString(getDosageIndex)
                            +" | " + c.getString(getFrequencyIndex)+"\n" );
                }while(c.moveToNext());
            }
        }

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,result);
        listContent.setAdapter(adapter);
        listContent.setTextFilterEnabled(true);

        c.close();

    }
    catch(SQLException e){  
    }

    listContent.setOnItemClickListener(this);
}

public void onClickHome(View v){
    startActivity (new Intent(getApplicationContext(), MenuUtama.class));
}

public void onClickAdd(View v){
    startActivity (new Intent(getApplicationContext(), MedicineAdd.class));
}

@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    Intent myIntent = new Intent(v.getContext(), MedicineDetail.class);
    myIntent.putExtra("ID", id);
    startActivity(myIntent);
}
} // end class

これは私の2番目の活動です

public class MedicineDetail extends Activity 
 {
private SQLiteDatabase database;
private ArrayList<Long> idList = new ArrayList<Long>();
ArrayList<String> list = new ArrayList<String>();

Button btnEdit;

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

    Intent i = getIntent();
    final int id = i.getIntExtra("int", -1);
    //int id = getIntent().getIntExtra("id", 2);

    btnEdit = (Button)findViewById(R.id.imageButton2);

    DBHelper helper = new DBHelper(this);
    database = helper.getWritableDatabase();

    String sql = "select * from " + DBHelper.TABLE_MEDICINE + " WHERE id = " + id;
    Cursor cursor = database.rawQuery(sql, null);

    try{
        if( cursor != null ){
            if (cursor.moveToFirst()) {
               do {
                   idList.add(cursor.getLong(0));
                   list.add(cursor.getString(1));
                   list.add(cursor.getString(2));
                   list.add(cursor.getString(3));
                   list.add(cursor.getString(4));
                   list.add(cursor.getString(5));
                   list.add(cursor.getString(6));
               } while (cursor.moveToNext());
            }
        }
    }
    catch(SQLException e){  
    }

    cursor.moveToFirst();

    TextView StartDate = (TextView) findViewById(R.id.textView8);
    TextView EndDate = (TextView)findViewById(R.id.textView9);
    TextView MedName = (TextView)findViewById(R.id.textView10);
    TextView Dosage = (TextView)findViewById(R.id.textView11);
    TextView Frequency =    (TextView)findViewById(R.id.textView12);
    TextView Instruction =  (TextView)findViewById(R.id.textView13);

    StartDate.setText(cursor.getString(1));
    EndDate.setText(cursor.getString(2));
    MedName.setText(cursor.getString(3));
    Dosage.setText(cursor.getString(4));
    Frequency.setText(cursor.getString(5));
    Instruction.setText(cursor.getString(6));
    cursor.close();

}

public void onClickHome(View v){
    startActivity (new Intent(getApplicationContext(), MedicineView.class));
}

} // end class

これは私のlogcatです

03-05 08:55:08.985: W/Bundle(2186): Key ID expected Integer but value was a    java.lang.Long.  The default value -1 was returned.
03-05 08:55:09.005: W/Bundle(2186): Attempt to cast generated internal exception:
03-05 08:55:09.005: W/Bundle(2186): java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
03-05 08:55:09.005: W/Bundle(2186):     at android.os.Bundle.getInt(Bundle.java:933)
03-05 08:55:09.005: W/Bundle(2186):     at android.content.Intent.getIntExtra(Intent.java:3683)
03-05 08:55:09.005: W/Bundle(2186):     at com.android.MyDoc.MedicineDetail.onCreate(MedicineDetail.java:32)
03-05 08:55:09.005: W/Bundle(2186):     at android.app.Activity.performCreate(Activity.java:4465)
03-05 08:55:09.005: W/Bundle(2186):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-05 08:55:09.005: W/Bundle(2186):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
03-05 08:55:09.005: W/Bundle(2186):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
03-05 08:55:09.005: W/Bundle(2186):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
03-05 08:55:09.005: W/Bundle(2186):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
03-05 08:55:09.005: W/Bundle(2186):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 08:55:09.005: W/Bundle(2186):     at android.os.Looper.loop(Looper.java:137)
03-05 08:55:09.005: W/Bundle(2186):     at android.app.ActivityThread.main(ActivityThread.java:4340)
03-05 08:55:09.005: W/Bundle(2186):     at java.lang.reflect.Method.invokeNative(Native Method)
03-05 08:55:09.005: W/Bundle(2186):     at  java.lang.reflect.Method.invoke(Method.java:511)
03-05 08:55:09.005: W/Bundle(2186):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-05 08:55:09.005: W/Bundle(2186):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-05 08:55:09.005: W/Bundle(2186):     at dalvik.system.NativeStart.main(Native Method)
03-05 08:55:09.053: D/AndroidRuntime(2186): Shutting down VM
03-05 08:55:09.053: W/dalvikvm(2186): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
03-05 08:55:09.093: E/AndroidRuntime(2186): FATAL EXCEPTION: main
03-05 08:55:09.093: E/AndroidRuntime(2186): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.MyDoc/com.android.MyDoc.MedicineDetail}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
03-05 08:55:09.093: E/AndroidRuntime(2186):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
03-05 08:55:09.093: E/AndroidRuntime(2186):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
03-05 08:55:09.093: E/AndroidRuntime(2186):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
03-05 08:55:09.093: E/AndroidRuntime(2186):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
03-05 08:55:09.093: E/AndroidRuntime(2186):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 08:55:09.093: E/AndroidRuntime(2186):     at android.os.Looper.loop(Looper.java:137)
03-05 08:55:09.093: E/AndroidRuntime(2186):     at android.app.ActivityThread.main(ActivityThread.java:4340)
03-05 08:55:09.093: E/AndroidRuntime(2186):     at java.lang.reflect.Method.invokeNative(Native Method)
03-05 08:55:09.093: E/AndroidRuntime(2186):     at java.lang.reflect.Method.invoke(Method.java:511)
03-05 08:55:09.093: E/AndroidRuntime(2186):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-05 08:55:09.093: E/AndroidRuntime(2186):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-05 08:55:09.093: E/AndroidRuntime(2186):     at dalvik.system.NativeStart.main(Native Method)
03-05 08:55:09.093: E/AndroidRuntime(2186): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
03-05 08:55:09.093: E/AndroidRuntime(2186):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:434)
03-05 08:55:09.093: E/AndroidRuntime(2186):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
03-05 08:55:09.093: E/AndroidRuntime(2186):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
03-05 08:55:09.093: E/AndroidRuntime(2186):     at com.android.MyDoc.MedicineDetail.onCreate(MedicineDetail.java:70)
03-05 08:55:09.093: E/AndroidRuntime(2186):     at android.app.Activity.performCreate(Activity.java:4465)
03-05 08:55:09.093: E/AndroidRuntime(2186):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-05 08:55:09.093: E/AndroidRuntime(2186):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
03-05 08:55:09.093: E/AndroidRuntime(2186):     ... 11 more
4

4 に答える 4

1

2番目のアクティビティに問題があります

in the first activity 



 Intent myIntent = new Intent(v.getContext(), MedicineDetail.class);
    myIntent.putExtra("ID", position);
    startActivity(myIntent);



final int id = i.getIntExtra("int", -1); should be

final int id = i.getIntExtra("ID", -1);
于 2013-03-05T06:32:00.300 に答える
1

発信者アクティビティには次のものがあります。myIntent.putExtra("ID", id);

代わりに開かれたアクティビティにいる間

i.getIntExtra("int", -1);

そのはず

i.getIntExtra("ID", -1);

インテントからエクストラを取得しようとするとき、エクストラの名前は同じでなければならないことに注意してください。

于 2013-03-05T06:32:52.507 に答える
0

最初のアクティビティでをに変更idpositionます。それで全部です。

 myIntent.putExtra("ID", position);

そして2番目に

getIntExtra("ID", -1);

また

getExtra("ID", -1);
于 2013-03-05T06:43:18.153 に答える
0

編集: long 値を取得していますが、id は int を返すため、クラスキャスト例外がスローされます

これの代わりに

idList.add(cursor.getLong(0));

これを使って

 idList.add(cursor.getInt(0));

あなたの ID はテーブルの行 ID とは異なります.それらを台無しにしないでください.リスト内の位置の ID を送信しています. だからあなたがする必要があるのは

ハッシュマップを作成して、ID とデータをグローバルにマッピングします

Hashmap Idshash=new Hashmap();

結果リストへの挿入中に hasmap に値を追加する

long id=c.getLong(getIndex);
String item=c.getString(getNameIndex)+" | " + c.getString(getDosageIndex)
                            +" | " + c.getString(getFrequencyIndex);
   idList.add(id);
Idshash.put(item,id);
  result.add(item);

そしてonitemclickリスナーではこのようにします

@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    String item=((TextView)v).getText();
    long selectedid=Idshash.get(item);
    Intent myIntent = new Intent(v.getContext(), MedicineDetail.class);
    myIntent.putExtra("ID", selectedid);
    startActivity(myIntent);
}
于 2013-03-05T06:47:41.277 に答える