从列表视图EditText上搜索项目显示错误的结果 [英] Search item in edittext from listview showing wrong result
问题描述
我有一个自定义行的列表视图从adapter.when被填充我点击任一行,它打开一个新的活动,我想。一切工作正常,但当我添加了搜索过滤功能,这个名单,我在其中搜索的EditText的列表项,并得到适当的过滤器,但是当我点击的过滤器产品,它不会打开与之相关的活动。它打开了与其他listItems中的关联,但并不是说我点击one.it这么多的怪异我。我不是在粘贴完整的code这里只安装所需的code等活动。
下面是我的code其中我启用了搜索功能EDITTEXT的列表视图。
edtSearch.addTextChangedListener(新TextWatcher(){
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,
诠释计数){
。adapter.getFilter()过滤器(S);
adapter.notifyDataSetChanged();
}
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
之后INT){
}
公共无效afterTextChanged(编辑S){
}
});
}
如果就同一任何疑问,那么请发表评论,对我来说,任何帮助将是AP preciated。
这是我的完整的code:
包com.tomar.sixxsigma;
进口的java.util.ArrayList;
进口的java.util.HashMap;
进口的java.util.List;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.text.Editable;
进口android.text.TextWatcher;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.ArrayAdapter;
进口android.widget.EditText;
进口android.widget.ListView;
进口android.widget.SimpleAdapter;
进口android.widget.TextView;
进口android.widget.AdapterView.OnItemClickListener;
公共类Tabtwo扩展活动实现OnItemClickListener {
ListView控件的ListView;
TextView的TXT;
ArrayAdapter<字符串>适配器;
//搜索的EditText
的EditText edtSearch;
//字符串存储国名的数组
的String []国家=新的String [] {管理成本,亲和图,
分析,Apprasal成本,评估利益相关者,
};
//的整数点阵列存储在/清晰度图像/绘制-LDPI /
INT []标志=新INT [] {R.drawable.admin,R.drawable.affinity,
R.drawable.analysis,R.drawable.appraisal,R.drawable.assessment,
};
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.tabtwo);
//在列表存储国名中的每一行,货币和标志
名单< HashMap的<字符串,字符串>> ALIST =新的ArrayList< HashMap的<字符串,字符串>>();
的for(int i = 0;我4;;我++){
HashMap的<字符串,字符串> HM =新的HashMap<字符串,字符串>();
hm.put(TXT,国家[I]);
hm.put(标志,Integer.toString(标志[I]));
aList.add(HM);
}
//在哈希映射使用的键
的String []从= {标志,TXT};
// ID的意见listview_layout
INT []到= {R.id.flag,R.id.txt};
//实例化一个适配器来存储每个项目
// R.layout.listview_layout定义每个项目的布局
最后SimpleAdapter适配器=新SimpleAdapter(getBaseContext()
ALIST,R.layout.listview_layout,从,到);
//获取一个引用main.xml中布局文件的列表视图
ListView控件的ListView =(ListView控件)findViewById(R.id.listview);
edtSearch =(EditText上)findViewById(R.id.Search_box);
TXT =(TextView中)findViewById(R.id.txt);
listView.setOnItemClickListener(本);
//适配器设置到ListView
listView.setAdapter(适配器);
listView.setTextFilterEnabled(真正的);
listView.setOnItemClickListener(本);
edtSearch.addTextChangedListener(新TextWatcher(){
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,
诠释计数){
。adapter.getFilter()过滤器(S);
adapter.notifyDataSetChanged();
}
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
之后INT){
}
公共无效afterTextChanged(编辑S){
}
});
}
@覆盖
公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT位置,
长ARG3){
// TODO自动生成方法存根
如果(位置== 0){
意图INT0 =新的意图(getApplicationContext(),Admincost.class);
startActivity(INT0);
}
如果(位置== 1){
意图INT1 =新的意图(getApplicationContext(),Affinity.class);
startActivity(INT1);
}
如果(位置== 2){
意图INT2 =新的意图(getApplicationContext(),Analyse.class);
startActivity(INT2);
}
如果(位置== 3){
意图INT3 =新的意图(getApplicationContext()
ApprasalCosts.class);
startActivity(INT3);
}
如果(位置== 4){
意图INT1 =新的意图(getApplicationContext(),Assessment.class);
startActivity(INT1);
}}
}
}
嘿家伙,我现在已经解决了这个问题,其实我对这个话题面临太大的问题。所以要粘贴解决那些有类似的问题,或将在今后面对的问题。
code,我已经在这里使用:
@覆盖
公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT位置,
长ARG3){
TextView的电视=(TextView中)arg1.findViewById(R.id.txt);
。字符串str = tv.getText()的toString()修剪();
如果(str.equals(国家[0])){
意图INT0 =新的意图(Tabtwo.this,Admincost.class);
startActivity(INT0);
}否则如果(str.equals(国家[1])){
意图INT1 =新的意图(Tabtwo.this,Affinity.class);
startActivity(INT1);
}
}
I have a listview with custom rows being populated from an adapter.when i am clicking of any row, It opening a new Activity that i wants. Everything was working fine but when I added the search filter functionality to this list and I search the listitem in edittext and gets right filter but when i clicked on an filter Item, it doesn't open the activity associated with it. It opening other activities that are associated with other listitems but not that i am clicking one.it is so much weird to me.I am not pasting complete code here only attaching the required code.
Here is my code where i enabled the search functionality to edittext on listview.
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) {
}
});
}
If any query regarding the same then please comment to me,Any help would be appreciated.
Here is my complete code:
package com.tomar.sixxsigma;
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);
} }
}
}
Hey guys now I have Resolved the problem, Actually I was facing much problem on this topic. So want to paste the solution to those that have similar problem or will face in future.
Code that I have use here :
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
TextView tv = (TextView) arg1.findViewById(R.id.txt);
String str = tv.getText().toString().trim();
if (str.equals(countries[0])) {
Intent int0 = new Intent(Tabtwo.this, Admincost.class);
startActivity(int0);
}else if(str.equals(countries[1])) {
Intent int1 = new Intent(Tabtwo.this, Affinity.class);
startActivity(int1);
}
}
这篇关于从列表视图EditText上搜索项目显示错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!