0

我有 2 个活动,在第一个活动中有一个“关键字”和“搜索”按钮,根据给定的关键字,相关项目应该以列表视图形式显示在第二个活动中,但只是为了测试我将搜索栏放在顶部列表视图并为其编码,这工作正常,现在问题我想在“搜索”按钮的 onclick 内的第二个活动中移动“搜索功能代码”。

这是我的代码

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


    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

    StrictMode.setThreadPolicy(policy); 

    final EditText searchBox=(EditText) findViewById(R.id.search);
    final ListView  list=(ListView)findViewById(android.R.id.list);

    //get the LayoutInflater for inflating the customomView
     //this will be used in the custom adapter
     inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);


  final  ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
        // Creating JSON Parser instance
                final JSONParser jParser = new JSONParser();

                // getting JSON string from URL
                JSONObject json = jParser.getJSONFromUrl(URL);
                try {
                     posts = json.getJSONArray(KEY_POSTS);

        // looping through all song nodes <song>
                for(int i = 0; i < posts.length(); i++){
                    JSONObject c = posts.getJSONObject(i);
                    // Storing each json item in variable
                    String id = c.getString(KEY_ID);
                    String title = c.getString(KEY_TITLE);
                    String date = c.getString(KEY_DATE);
                    String content = c.getString(KEY_CONTENT);
                    // to remove all <P> </p> and <br /> and replace with ""
                     content = content.replace("<br />", "");
                     content = content.replace("<p>", "");
                     content = content.replace("</p>", "");

                    //authornumber is agin  JSON Object
                    JSONObject author = c.getJSONObject(KEY_AUTHOR);
                    String name = author.getString(KEY_NAME);

                    String url = null;
                    String slug = null;
                    try {
                    JSONArray atta = c.getJSONArray("attachments");
                    for(int j = 0; j < atta.length(); j++){
                        JSONObject d = atta.getJSONObject(j);

                        slug = d.getString(KEY_SLUG);

                        JSONObject images = d.getJSONObject(KEY_IMAGES);

                        JSONObject thumbnail = images.getJSONObject(KEY_THUMB_URL);
                        url = thumbnail.getString(KEY_URL);

                    }
                    } catch (Exception e) {
                        e.printStackTrace();

                    }

            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value
            map.put(KEY_ID, id);
            map.put(KEY_TITLE, title);
            map.put(KEY_DATE, date);
            map.put(KEY_NAME, name);
            map.put(KEY_CONTENT, content);
            map.put(KEY_SLUG, slug);
            map.put(KEY_URL, url);
  //                System.out.println("the map is title "+map.get("title"));
         //title2.add(map.get("title"));
            // adding HashList to ArrayList
            songsList.add(map);
                }   

                }catch (JSONException e) {
                    e.printStackTrace();

                    }


              //searchResults=OriginalValues initially
                searchResults=new ArrayList<HashMap<String, String>>(songsList);

         // Getting adapter by passing json data ArrayList
            adapter=new LazyAdapter(this, songsList);   

             list.setAdapter(adapter);


             searchBox.addTextChangedListener(new TextWatcher() {

                 public void onTextChanged(CharSequence s, int start, int before, int count) {
                   //get the text in the EditText
                    searchString=searchBox.getText().toString();
                    textLength=searchString.length();
                   searchResults.clear();

                   for(int i=0;i<songsList.size();i++)
                   {
                   playerName=songsList.get(i).get("title").toString();
                  System.out.println("player name "+playerName);
                  if(textLength<=playerName.length()){
                  //compare the String in EditText with Names in the    ArrayList
                         if(searchString.equalsIgnoreCase(playerName.substring(0,textLength))){
                    searchResults.add(songsList.get(i));
                    System.out.println("the array list is "+songsList.get(i));
                    adapter=new LazyAdapter(Home.this, searchResults);   

                     list.setAdapter(adapter);
                    }

                      }
4

1 回答 1

1

让我们这样说吧。您现在正在做的是-> 您正在从webservice一个活动中获取数据并在listview. 您只在呈现的列表视图本身上添加了一个搜索功能。

您的要求是 --> 您应该在first activity. 单击它时,您应该在 中显示相关结果second activity。为此,您只需借助 .将用户输入的关键字传递给下一个活动putExtras() through intents。然后,您根据搜索关键字进行调用,webservice而不是带来通用结果。如果这不是您想要的,只需编辑您的问题并进行更多说明,以便有机会编辑我的答案。

于 2013-03-02T12:09:50.443 回答