如何启动包含有关列表项信息的新活动?

时间:2013-11-22 12:15:58

标签: android search android-listview

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

这是主要活动文件:

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

3 个答案:

答案 0 :(得分:0)

要使用其他信息启动新活动,请使用附加功能。例如,这是您启动新活动的方式:

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附近,但你明白了。

答案 1 :(得分:0)

您必须实施以下

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将帮助您了解有关意图的更多信息:)

答案 2 :(得分:0)

当我们将位置(arg2)与Filter一起使用时,这是一个问题。

<强>例如
问题是:

  • 过滤前,
    的ArrayList - &GT; [AA,BB,AC]
    列表 - &GT;
    AA
    BB
    AC

  • 过滤后,
    的ArrayList - &GT; [AA,BB,AC]
    列表 - &GT;
    AA
    AC

因此,当使用arg2位置)时,过滤后,如果点击“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); 

           }
    });
相关问题