我的代码有问题..我试图添加一个搜索功能到我的列表视图在Android ..它是以某种方式工作,但是当我搜索它给了我其他活动不是真正的结果..喜欢在我的列表视图,如果我搜索销售即5日活动它显示的结果,但是当我点击打开活动打开它撒尼活性,即列表中的第一个活动...Android的Listview搜索功能
下面是活动的Java代码
package com.example.bati;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
public class MainActivity extends Activity {
ListView lv;
SearchView sv;
String[] teams = {"Sani", "Bali", "Sati", "Umam", "sale"};
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, teams);
lv.setAdapter(adapter);
sv.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Intent myIntent = new Intent(view.getContext(), Sani.class);
startActivityForResult(myIntent, 0);
}
if (position == 1) {
Intent myIntent = new Intent(view.getContext(), Bali.class);
startActivityForResult(myIntent, 1);
}
if (position == 2) {
Intent myIntent = new Intent(view.getContext(), Sati.class);
startActivityForResult(myIntent, 2);
}
}
});
}
}
谢谢兄弟,它的工作原理 – Elpedia
不客气如果你接受和upvote我的答案会很高兴。谢谢。 – savepopulation