无法解析法“getStringArrayList”当我做一个搜索过滤器列表视图 [英] Cannot resolve method 'getStringArrayList' when I'm making a search filter for Listview

查看:211
本文介绍了无法解析法“getStringArrayList”当我做一个搜索过滤器列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是个初学者,我创建一个求职的应用程序,显示就业信息来源的列表视图,其中的数据是从WAMP的服务器数据库。我遇到一个问题:无法解析法getStringArrayList',当我做一个搜索过滤器这个列表视图。请参阅SearchFilter.java的11号线。谁能帮助吗?非常感谢你!

SearchFilter.java

 公共类SearchFilter扩展ListActivity {
私人的EditText filterText = NULL;
ArrayAdapter<字符串>适配器= NULL;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    的setContentView(R.layout.activity_main);

    filterText =(EditText上)findViewById(R.id.search_box);
    filterText.addTextChangedListener(filterTextWatcher);

    setListAdapter(新ArrayAdapter<字符串>(这一点,
            android.R.layout.simple_list_item_1,
            getStringArrayList())); ***<<<<<这条线!***
}

私人TextWatcher filterTextWatcher =新TextWatcher(){

    公共无效afterTextChanged(编辑S){
    }

    公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
                                  之后INT){
    }

    公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,
                              诠释计数){
        。adapter.getFilter()过滤器(S);
    }

};

@覆盖
保护无效的onDestroy(){
    super.onDestroy();
    filterText.removeTextChangedListener(filterTextWatcher);
}

}
 

MainActivity.java

 公共类MainActivity扩展ListActivity {

私人ProgressDialog pDialog;

// URL获得接触JSON
私有静态字符串URL =htt​​p://192.168.0.102/get_json_select_all.php;

// JSON节点名称
私有静态最后弦乐TAG_INFO =信息;
私有静态最后弦乐TAG_POSTNAME =PostName;
私有静态最后弦乐TAG_LOCATION =位置;
私有静态最后弦乐TAG_SALARY =工资;
私有静态最后弦乐TAG_RESPONSIBILITY =责任;
私有静态最后弦乐TAG_COMPANY =公司;
私有静态最后弦乐TAG_CONTACT =联系;

//联系人JSONArray
JSONArray相关信息= NULL;

//哈希映射为的ListView
ArrayList的< HashMap的<字符串,字符串>> infoList;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    infoList =新的ArrayList< HashMap的<字符串,字符串>>();

    最终的ListView LV = getListView();

    //列表视图对项目点击监听器
    lv.setOnItemClickListener(新OnItemClickListener(){

        @覆盖
        公共无效onItemClick(适配器视图<>母公司视图中查看,
                                INT位置,长的id){
            //从选择列表项获得价值
            字符串名称=((TextView中)view.findViewById(R.id.PostName))
                    .getText()的toString()。
            串成本=((TextView中)view.findViewById(R.id.Location))
                    .getText()的toString()。
            字符串描述=((TextView中)view.findViewById(R.id.Salary))
                    .getText()的toString()。

            HashMap的<字符串,字符串>信息=新的HashMap<字符串,字符串>();
            信息=(HashMap的<字符串,字符串>)。lv.getAdapter()的getItem(位置);


            //开始单触点活动
            意图=新的意图(getApplicationContext()
                    SingleContactActivity.class);

            in.putExtra(TAG_POSTNAME,姓名);
            in.putExtra(TAG_LOCATION,成本);
            in.putExtra(TAG_SALARY,说明);
            in.putExtra(TAG_RESPONSIBILITY,info.get(TAG_RESPONSIBILITY));
            in.putExtra(TAG_COMPANY,info.get(TAG_COMPANY));
            in.putExtra(TAG_CONTACT,info.get(TAG_CONTACT));

            startActivity(在);

        }
    });

    //调用异步任务来获得JSON
    新GetContacts()执行();
}

/ **
 *异步任务类,通过HTTP调用获得JSON
 * * /
