2

我做了一個TextView,我想做點什麼,當我點擊它,當我長時間點擊它。單擊工作正常,但當我長時間點擊活動關閉TextViewTextView longclick listener

這是我的代碼。

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()); 

的logcat:

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 

謝謝。

+1

請發佈logcat。 – Egor 2013-03-11 13:50:15

+0

請發佈日誌以及您的活動的整個代碼。 – 2013-03-11 13:51:40

+0

問題在哪裏? – stinepike 2013-03-11 13:51:57

回答

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; 
       } 
      }); 
+0

的logcat的我以前開它,如果(c.moveToFirst()) – nestorasg 2013-03-11 15:23:04

+0

你應該分享db.delpin(名稱)方法的代碼 – Talha 2013-03-11 15:24:58

+0

這是代碼: 公共布爾delpin(字符串名稱){ \t return db.delete(「Pin」,「Name =」+ name,null)> 0; } – nestorasg 2013-03-11 17:29:00