gpt4 book ai didi

android - TextView 长按监听器

转载 作者:行者123 更新时间:2023-11-29 01:55:37 24 4
gpt4 key购买 nike

我制作了一个 TextView,我想在单击它和长按它时制作一些东西。单击一次工作正常,但是当我长按 TextView 时,Activity 关闭。

这是我的代码。

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

谢谢。

最佳答案

数据库未打开 03-11 16:11:56.318: E/AndroidRuntime(11301): at 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;
}
});

关于android - TextView 长按监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15340246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com