【问题标题】:How to launch new activity that contains info about list item?如何启动包含列表项信息的新活动?
【发布时间】:2013-12-07 07:46:54
【问题描述】:

我创建了一个搜索功能和一个列表。在选择单个列表项时,我想启动包含有关该列表项信息的新活动。我搜索了解决方案,但没有发现任何对我完全初学者有用的东西。

这是一个主活动文件:

public class MainActivity extends Activity {

    // List view
    private ListView lv;

    // Listview Adapter
    ArrayAdapter<String> adapter;

    // Search EditText
    EditText inputSearch;


    // ArrayList for Listview
    ArrayList<HashMap<String, String>> productList;

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

        // Listview Data
        String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
                                "iPhone 4S", "Samsung Galaxy Note 800",
                                "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};

        lv = (ListView) findViewById(R.id.list_view);
        inputSearch = (EditText) findViewById(R.id.inputSearch);

        // Adding items to listview
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
        lv.setAdapter(adapter);

        /**
         * Enabling Search Filter
         * */
        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                MainActivity.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                         
            }
        });
    }   
}

【问题讨论】:

    标签: android search android-listview


    【解决方案1】:

    要启动一项新活动,并提供其他信息,请使用额外信息。例如,这是您启动新 Activity 的方式:

    Intent intent=new Intent(this, MyActivityToLaunch.class);
    intent.putExtras("KEY_FOR_THE_VALUE", "value that is added as extra");
    startActivity(intent);
    

    这样,当新 Activity 启动时,您可以检索添加的额外内容。例如,你会这样得到它:

    @Override
    protected void onCreate(Bundle bundle)
    {
        /*initialize here, like you would normally*/
    
        Bundle extras=getIntent.getExtras(); //get the extras here
        String myValue;
        if(extras!=null) //just to be sure, check if the extras exist
        {
            myValue=extras.getStringExtra("KEY_FOR_THE_VALUE"); //notice that the key value is the same as when you putted in the extra
        }
        else myValue="Error: getExtras() returned null";
    }
    

    请原谅我,如果有任何语法错误,因为我不在任何 IDE 附近,但你明白了。

    【讨论】:

      【解决方案2】:

      您必须执行以下操作

      lv.setOnItemClickListener(new OnItemClickListener() {
      
              @Override
              public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                      long arg3) {
                             Intent intent=new Intent(MainActivity.this, MyActivityToLaunch.class);
                             String item = lv.getAdapter().getItem(arg2).toString();
                             intent.putExtras("KEY", item);
                              startActivity(intent);        
              }
          };
      

      使用相同的键“KEY”,您在启动的活动中检索项目

      link 将帮助您了解有关意图的更多信息:)

      【讨论】:

        【解决方案3】:

        当我们将位置 (arg2) 与 Filter 一起使用时会出现问题。

        例如:
        问题是:

        • 过滤前,
          ArrayList-->[AA,BB,AC]
          列表-->
          AA
          BB
          交流

        • 过滤后,
          ArrayList-->[AA,BB,AC]
          列表-->
          AA
          交流

        所以,当使用arg2(position)时,过滤后,如果点击'AC',arraylist.get(arg2)会返回'BB'。

        解决方案:

        请改用arg1(查看)。就是你点击的当前单行项View。

        参考以下代码片段:

        lv.setOnItemClickListener(new OnItemClickListener() {
        
               @Override
               public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                                    long arg3) {
                     TextView txt=(TextView) arg1.findViewById(R.id.product_name);
                     String product=txt.getText().toString();
                     Toast.makeText(MainActivity.this, product, Toast.LENGTH_LONG).show();
                     Intent intent=new Intent(MainActivity.this, SecondActivity.class);
                     intent.putExtra("KEY", product);
                     startActivity(intent); 
        
                   }
            });
        

        【讨论】:

          猜你喜欢
          • 2017-06-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-01
          • 2020-01-06
          • 1970-01-01
          • 1970-01-01
          • 2014-02-10
          相关资源
          最近更新 更多