TextView longclick监听器

时间:2013-03-11 13:48:06

标签: android android-activity onclicklistener onlongclicklistener

我制作了一个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());

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 个答案:

答案 0 :(得分: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;
                }
            });