我对List.i的过滤数据没有什么问题,想要从List中过滤数据。我的问题是wech我开始在edittext上写文本而不是listview只显示List的第一个位置。
这是我的代码。
MainClass
public class ItemType extends AppCompatActivity {
ListView item;
Context context=this;
List<String> allItemNames = new ArrayList<String>();
Item_Type_list_adapter adapter;
EditText search;
ArrayAdapter<String> adapter1;
String[] data = {"mehul joisar","amit mishra","amitabh","Aamir khan","jesica","katrina"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_type);
search=(EditText)findViewById(R.id.Search);
item=(ListView)findViewById(R.id.item_type_listview);
allItemNames = new ArrayList<String>();
allItemNames.add("Coil");
allItemNames.add("Plate");
allItemNames.add("Sheet");
allItemNames.add("Extra");
adapter=new Item_Type_list_adapter(this,allItemNames);
item.setAdapter(adapter);
item.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String data = (String) adapterView.getItemAtPosition(i);
Log.d("Positions", data);
}
});
search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence cs, int i, int i1, int i2) {
adapter.getFilter().filter(search.getText().toString());
adapter.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_item_type, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
}
ArrayAdapter类
public class Item_Type_list_adapter extends ArrayAdapter<String> implements Filterable {
Activity context;
private final List<String> allNames;
public Item_Type_list_adapter(Activity context,List<String> allNames) {
super(context, R.layout.item_type_layout,allNames);
this.context=context;
this.allNames=allNames;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.item_type_layout, null, true);
TextView ItemName = (TextView) rowView.findViewById(R.id.ItemName);
ItemName.setText(allNames.get(position));
return rowView;
};
}
我在List
中添加4值1.Coil 2.Plate 3.sheet 4.extra
当我开始过滤时,它只显示Coil.But当我在listview上触摸它的Log完美值但只是显示错误值。
帮我解决这个问题。
我的代码中是否有遗漏?
提前致谢
答案 0 :(得分:1)
尝试使用ListView
来过滤EditText
的此代码
的 activity_main.xml中强>
<EditText
android:id="@+id/etText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10">
<requestFocus />
</EditText>
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@android:color/transparent"
android:isScrollContainer="false" />
一个名为 item.xml 的新TextView布局,用于显示项目
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TextView>
MainActivity 中的 onCreate()
方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Array list of items
List<String> itemsList = new ArrayList<String>();
itemsList.add("AAAA");
itemsList.add("AABC");
itemsList.add("ABCD");
itemsList.add("BBCD");
itemsList.add("BCDE");
itemsList.add("CCDE");
itemsList.add("CDEF");
// create an ArrayAdaptar from the String Array
final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
R.layout.item, itemsList);
ListView listView = (ListView) findViewById(R.id.list);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
// enables filtering for the contents of the given ListView
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
EditText myFilter = (EditText) findViewById(R.id.etText);
myFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
dataAdapter.getFilter().filter(s);
}
});
}
希望有所帮助