任何人都可以帮我解决如何从listview的searchview中修复错误的结果。例如,我有列表视图中的数据(1,2,3,4,5)当我从searchview搜索数据2时,我点击它,总是数据1显示它,当我没有搜索来自searchview的数据时结果如果为真,我点击数据1显示1,2显示2
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
final String[] values= new String[]{"button1","button2","buttton3"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.activity_list_item,android.R.id.text1,values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
if(position==0){
Intent myintent = new Intent(view.getContext(),Main2Activity.class);
startActivityForResult(myintent,0);
}
if(position==1){
Intent myintent = new Intent(view.getContext(),Main3Activity.class);
startActivityForResult(myintent,1);
}
if(position==2){
Intent myintent = new Intent(view.getContext(),Main2Activity.class);
startActivityForResult(myintent,2);
}
}
});
答案 0 :(得分:0)
startActivityForResult
的第二个参数是用于标识发送到发起活动的结果的代码。将一个值传递给另一个活动的理想方法是通过Intent extras(即作为Intent的一部分发送的额外信息)。因此,而不是startActivityForResult(intent,value_to_be_used)
你应该使用以下内容: -
Intent myintent = new Intent(view.getContext(),Main2Activity.class);
myintent.putExtra("MYKEYFORTHEVALUE",position)
startActivityForResult(myintent,111);
这适用于所有三个。
在Main2Activity
中,您可以使用以下内容来检索值: -
int myvaluefromtheotheractivity = getIntent.getIntExtra("MYKEYFORTHEVALUE",-1);
if (mymyvaluefromtheotheractivity < 0) {
// handle value not being obtained for some reason
}