搜索过滤器,带有可点击的列表视图

时间:2015-12-13 22:01:37

标签: android-intent

我正在创建一个带有搜索过滤器和可点击列表视图的应用程序。搜索过滤器正在运行,但不是可点击的列表视图。

public class MainActivity extends Activity {

    private ListView lv;

     ArrayAdapter<String> adapter;

     EditText inputSearch;

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


        String products[] ={"1","1A","1B","1C","1D","2","2A","3","3A","3B","3C","3D","4A","4","5","5A","6","6A","6B","7","8","8B","9","10","10A","11","12","12A","13","13A","13D","14","14A","14B","15","15A","15C","15D","15F","16","16A","16D","17","17A","17B","17C","17D","17E","17F","18","19","20","20A","20B","20C","20D"};


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

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


        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub
                MainActivity.this.adapter.getFilter().filter(s); 
            }});




        //the code for clicking on the list view

        public void onItemClick(AdapterView<?> parent, View view,
                          int position, long id) {
                    // TODO Auto-generated method stub

                String routes =((TextView)view).getText().toString();

                 Intent i = new Intent(getApplicationContext(), SingleListItem.class);
                  // sending data to new activity

                  i.putExtra("routes", routes);
                  startActivity(i);
                }});
            }

这是singlelistitem类

public class SingleListItem extends Activity{

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.single_list_item_view);

    TextView txtProduct = (TextView) findViewById(R.id.routes_lab);

    Intent i = getIntent();
    // getting attached intent data
    String product = i.getStringExtra("routes");

    txtProduct.setText(product);

1 个答案:

答案 0 :(得分:0)

根据您的代码,我提供以下解决方案。

注意:在代码中进行一些修改。

Step 1:lv.setOnItemClickListener(this);

// lv = (ListView) findViewById(R.id.list_view); add step 1 below of this in your code


Step 2:public class MainActivity extends Activity implements OnItemClickListener

第3步:请让我知道它是否有效,以便我可以帮助你