Android搜索未开启活动

时间:2013-12-03 16:15:14

标签: android listview search

我在我的应用程序中实现了搜索功能,但每当我从搜索结果中选择一个项目时,它都不会启动已挑选的活动。根据代码,如果我点击第二个项目,它应该触发活动,

这是我的代码

package com.Example.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

private ListView list1;

private String array[] = { "Iphone", "Tutorials", "Gallery", "Android",

"item 1", "item 2", "item3", "item 4", "item 1", "item 2", "item3", "item 4","item 1",       "item 2", "item3", "item 4"};

EditText inputSearch; 
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputSearch = (EditText) findViewById(R.id.inputSearch); // initialize edittext 
list1 = (ListView) findViewById(R.id.ListView01);
adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array);
list1.setAdapter(adapter);
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                          
}
});
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
 if (position == 1)
{
  Intent myIntent = new Intent(getApplicationContext(),Next.class);
  startActivity(myIntent);
 }
}
});
}
}

2 个答案:

答案 0 :(得分:0)

您的代码仅适用于第一个位置的项目,因为您已在listview项目中单击了侦听器: -

 if (position == 1)
{
  Intent myIntent = new Intent(getApplicationContext(),Next.class);
  startActivity(myIntent);
 }

仅在点击第1个位置的项目时,此if条件将成立,并且将调用“Next”活动

答案 1 :(得分:0)

将onItemClick更改为

public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
   Intent myIntent = new Intent(MainActivity.this,Next.class);
   startActivity(myIntent);
   // will launch activity every time an item in listview is clicked 
}
});

你有这个

if (position == 1) // only if position is 1 
{
 Intent myIntent = new Intent(getApplicationContext(),Next.class);
 startActivity(myIntent);
}

使用活动上下文而不是getApplicationContext作为MainActivity.this

编辑:

  public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
   if(position==0)
   {
   Intent myIntent = new Intent(MainActivity.this,Next.class);
   startActivity(myIntent);
   } 
   if(position==1)
   {
   Intent myIntent = new Intent(MainActivity.this,SecondActivityName.class);
   startActivity(myIntent);
   } 
   ...// similar position 2 and 3 and so on
   }
   });