私有类GetContacts扩展的AsyncTask<虚空,虚空,虚空> {

    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
        //显示进度对话框
        pDialog =新ProgressDialog(MainActivity.this);
        pDialog.setMessage(请稍候...);
        pDialog.setCancelable(假);
        pDialog.show();

    }

    @覆盖
    保护无效doInBackground(虚空......为arg0){
        //创建服务处理程序类实例
        ServiceHandler SH =新ServiceHandler();

        //制作一个请求URL并得到响应
        字符串jsonStr = sh.makeServiceCall(URL,ServiceHandler.GET);

        Log.d(回应:,>中+ jsonStr);

        如果(jsonStr!= NULL){
            尝试 {
                JSONObject的jsonObj =新的JSONObject(jsonStr);

                //获取JSON数组节点
                相关信息= jsonObj.getJSONArray(TAG_INFO);

                //遍历所有联系人
                的for(int i = 0; I< infos.length();我++){
                    JSONObject的C = infos.getJSONObject(我);

                    字符串ID = c.getString(TAG_POSTNAME);
                    字符串名称= c.getString(TAG_LOCATION);
                    字符串email = c.getString(TAG_SALARY);
                    字符串的地址= c.getString(TAG_RESPONSIBILITY);
                    字符串性别= c.getString(TAG_COMPANY);

                    //电话节点JSON对象
                    字符串移动= c.getString(TAG_CONTACT);

                    // TMP的HashMap的单触点
                    HashMap的<字符串,字符串>信息=新的HashMap<字符串,字符串>();

                    //添加每个子节点HashMap中的key =>值
                    info.put(TAG_POSTNAME,ID);
                    info.put(TAG_LOCATION,姓名);
                    info.put(TAG_SALARY,电子邮件);
                    info.put(TAG_RESPONSIBILITY,地址);
                    info.put(TAG_COMPANY,性别);
                    info.put(TAG_CONTACT,手机);
                    //添加联系人到联系人列表
                    infoList.add(信息);
                }
            }赶上(JSONException E){
                e.printStackTrace();
            }
        } 其他 {
            Log.e(ServiceHandler,无法从URL中得到任何数据);
        }

        返回null;
    }

    @覆盖
    保护无效onPostExecute(无效的结果){
        super.onPostExecute(结果);
        //辞退进度对话框
        如果(pDialog.isShowing())
            pDialog.dismiss();
        / **
         *更新解析JSON数据到ListView控件
         * * /
        ListAdapter适配器=新SimpleAdapter(
                MainActivity.this,infoList,

                R.layout.list_item,新的String [] {TAG_POSTNAME,TAG_LOCATION,
                TAG_SALARY},新的INT [] {R.id.PostName,
                R.id.Location,R.id.Salary});

        setListAdapter(适配器);
    }

}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){

    。getMenuInflater()膨胀(R.menu.menu_main,菜单);

    返程(super.onCreateOptionsMenu(菜单));
}

}
 

activity_main.xml

 < EditText上机器人:ID =@ + ID / search_box
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:提示=搜索职位
    机器人:inputType =文本
    机器人:MAXLINES =1/>

    <的ListView
    机器人:ID =@机器人:ID /列表
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT/>
 

解决方案

getStringArrayList 是捆绑的方法(如savedInstanceState)。有没有在活动没有这样的方法。希望有所帮助。

目前你infoList是一个ArrayList>,东西是很难直接传递到活动中。因此,找到一种方法来重新present它作为一个ArrayList,或查找意向的putExtra-方法支持一个更合适的数据类型。在这里,下面是一个使用一个ArrayList建议的解决方案。

传递数据与意图的活动可以让你把它找回来你SearchFilter。在调用活动把这样的事情:

 意向书我=新的意图(这一点,SearchFilter.class);
i.putStringArrayListExtra(com.yourpackagename.here.keynamehere,aStringArrayList);
 

在SearchFilter.java,把这样的事情:

 公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    filterText =(EditText上)findViewById(R.id.search_box);
    filterText.addTextChangedListener(filterTextWatcher);

    意图startingIntent = getIntent();

    ArrayList的<字符串> ArrayList的=新的ArrayList<字符串>();
    如果(startingIntent!= NULL){
       ArrayList的= startingIntent.getStringArrayList(com.yourpackagename.here.keynamehere);
    }
    setListAdapter(新ArrayAdapter<字符串>(这一点,
        android.R.layout.simple_list_item_1,
        ArrayList中));
}
 

I'm a beginner, I'm creating a job search app which shows job infomation as listview where the data is from WAMP server database. I encounter a problem : Cannot resolve method 'getStringArrayList' , when I'm making a search filter for this Listview. Please see line 11 of SearchFilter.java. Could anyone help? thank you very much!

SearchFilter.java

public class SearchFilter extends ListActivity {
private EditText filterText = null;
ArrayAdapter<String> adapter = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    filterText = (EditText) findViewById(R.id.search_box);
    filterText.addTextChangedListener(filterTextWatcher);

    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,
            getStringArrayList()));                   ***<<<<< this line !***
}

private TextWatcher filterTextWatcher = new TextWatcher() {

    public void afterTextChanged(Editable s) {
    }

    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before,
                              int count) {
        adapter.getFilter().filter(s);
    }

};

