《浮动搜索框》看的我眼睛都花了,到底哪错了。

Crazy_hand 2013-02-02 05:17:29
利用Toast调试时正常显示了,调试信息,但没有看到搜索框

//这是Manifest.xml里面的配置
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Learn.diywidget"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<activity
android:name=".DIYWidget"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.default_searchable"
android:value=".DIYSearchManager"/>
</activity>
<activity
android:name=".DIYGallery"
android:label="Gallery">
</activity>
<activity
android:name=".DIYGridView"
android:label="GridView">
</activity>
<activity
android:name=".DIYListView"
android:label="DIYListView">
</activity>
<activity
android:name=".DIYListActivity"
android:label="ListActivity">
</activity>
<activity
android:name=".DIYSearchFile"
android:label="SearchFile">
</activity>
<activity
android:name=".DIYSearchManager"
android:label="SearchManager">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
</application>

</manifest>


//searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="search_label"
android:hint="search_hint"
android:searchSuggestAuthority="com.android.cbin.SearchSuggestionSampleProvider"
android:searchSuggestSelection=" ? "
>
</searchable>



//代码是照抄的android 开发范例里的内容
package Learn.diywidget;

import java.io.InputStream;
import java.util.ArrayList;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("UseValueOf")
public class DIYSearchManager extends Activity{
private static final int MENU_SEARCH = 1;
private Gallery Gallery01;
private TextView TextView01;
private ArrayList<String> fileName;

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

Gallery01 = (Gallery) findViewById(R.id.Gallery01);
TextView01 = (TextView) findViewById(R.id.TextView01);

//在androidManifest.xml定义SEARCH的intent-filter
Intent queryIntent = getIntent();
final String queryAction = queryIntent.getAction();
//取得当按下搜索时的Intent
if(Intent.ACTION_SEARCH.equals(queryAction)) {
//取得要搜索的字符串
String query = queryIntent.getStringExtra(SearchManager.QUERY);
Toast.makeText(this, query, Toast.LENGTH_LONG);
queryPicture(query);
}
//按下键盘即调用搜索框
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
}

public boolean onCreateOptionsMenu( Menu menu) {
menu.add(0, 0, 0, "Search")
.setIcon(android.R.drawable.ic_search_category_default)
.setAlphabeticShortcut(SearchManager.MENU_KEY);
return super.onCreateOptionsMenu(menu);
}

//菜单选择
public boolean onOptionsItemSelected(MenuItem item){
super.onOptionsItemSelected(item);
switch( item.getItemId() ){
case 0:
//调用搜索框
onSearchRequested();
Toast.makeText(this, "onSearchRequested()", Toast.LENGTH_LONG).show();
break;
}
return true;
}
public boolean onSearchRequested() {
Toast.makeText(DIYSearchManager.this, "startSearch(“mm”, false, null, false)", Toast.LENGTH_LONG).show();
startSearch("mm", false, null, false);
return true;
}
//输入日期到sd card支寻找最后修改时间的图
private void queryPicture(String strQuery){
fileName = new ArrayList<String>();
String[] projection = {
MediaStore.Images.Media.DATA,
MediaStore.Images.Media.DATE_MODIFIED,
MediaStore.Images.Media.DISPLAY_NAME
};

//搜索条件
String selection = "(" + projection[1] + ">=? and" + projection[1] +"<=?)";
//条件的参数
String selectionArgs[] = getStartEnd(strQuery);
//有输入的日期加上日期的开始时间结束时间查询图片
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, selection, selectionArgs, null
);

while( cursor.moveToNext()) {
String filename = cursor.getString(0);
fileName.add(filename);
}
TextView01.setText(strQuery + "共有" + fileName.size() + "张图");
Gallery01.setAdapter(new MyAdapter(this));
}

//用日期取得该日期开始时间跟结束时间
@SuppressLint("UseValueOf")
private String[] getStartEnd(String strQueryDate) {
String[] result = {"",""};
String startDateTime = strQueryDate + "000000000";
String endDateTime = strQueryDate + "235959000";
try{
java.util.Calendar calendar = java.util.Calendar.getInstance();
calendar.set( java.util.Calendar.YEAR, new Integer(startDateTime.substring(0,4)).intValue());
calendar.set( java.util.Calendar.MONTH, new Integer(startDateTime.substring(4,6)).intValue()-1);
calendar.set( java.util.Calendar.DATE, new Integer(startDateTime.substring(6,8)).intValue());
calendar.set( java.util.Calendar.HOUR_OF_DAY, new Integer(startDateTime.substring(8,10)).intValue());
calendar.set( java.util.Calendar.MINUTE, new Integer(startDateTime.substring(10,12)).intValue());
calendar.set( java.util.Calendar.SECOND, new Integer(startDateTime.substring(12,14)).intValue());
//将后面的3位去掉
startDateTime = String.valueOf(calendar.getTimeInMillis()).substring(0,10);

calendar.set( java.util.Calendar.YEAR, new Integer(endDateTime.substring(0,4)).intValue());
calendar.set( java.util.Calendar.MONTH, new Integer(endDateTime.substring(4,6)).intValue()-1);
calendar.set( java.util.Calendar.DATE, new Integer(endDateTime.substring(6,8)).intValue());
calendar.set( java.util.Calendar.HOUR_OF_DAY, new Integer(endDateTime.substring(8,10)).intValue());
calendar.set( java.util.Calendar.MINUTE, new Integer(endDateTime.substring(10,12)).intValue());
calendar.set( java.util.Calendar.SECOND, new Integer(endDateTime.substring(12,14)).intValue());
//将后面的3位去掉
endDateTime = String.valueOf(calendar.getTimeInMillis()).substring(0,10);
}catch(Exception e){
startDateTime="";
endDateTime = "";
}finally{
result[0] = startDateTime;
result[1] = endDateTime;
}
return result;
}

private class MyAdapter extends BaseAdapter{
Context myContext;

public MyAdapter(Context context) { myContext = context; }
public int getCount() { return fileName.size(); }
public Object getItem( int arg0 ){ return arg0; }
public long getItemId( int position ) { return position; }

public View getView( int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView( this.myContext);
InputStream is = null;
try{
is = new java.io.FileInputStream(fileName.get(position));
//将inputStream变成bitmap
Bitmap bm = BitmapFactory.decodeStream(is);
//关闭is
is.close();
//设置Bitmap于imageview中
imageView.setImageBitmap(bm);
}catch(Exception e ){
e.printStackTrace();
}
return imageView;
}
}
}
...全文
179 2 点赞 打赏 收藏 举报
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Crazy_hand 2013-02-02
这里是第二个Activity, 第一个上面只有一个按钮,跳过来的。 我调试过,问题应该出现在onSearchRequested() 函数那里,程序到那里就没反应了。。。
引用 1 楼 jialiry 的回复:
你这个程序中好象没有setContentView(R.layout.abc)之类的布局文件设置;
  • 打赏
  • 举报
回复
jialiry 2013-02-02
你这个程序中好象没有setContentView(R.layout.abc)之类的布局文件设置;
  • 打赏
  • 举报
回复
相关推荐
发帖
Android
加入

7.9w+

社区成员

移动平台 Android
申请成为版主
帖子事件
创建了帖子
2013-02-02 05:17
社区公告
暂无公告