2

私はTextViewそれをシングルクリックしたり、ロングクリックしたりしたときに何かを作りたいと思っています。シングルクリックは正常に機能していますがTextView、アクティビティを長くクリックすると閉じます。

これが私のコードです。

if (c.moveToFirst())
    {
        do
        {
            final String name = c.getString(0);
            TextView resname = new TextView(this);
            View line = new View(this);
            //ImageButton pin = new ImageButton(this);

            resname.setText(c.getString(0));
            resname.setTextColor(Color.WHITE);
            resname.setTextSize(30);
            resname.setTypeface(null,Typeface.BOLD);
            resname.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            resname.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent i = new Intent();
                    if (getIntent().getStringExtra("type").contentEquals("Restaurant")){
                        i.setClassName("com.mdl.cyrestaurants.guide", "com.mdl.cyrestaurants.guide.RestaurantDetails");
                        i.putExtra("name",name);
                    }
                    else if(getIntent().getStringExtra("type").contentEquals("Cafe")){
                        i.setClassName("com.mdl.cyrestaurants.guide", "com.mdl.cyrestaurants.guide.CafeDetails");
                        i.putExtra("name",name);
                    }
                    else if(getIntent().getStringExtra("type").contentEquals("Bar")){
                        i.setClassName("com.mdl.cyrestaurants.guide", "com.mdl.cyrestaurants.guide.BarDetails");
                        i.putExtra("name",name);
                    }
                    startActivity(i);
                }
            });

            resname.setOnLongClickListener(new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {
                    if(db.delpin(name))
                        Toast.makeText(getBaseContext(), "Delete successful", Toast.LENGTH_SHORT).show();
                    else
                        Toast.makeText(getBaseContext(), "Delete failed", Toast.LENGTH_SHORT).show();

                    Intent i = new Intent();
                    i.setClassName("com.mdl.cyrestaurants.guide", "com.mdl.cyrestaurants.guide.pins");
                    i.putExtra("type", getIntent().getStringExtra("type"));
                    startActivity(i);
                    finish();

                    return false;
                }
            });

            //pin.setImageResource(R.drawable.pin);
            //pin.setBackgroundDrawable(getResources().getDrawable(R.drawable.unpin));
            //pin.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));


            line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,2));
            line.setBackgroundColor(Color.RED);

            ((LinearLayout) layout).addView(resname);
            //((LinearLayout) layout).addView(pin);
            ((LinearLayout) layout).addView(line);
        }while(c.moveToNext());

ログキャット:

03-11 16:11:56.318: E/AndroidRuntime(11301): FATAL EXCEPTION: main
03-11 16:11:56.318: E/AndroidRuntime(11301): java.lang.IllegalStateException: database not open
03-11 16:11:56.318: E/AndroidRuntime(11301):    at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1663)
03-11 16:11:56.318: E/AndroidRuntime(11301):    at com.mdl.cyrestaurants.guide.DBAdapter.delpin(DBAdapter.java:422)
03-11 16:11:56.318: E/AndroidRuntime(11301):    at com.mdl.cyrestaurants.guide.pins$2.onLongClick(pins.java:69)
03-11 16:11:56.318: E/AndroidRuntime(11301):    at android.view.View.performLongClick(View.java:2427)
03-11 16:11:56.318: E/AndroidRuntime(11301):    at android.widget.TextView.performLongClick(TextView.java:7430)
03-11 16:11:56.318: E/AndroidRuntime(11301):    at android.view.View$CheckForLongPress.run(View.java:8793)
03-11 16:11:56.318: E/AndroidRuntime(11301):    at android.os.Handler.handleCallback(Handler.java:587)
03-11 16:11:56.318: E/AndroidRuntime(11301):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-11 16:11:56.318: E/AndroidRuntime(11301):    at android.os.Looper.loop(Looper.java:143)
03-11 16:11:56.318: E/AndroidRuntime(11301):    at android.app.ActivityThread.main(ActivityThread.java:4914)
03-11 16:11:56.318: E/AndroidRuntime(11301):    at java.lang.reflect.Method.invokeNative(Native Method)
03-11 16:11:56.318: E/AndroidRuntime(11301):    at java.lang.reflect.Method.invoke(Method.java:521)
03-11 16:11:56.318: E/AndroidRuntime(11301):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
03-11 16:11:56.318: E/AndroidRuntime(11301):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-11 16:11:56.318: E/AndroidRuntime(11301):    at dalvik.system.NativeStart.main(Native Method)
03-11 16:11:58.638: I/Process(11301): Sending signal. PID: 11301 SIG: 9

ありがとうございました。

4

1 に答える 1

1

データベースが開いていません 03-11 16:11:56.318: E/AndroidRuntime(11301): android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1663) で

接続を開かずにデータベース操作をしようとしています

resname.setOnLongClickListener(new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {
                  try
                  {
                    if(db.delpin(name))
                        Toast.makeText(getBaseContext(), "Delete successful", Toast.LENGTH_SHORT).show();
                    else
                        Toast.makeText(getBaseContext(), "Delete failed", Toast.LENGTH_SHORT).show();

                    Intent i = new Intent();
                    i.setClassName("com.mdl.cyrestaurants.guide", "com.mdl.cyrestaurants.guide.pins");
                    i.putExtra("type", getIntent().getStringExtra("type"));
                    startActivity(i);
                    finish();
                  }catch(Exception e)
                  {
                     // ---------------------------------
                      // handle error code here
                      // You have problem with db.delpin(name)
                     // ---------------------------------

                      return false;
                  }
                    return false;
                }
            });
于 2013-03-11T14:40:54.830 に答える