【问题标题】:Search not done unless complete line typed in edit text除非在编辑文本中输入完整的行,否则搜索不会完成
【发布时间】:2016-02-24 07:05:25
【问题描述】:

我有一个新的搜索活动,它有一个用于输入搜索查询的编辑文本和一个用于搜索在EditText 中输入的查询的搜索按钮

搜索到的项目在搜索活动中显示在ListView

现在的问题是我有一个ListView,用户可以从那里搜索所需的内容

让我们假设这个列表视图有 100 个列表项,其中所有命名为 :

  1. 测试标题 1,
  2. 测试标题 2,
  3. 测试标题 3,
  4. 测试标题 4,

    以此类推,直到测试标题 100

现在在editext的搜索活动中,如果我输入“test”并按下搜索按钮,它应该会显示所有列表,因为每个列表项标题都在其标题中包含测试,但我什么也没得到

我必须输入整个标题才能完成搜索,如“test heading 1”,匹配的标题显示在列表中

如果我在edittext中输入“test”并按下搜索按钮,我怎样才能使搜索更好,它应该在其标题中显示所有包含测试词的项目

现在我正在使用 parse.com 将数据检索到 listview 中(悲伤 thar parse.com 正在关闭),并且我正在将我的数据库迁移到 m6 自己的服务器

我尝试过的代码

搜索活动

public class SearchActivity extends Activity
 {

protected  EditText searchedittext;
 Button searchButton;
List<ParseObject> ob;

@Override
public void onCreate(Bundle savedInstanceState )
{
    // TODO: Implement this method
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_layout);

    searchedittext = (EditText) findViewById(R.id.search_layoutEditText);

    final ListView searchedlist = (ListView) findViewById(R.id.searchlist);
    searchButton = (Button) findViewById(R.id.searchlayoutbtn);

    searchButton.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            String seaechedit = searchedittext.getText().toString();

            if(seaechedit.isEmpty()){

                AlertDialog.Builder builder = new AlertDialog.Builder(SearchActivity.this);
                builder.setMessage("PLEASE ENTER SOME SEARCH QUERY")
                       .setTitle("EMPTY SEARCH") 
                       .setPositiveButton(android.R.string.ok, null);

                AlertDialog dialog = builder.create();
                dialog.show();

            }
            else{
                setProgressBarIndeterminateVisibility(true);
                // InterActivity is the class name in parse database where listview retrives it data from
                ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                    "InterActivity");

                query.whereEqualTo("listheading", seaechedit);
                query.orderByAscending("_created_at");
                query.setLimit(200);

                query.findInBackground(new FindCallback<ParseObject>() {

                        @Override
                        public void done(List<ParseObject> p1, ParseException e)
                        {
                            setProgressBarIndeterminateVisibility(false);

                            if(e == null){

                                ob = p1;

                                String [] searchHeadings = new String[ob.size()];

                                int i = 0;

                                     // listheading is the coloumn name in parse database
                                      for(ParseObject heading : ob){ searchHeadings[i] = (String) heading.get("listheading");
                                i++;

                                }

                                ArrayAdapter<String> adapter = new ArrayAdapter<String>( SearchActivity.this, android.R.layout.simple_list_item_1, searchHeadings );
                                searchedlist.setAdapter(adapter);


                            }else{

                                Log.e("searchactivity", e.getMessage()); 
                                AlertDialog.Builder builder = new AlertDialog.Builder(SearchActivity.this); 
                                builder.setMessage(e.getMessage()) 
                                       .setTitle("Nothing found")
                                       .setPositiveButton(android.R.string.ok, null); 
                                AlertDialog dialog = builder.create();
                                dialog.show();
                            }
                        }
                    });
            }


        }

    });

}



 }

第一张图片中列表的活动的aynctask

private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();


        dialog = new SpotsDialog(InterActivity.this, R.style.Custom);

        dialog.show();
    }


    @Override
    protected Void doInBackground(Void... params) {
        // Create the array
        codelist = new ArrayList<CodeList>();
        try {
            // Locate the class table named "Country" in Parse.com
            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                "InterActivity");
            // Locate the column named "ranknum" in Parse.com and order list
            // by ascending
            query.orderByAscending("_created_at");
            query.setLimit(limit);
            ob = query.find();
            for (ParseObject inter : ob) {




                map.setDescription((String) inter.get("subheading"));
                map.setIntroduction((String) inter.get("intro"));


                codelist.add(map);
            }
        } catch (ParseException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // Locate the listview in listview_main.xml


        listview = (SwipeMenuListView) findViewById(R.id.inter_layoutListView);
        // Pass the results into ListViewAdapter.java
        adapter = new FinalAdapter(InterActivity.this,
                                      codelist);


        // Binds the Adapter to the ListView
    listview.setAdapter(adapter);


        listview.setOnItemClickListener(InterActivity.this);


        dialog.dismiss();





}


@Override
public void onItemClick(AdapterView<?> p1, View view, int position, long p4)
{

    ObjectMapper mapper = new ObjectMapper();
    CodeList codes = (CodeList) adapter.getItem(position);


    try{

        Intent intent = new Intent(InterActivity.this, SingleItemView.class);

        String jsonString = mapper.writeValueAsString(codes);


        intent.putExtra("selected item", jsonString);





        intent.putExtra("subheading",
                        (codelist.get(position).getDescription()));



        intent.putExtra("intro",
                        (codelist.get(position).getIntroduction()));



        // Start SingleItemView Class
        //   startActivity(intent);
        startActivityForResult(intent, 1);

    }catch(JsonProcessingException e){
        //something went w3ong
    }
}

【问题讨论】:

    标签: android listview search parse-platform


    【解决方案1】:

    您正在使用whereEqualTo() 它要求特定键的值等于提供的值。

    query.whereEqualTo("listheading", seaechedit);
    

    将其替换为 它会查找包含提供的字符串的字符串值。

    query.whereContains("listheading", seaechedit);
    

    whereContains Function

    【讨论】:

    • 我试试看告诉你,thnx
    • 非常感谢,不知道这个
    • 欢迎@user5894647 快乐编码
    • 兄弟我遇到了一个新问题,我如何设置项 clicklitener 以进行搜索活动,就像我在主列表活动中所做的那样,如第一张图片所示打开相应的单项视图,我在我的问题中发布了我的第一张图片的 ac5ivity ,请看看并帮助我
    • 我什至尝试为 searchactivity 实现 onitemclicklistener 并从主列表活动中粘贴 obnitemclick 方法,但项目单击未打开单项类
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    相关资源
    最近更新 更多