0

我将 setOnItemClickListener 添加到此代码并具有搜索过滤器,当我单击列表项而不是打开新活动时,我会强制关闭!一切正常,但没有打开 listitem 活动!我的代码哪里错了?

我的代码:

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

 //-----------------------------------------------------------------------------------

    // listening to single list item on click
    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {

          // selected item 
          String product = ((TextView) view).getText().toString();

          // Launching new Activity on selecting single List Item
          Intent i = new Intent(getApplicationContext(), SingleListItem.class);
          // sending data to new activity
          i.putExtra("product", product);
          startActivity(i);

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

        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            MainActivity.this.adapter.getFilter().filter(cs);   
        }

        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }

        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub                          
        }
    });
    }

 }
4

2 回答 2

1

没有日志很难说,但有两件事是可能的:1)您的视图不是 TextView,因此您得到 ClassCastException 2)活动 SingleListItem 未在清单中声明

于 2012-09-15T06:35:29.103 回答
1

问题似乎在这里String product = ((TextView) view).getText().toString();

通过获取值position

IEString product = products[position];

于 2012-09-15T06:18:30.963 回答