【问题标题】:How to use intent to call the method of next activity in androidandroid中如何使用intent调用下一个activity的方法
【发布时间】:2013-03-04 04:50:01
【问题描述】:

我现在正在做的是--> 我在一个活动中从网络服务获取数据并在列表视图中显示整个数据。我在呈现的列表视图本身上添加了一个搜索功能。

我的要求是 --> 我应该在第一个活动中有一个搜索按钮。当它被点击时,应该在第二个活动中显示相关结果,为此在意图中添加putExtrasintent.putExtra("search", searchBox);,即第二个活动搜索功能关键字。但仍然没有得到搜索到的输出。

@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);
                }

                  }

【问题讨论】:

  • intent.putExtra("search", searchBox);这里的搜索框是什么? n 你想传递给第二个活动什么?你的问题不是很清楚...
  • 只需在第一个活动本身中执行搜索,然后单击搜索按钮,使用意图将搜索结果(而不是搜索框)解析到下一个活动。
  • nw i hd 在第一个活动本身中添加了搜索代码,但在惰性适配器@MehulJoisar 中出现空指针异常
  • searchBox 是editext的id值,我想在第一个活动@vnshetty中执行第二个活动任务

标签: java android search android-intent filter


【解决方案1】:

Intents 用于活动,而不是其中的方法,因此您不能使用它来调用任何特定方法,但是您可以使用附加功能来确定您想要做什么 \

当你从 Intent 获得额外的东西之后,

 if extras has "search" value 
 then do something about it

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    相关资源
    最近更新 更多