我需要计算当前在我的sqlite数据库中的表.尝试搜索但没有得到任何直接的方法.
我在DbHelper中尝试了这种方法.
public int countTables() {
int count = 0;
String SQL_GET_ALL_TABLES = "SELECT * FROM sqlite_master WHERE type='table'";
Cursor cursor = getReadableDatabase()
.rawQuery(SQL_GET_ALL_TABLES, null);
cursor.moveToFirst();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
count++;
getReadableDatabase().close();
}
cursor.close();
return count;
}
Run Code Online (Sandbox Code Playgroud)
但这种方法给了我一个错误的计数.我的数据库中只有3个表,但此方法将计数返回为5.此方法可能出错.有没有直接的方法来计算.
我实现了一个弹出菜单,单击按钮即可显示该菜单。这是我的 onclick 方法。
public void showOverflow(View view) {
boolean click = true;
Button action = (Button) findViewById(R.id.btbAction);
LayoutInflater inflater = (LayoutInflater) main.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.overflow_layout, null);
final PopupWindow pw = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
pw.setOutsideTouchable(true);
if (click) {
pw.showAsDropDown(action, 0, 0);
click = false;
} else {
pw.dismiss();
click = true;
}
}
Run Code Online (Sandbox Code Playgroud)
单击按钮时会出现弹出窗口。现在的问题是,当我触摸弹出窗口之外时,窗口不会被关闭。我尝试将此属性设置为弹出窗口
pw.setOutsideTouchable(true);
Run Code Online (Sandbox Code Playgroud)
事情保持不变。请帮我解决这个问题
我正在尝试实现一个操作栏,其中一个按钮上的按钮显示一个弹出菜单.这是菜单.XML(操作栏中的菜单项)
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/search"
android:icon="@drawable/ic_action_search"
android:orderInCategory="0"
android:showAsAction="always"
android:title="@string/menu_search"/>
<item
android:id="@+id/refresh"
android:icon="@drawable/ic_action_refresh"
android:orderInCategory="1"
android:showAsAction="always"
android:title="@string/menu_refresh"/>
<Item
android:id="@+id/popup"
android:icon="@drawable/ic_action_search"
android:onClick="showPopup"
android:orderInCategory="1"
android:showAsAction="always"
android:title="@string/menu_search" />
Run Code Online (Sandbox Code Playgroud)
我希望在点击ID为"@ + id/popup"的项目时显示一个弹出菜单.
这是弹出菜单的XML
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/item1"
android:icon="@drawable/ic_action_search"
android:orderInCategory="0"
android:showAsAction="always"
android:title="@string/menu_search"/>
<item
android:id="@+id/item2"
android:icon="@drawable/ic_action_search"
android:orderInCategory="1"
android:showAsAction="always"
android:title="@string/menu_search"/>
Run Code Online (Sandbox Code Playgroud)
这是按钮的onClick方法
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.overflow, popup.getMenu());
popup.show();
}
Run Code Online (Sandbox Code Playgroud)
问题是单击该按钮时不会显示弹出窗口.需要帮助的人.
我需要将密度无关的像素转换为活动中的像素.我尝试在onCreate中使用此方法
float pix = dipValue * getResources().getDisplayMetrics().density;
pixel = Math.round(pix);
Run Code Online (Sandbox Code Playgroud)
我尝试了另一种方法
Resources r = getResources();
float pix = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 35,r.getDisplayMetrics());
value = Math.round(pix);
Run Code Online (Sandbox Code Playgroud)
两者都产生相同的价值.我认为问题不在于方法,而在于资源.我应该在Manifest文件中进行任何更改吗?
<supports-screens android:resizeable=["true"| "false"]
android:smallScreens=["true" | "false"]
android:normalScreens=["true" | "false"]
android:largeScreens=["true" | "false"]
android:xlargeScreens=["true" | "false"]
android:anyDensity=["true" | "false"]
android:requiresSmallestWidthDp="integer"
android:compatibleWidthLimitDp="integer"
android:largestWidthLimitDp="integer"/>
Run Code Online (Sandbox Code Playgroud)
我有三个活动 - AB和C,其中B是Tab活动.活动A首先启动,B从A启动.当使用此代码启动B时,我完成了活动A.
public void onStop() {
super.onStop();
this.finish();
}
Run Code Online (Sandbox Code Playgroud)
现在我想在B中按下后退键时启动Activity C.
我尝试使用此代码覆盖后退密钥
@Override
public void onBackPressed() { this.getParent().onBackPressed();
}
Run Code Online (Sandbox Code Playgroud)
在启动子活动时完成父活动时,这不会有帮助.按回键时实际发生的是活动退出到主屏幕.
我尝试覆盖后退键并设置一个Intent
@Override
public void onBackPressed() {
Intent backIntent = new Intent();
backIntent.setClass(this, main.class);
startActivity(backIntent);
}
Run Code Online (Sandbox Code Playgroud)
这对我也没有帮助.什么可能是这个问题的可能解决方案,如何在按下后退键时启动Activity C?
我有两个活动A和B.A在应用程序启动时启动.我有一个从A发起的服务.
这是活动A中的代码.
Button btnStart = (Button) findViewById(R.id.button1);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startService(new Intent(getBaseContext(), Service_class.class));
}
});
Run Code Online (Sandbox Code Playgroud)
这是我服务中的onStartCommand方法.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
createNotification();
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
Run Code Online (Sandbox Code Playgroud)
这是createNotification方法
private void createNotification() {
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent nintent = new Intent();
nintent.setClass(this, TestActivity.class);
PendingIntent pin = PendingIntent.getActivity(getApplicationContext(),
0, nintent, 0);
String title = "KR Darsan";
String body = "This is a notification from darsh";
Notification n …Run Code Online (Sandbox Code Playgroud) 我有一个MySQL表有三列id,名称和高度.
我想运行一个查询来返回所有条目的名称和高度,以及所有条目的平均高度.所有这些都必须在一个查询中完成.
这是我试过的.
SELECT name, height, avg(height) as average_height FROM mytable
Run Code Online (Sandbox Code Playgroud)
此查询仅返回单行,但平均高度正确.
name height average_height
--------------------------------
Harry 165 169.5
Run Code Online (Sandbox Code Playgroud)
然后我尝试了这个查询
SELECT name, height, avg(height) as average_height FROM mytable GROUP BY name
Run Code Online (Sandbox Code Playgroud)
然后它返回所有行,但平均值不正确
name height average_height
--------------------------------
Harry 165 165
Tom 170 170
George 180 180
Raj 163 163
Run Code Online (Sandbox Code Playgroud)
如何在单个查询中获取平均值和所有行.