首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法解析为type...Adding搜索特性到小部件

无法解析为type...Adding搜索特性到小部件
EN

Stack Overflow用户
提问于 2013-10-16 20:04:49
回答 1查看 488关注 0票数 0

在我的小部件中,我有一个listview,它收集用户在滑动抽屉中安装的所有应用程序。我想添加一个搜索功能,使用户只需在列表中搜索已安装的应用程序就更容易了,因此我遵循教程这里。我已经按照这里的定义创建了我的listview:

代码语言:javascript
复制
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;
}
}

我的列表和搜索栏显示在这里:

代码语言:javascript
复制
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行得到了这个错误(

代码语言:javascript
复制
Drag_and_Drop_App.this.adapter.getFilter().filter(cs);

)

代码语言:javascript
复制
adapter cannot be resolved or is not a field    

不管我做什么,我都想不出如何纠正这个错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-16 20:13:02

往这边走

代码语言:javascript
复制
AppInfoAdapter adapter ;

onCreate(){

---------------

---------------

 adapter  = new AppInfoAdapter(.................

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19412740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档