我的应用程序正在搜索字符串,如果我点击打开活动,
但是我的问题是,如果我写第二个,点击后显示第一个活动,我想打开第二个活动
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editsearch = (EditText)findViewById(R.id.editText1);
listView = (ListView)findViewById(R.id.listView1);
mItems = new ArrayList<String>();
mItems.add(new String(getResources().getString(R.string.First)));
mItems.add(new String(getResources().getString(R.string.Second)));
mItems.add(new String(getResources().getString(R.string.Third)));
mItems.add(new String(getResources().getString(R.string.D)));
mItems.add(new String(getResources().getString(R.string.E)));
listView.setAdapter(new CustomeArrayAdapter(MainActivity.this, mItems));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0)
{
Intent myIntent = new Intent(MainActivity.this, Test.class);
MainActivity.this.startActivity(myIntent);
}
if(position == 1)
{
Intent myIntent = new Intent(MainActivity.this, Test2.class);
MainActivity.this.startActivity(myIntent);
}
}
});
editsearch.addTextChangedListener(new TextWatcher() {
//Event when changed word on EditTex
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
ArrayList<String> temp = new ArrayList<String>();
int textlength = editsearch.getText().length();
temp.clear();
for (int i = 0; i < mItems.size(); i++)
{
if (textlength <= mItems.get(i).length())
{
if(editsearch.getText().toString().equalsIgnoreCase(
(String)
mItems.get(i).subSequence(0,
textlength)))
{
temp.add(mItems.get(i));
}
}
}
listView.setAdapter(new CustomeArrayAdapter(MainActivity.this, temp));
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
我只想要字符串搜索和打开活动
这是因为您的条件是:if(position == 0)
…所以当您过滤列表时,第二个活动项将成为位置0…
让你的条件:
if((String) getListAdapter().getItem(position).equals(new String(getResources().getString(R.string.First)))
对第二个条件也执行相同的操作