我制作了一个应用程序,其中包含使用listView
的保险公司列表。使用数组适配器填充listView
。我希望能够使用搜索栏搜索此列表,并且只返回搜索到的保险公司。我已经完成了一个教程,但它没有过滤保险公司。有人能告诉我哪里出错了吗
到目前为止,这是我的代码
insurance.xml
<EditText
android:layout_width="400dp"
android:layout_height="60dp"
android:id="@+id/editText"
android:layout_marginBottom="50dp"
/>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="107dp">
</ListView>
myList.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="10"
android:background="#26BFDA">
<LinearLayout
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="2"
>
<ImageView
android:id="@+id/icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="6"
android:orientation="vertical"
>
<TextView
android:id="@+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="TextView"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
>
<ImageView
android:id="@+id/imageView2"
android:layout_width="20dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:src="@drawable/nextarrow"
/>
</LinearLayout>
</LinearLayout>
Insurance.java
public class Insurance extends AppCompatActivity {
ListView list;
ArrayAdapter<String> newadapter;
EditText inputSearch;
String[] itemname ={
"AA",
"Acorn",
"Admiral",
};
Integer[] imgid= {
R.drawable.aa,
R.drawable.acorn,
R.drawable.admiral,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insurance);
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
newadapter = new ArrayAdapter<String>(this, R.layout.mylist, R.id.item, itemname);
inputSearch = (EditText) findViewById(R.id.editText);
list.setAdapter(newadapter);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Insurance.this.newadapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem = itemname[+position];
Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();
if (Slecteditem.toString() == "AA") {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:028 9032 2232"));
startActivity(intent);
}
if (Slecteditem.toString() == "Carrot") {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:028 9032 2265"));
startActivity(intent);
}
}
});