从列表视图中搜索edittext中的项目显示错误的结果
Posted
技术标签:
【中文标题】从列表视图中搜索edittext中的项目显示错误的结果【英文标题】:Search item in edittext from listview showing wrong result 【发布时间】:2014-01-03 14:21:54 【问题描述】:尝试使用edittext搜索listview项目,点击搜索项目返回错误位置。
假设我从给定列表中搜索“C”,其中列表项如下“A”,“B”,“C”,“D”,“CC”,并得到正确的搜索结果,但一旦点击搜索项目,它返回错误的项目位置。
这里 Edittext addTextChangedListener :
edtSearch.addTextChangedListener(new TextWatcher()
public void onTextChanged(CharSequence s, int start, int before,
int count)
adapter.getFilter().filter(s);
adapter.notifyDataSetChanged();
public void beforeTextChanged(CharSequence s, int start, int count,
int after)
public void afterTextChanged(Editable s)
);
完整的课程代码:
package com.tomar.xyz;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
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.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class Tabtwo extends Activity implements OnItemClickListener
ListView listView;
TextView txt;
ArrayAdapter<String> adapter;
// Search EditText
EditText edtSearch;
// Array of strings storing country names
String[] countries = new String[] "Admin Cost", "Affinity Diagram",
"Analyse", "Apprasal Costs", "Assessment of Stakeholders",
;
// Array of integers points to images stored in /res/drawable-ldpi/
int[] flags = new int[] R.drawable.admin, R.drawable.affinity,
R.drawable.analysis, R.drawable.appraisal, R.drawable.assessment,
;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.tabtwo);
// Each row in the list stores country name, currency and flag
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 4; i++)
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("txt", countries[i]);
hm.put("flag", Integer.toString(flags[i]));
aList.add(hm);
// Keys used in Hashmap
String[] from = "flag", "txt" ;
// Ids of views in listview_layout
int[] to = R.id.flag, R.id.txt ;
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
final SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
aList, R.layout.listview_layout, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = (ListView) findViewById(R.id.listview);
edtSearch = (EditText) findViewById(R.id.Search_box);
txt = (TextView) findViewById(R.id.txt);
listView.setOnItemClickListener(this);
// Setting the adapter to the listView
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(this);
edtSearch.addTextChangedListener(new TextWatcher()
public void onTextChanged(CharSequence s, int start, int before,
int count)
adapter.getFilter().filter(s);
adapter.notifyDataSetChanged();
public void beforeTextChanged(CharSequence s, int start, int count,
int after)
public void afterTextChanged(Editable s)
);
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3)
// TODO Auto-generated method stub
if (position == 0)
Intent int0 = new Intent(getApplicationContext(), Admincost.class);
startActivity(int0);
if (position == 1)
Intent int1 = new Intent(getApplicationContext(), Affinity.class);
startActivity(int1);
if (position == 2)
Intent int2 = new Intent(getApplicationContext(), Analyse.class);
startActivity(int2);
if (position == 3)
Intent int3 = new Intent(getApplicationContext(),
ApprasalCosts.class);
startActivity(int3);
if (position == 4)
Intent int1 = new Intent(getApplicationContext(), Assessment.class);
startActivity(int1);
【问题讨论】:
您必须清除包含数据的数组列表,然后将它们填充到您过滤列表视图的位置。 请看下面我的回答,希望对你有所帮助。 向我们显示项目点击监听器列表 @vipul,我已经添加了完整的代码。 检查此链接 - 我认为有帮助 ***.com/questions/1737009/… 【参考方案1】:参考此代码:-
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
// TODO Auto-generated method stub
ArrayList<String> STOREID_SUB = new ArrayList<String>();
ArrayList<String> STORE_NAME_SUB = new ArrayList<String>();
ArrayList<String> PHOTO_SUB = new ArrayList<String>();
ArrayList<String> PLACE_STATUS_SUB = new ArrayList<String>();
final ArrayList<String> STORE_DESC_SUB = new ArrayList<String>();
textlength = searchText.getText().length();
for (int i = 0; i < STORE_NAME.size(); i++)
if (textlength <= STORE_NAME.get(i).length())
if (searchText.getText().toString().
equalsIgnoreCase((String) STORE_NAME.get(i).subSequence(0, textlength)))
STORE_NAME_SUB.add(STORE_NAME.get(i));
STOREID_SUB.add(STOREID.get(i));
PHOTO_SUB.add(PHOTO.get(i));
STORE_DESC_SUB.add(STORE_DESC.get(i));
PLACE_STATUS_SUB.add(PLACE_STATUS.get(i));
adapter=new StoreSearchListAdapter(context, R.drawable.ic_launcher, R.drawable.ic_launcher, STORE_NAME_SUB, PHOTO_SUB,STORE_DESC_SUB,PLACE_STATUS_SUB);
singlePlaceChooserList.setAdapter(adapter);
我在 onTextCahnged 方法中创建一个新的数组列表,然后使用这些值来设置我的适配器。
【讨论】:
【参考方案2】:将您的 onItemClick 更改为:
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3)
// TODO Auto-generated method
if (((String)adapter.getItem(position)).equals("Admin Cost"))
Intent int0 = new Intent(getApplicationContext(), Admincost.class);
startActivity(int0);
.........................
当您过滤列表时,项目会重新排列并且它们的位置会发生变化,因此请根据该位置上的值而不是位置导航到下一个活动。
【讨论】:
我猜这个字符串会返回所选的国家名称。如果这是真的,这将起作用。如果没有尝试打印该值并找出它返回的内容。 现在我的应用程序崩溃了,在过滤器项目上单击以获取新答案。 感谢您的回答,现在我已经解决了我的问题。请参阅我的答案中的代码。以上是关于从列表视图中搜索edittext中的项目显示错误的结果的主要内容,如果未能解决你的问题,请参考以下文章