Android 无法解析为键入…将搜索功能添加到小部件
在我的小部件中,我有一个listview,它在一个滑动抽屉中收集用户安装的所有应用程序。我想添加一个搜索功能,使用户可以更轻松地在列表中搜索已安装的应用程序,因此我将继续学习本教程。我已经按照此处的定义创建了我的listview:Android 无法解析为键入…将搜索功能添加到小部件,android,Android,在我的小部件中,我有一个listview,它在一个滑动抽屉中收集用户安装的所有应用程序。我想添加一个搜索功能,使用户可以更轻松地在列表中搜索已安装的应用程序,因此我将继续学习本教程。我已经按照此处的定义创建了我的listview: package com.example.awesomefilebuilderwidget; import java.util.List; import android.content.ActivityNotFoundException; import androi
package com.example.awesomefilebuilderwidget;
import java.util.List;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.widget.Toast;
public class Utilities {
/*
* Get all installed application on mobile and return a list
* @param c Context of application
* @return list of installed applications
*/
public static List getInstalledApplication(Context c) {
return c.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
}
/*
* Launch an application
* @param c Context of application
* @param pm the related package manager of the context
* @param pkgName Name of the package to run
*/
public static boolean launchApp(Context c, PackageManager pm, String pkgName) {
// query the intent for lauching
Intent intent = pm.getLaunchIntentForPackage(pkgName);
// if intent is available
if(intent != null) {
try {
// launch application
c.startActivity(intent);
// if succeed
return true;
// if fail
} catch(ActivityNotFoundException ex) {
// quick message notification
Toast toast = Toast.makeText(c, "Application Not Found", Toast.LENGTH_LONG);
// display message
toast.show();
}
}
// by default, fail to launch
return false;
}
}
我的列表视图和搜索栏显示在这里:
package com.example.awesomefilebuilderwidget;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class Drag_and_Drop_App extends Activity {
private ListView mListAppInfo;
// Search EditText
EditText inputSearch;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set layout for the main screen
setContentView(R.layout.drag_and_drop_app);
// import buttons
Button btnLinkToFeedback = (Button) findViewById(R.id.btnLinkToFeedback);
// Link to Feedback Screen
btnLinkToFeedback.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
Feedback.class);
startActivity(i);
finish();
}
});
// create new adapter
AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager());
// load list application
mListAppInfo = (ListView)findViewById(R.id.lvApps);
// set adapter to list view
mListAppInfo.setAdapter(adapter);
// search bar
inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
// Drag_and_Drop_App.this.adapter.getFilter().filter(cs);
Drag_and_Drop_App.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
// implement event when an item on list view is selected
mListAppInfo.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int pos, long id) {
// get the list adapter
AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
// get selected item on the list
ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
// launch the selected application
Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
}
});
// implement event when an item on list view is selected via long-click for drag and drop
mListAppInfo.setOnItemLongClickListener(new OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
// get the list adapter
AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
// get selected item on the list
ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
// launch the selected application
Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
return true;
}
});
}
}
我的应用程序运行正常,只是搜索功能不起作用。我在第58行发现这个错误(
)
无论我做什么,我都不知道如何修复这个错误 就这样吧
AppInfoAdapter adapter ;
onCreate(){
---------------
---------------
adapter = new AppInfoAdapter(.................
}
“适配器”的范围仅限于onCreate方法。您可以尝试在类外部声明它。因为
adapter
在onCreate
中,而不是在class字段中,所以您只需使用adapter.getFilter().filter(cs)
或者您可以在onCreate方法中将其声明为final我如何扩展限制以达到我的OnTextChanged方法?我试图复制我已经定义的代码,结果却出现了错误。然后,我只是尝试了一个简单的“AppInfoAdapter”;但那没有起作用。如果我只是使用“adapter.getFilter().filter(cs)”,“我在“.getFilter”上得到了一个错误,说它是未定义的。如果我误解了,很抱歉,这会消除适配器上的错误,但是现在我在“.getFilter()上得到了一个新的错误。”说AppInfoAdapterI类型没有定义,我开始了一个新问题
adapter cannot be resolved or is not a field
AppInfoAdapter adapter ;
onCreate(){
---------------
---------------
adapter = new AppInfoAdapter(.................
}