@Override
protected void onDestroy() {
    super.onDestroy();
    filterText.removeTextChangedListener(filterTextWatcher);
}

}

MainActivity.java

public class MainActivity extends ListActivity {

private ProgressDialog pDialog;

// URL to get contacts JSON
private static String url = "http://192.168.0.102/get_json_select_all.php";

// JSON Node names
private static final String TAG_INFO = "info";
private static final String TAG_POSTNAME = "PostName";
private static final String TAG_LOCATION = "Location";
private static final String TAG_SALARY = "Salary";
private static final String TAG_RESPONSIBILITY = "Responsibility";
private static final String TAG_COMPANY = "Company";
private static final String TAG_CONTACT = "Contact";

// contacts JSONArray
JSONArray infos = null;

// Hashmap for ListView
ArrayList<HashMap<String, String>> infoList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    infoList = new ArrayList<HashMap<String, String>>();

    final ListView lv = getListView();

    // Listview on item click listener
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.PostName))
                    .getText().toString();
            String cost = ((TextView) view.findViewById(R.id.Location))
                    .getText().toString();
            String description = ((TextView) view.findViewById(R.id.Salary))
                    .getText().toString();

            HashMap<String, String> info = new HashMap<String, String>();
            info=(HashMap<String, String>)lv.getAdapter().getItem(position);


            // Starting single contact activity
            Intent in = new Intent(getApplicationContext(),
                    SingleContactActivity.class);

            in.putExtra(TAG_POSTNAME, name);
            in.putExtra(TAG_LOCATION, cost);
            in.putExtra(TAG_SALARY, description);
            in.putExtra(TAG_RESPONSIBILITY,  info.get(TAG_RESPONSIBILITY));
            in.putExtra(TAG_COMPANY, info.get(TAG_COMPANY));
            in.putExtra(TAG_CONTACT, info.get(TAG_CONTACT));

            startActivity(in);

        }
    });

    // Calling async task to get json
    new GetContacts().execute();
}

/**
 * Async task class to get json by making HTTP call
 * */
private class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                infos = jsonObj.getJSONArray(TAG_INFO);

                // looping through All Contacts
                for (int i = 0; i < infos.length(); i++) {
                    JSONObject c = infos.getJSONObject(i);

                    String id = c.getString(TAG_POSTNAME);
                    String name = c.getString(TAG_LOCATION);
                    String email = c.getString(TAG_SALARY);
                    String address = c.getString(TAG_RESPONSIBILITY);
                    String gender = c.getString(TAG_COMPANY);

                    // Phone node is JSON Object
                    String mobile = c.getString(TAG_CONTACT);

                    // tmp hashmap for single contact
                    HashMap<String, String> info = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    info.put(TAG_POSTNAME, id);
                    info.put(TAG_LOCATION, name);
                    info.put(TAG_SALARY, email);
                    info.put(TAG_RESPONSIBILITY, address);
                    info.put(TAG_COMPANY, gender);
                    info.put(TAG_CONTACT, mobile);
                    // adding contact to contact list
                    infoList.add(info);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(
                MainActivity.this, infoList,

                R.layout.list_item, new String[] { TAG_POSTNAME, TAG_LOCATION,
                TAG_SALARY }, new int[] { R.id.PostName,
                R.id.Location, R.id.Salary });

        setListAdapter(adapter);
    }

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_main, menu);

    return (super.onCreateOptionsMenu(menu));
}

}

activity_main.xml

    <EditText android:id="@+id/search_box"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Search Jobs"
    android:inputType="text"
    android:maxLines="1"/>

    <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

解决方案

getStringArrayList is a method on Bundle (such as savedInstanceState). There's no such method in Activity. Hope that helps.

Currently your infoList is an ArrayList>, something that is harder to pass directly to an activity. So find a way to represent it as an ArrayList, or find a more suitable datatype supported by Intent's putExtra-methods. Here below is a suggested solution using an ArrayList.

Passing the data into the activity with the Intent allows you to get it back in your SearchFilter. In the calling activity put something like this:

Intent i = new Intent(this, SearchFilter.class);
i.putStringArrayListExtra("com.yourpackagename.here.keynamehere", aStringArrayList);

In SearchFilter.java, put something like this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    filterText = (EditText) findViewById(R.id.search_box);
    filterText.addTextChangedListener(filterTextWatcher);

    Intent startingIntent = getIntent();

    ArrayList<String> arrayList = new ArrayList<String>();
    if (startingIntent != null) {
       arrayList = startingIntent.getStringArrayList("com.yourpackagename.here.keynamehere");
    }
    setListAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1,
        arrayList));
}      

这篇关于无法解析法“getStringArrayList”当我做一个搜索过滤器列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