我正在开发一个应用程序。其中我需要一个具有textview和图像的列表视图。我这样做了:
XML格式的列表视图:
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/tablelayout1"
android:layout_below="@+id/edittext_searchbar"
android:layout_margin="15dip"
android:listSelector="@drawable/list_selector"
android:divider="#623b42"
android:dividerHeight="0.5dip"
android:cacheColorHint="#00000000"
android:overScrollMode="never" >
</ListView>
XML中列表视图的适配器是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_item_selector" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
tools:ignore="UselessParent" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:ignore="UselessParent" >
<TextView
android:id="@+id/textview"
android:layout_width="0sp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="5sp"
android:textColor="@color/color_black"
android:textSize="18sp"
tools:ignore="SelectableText" />
<ImageView
android:id="@+id/imageview"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@string/string_todo"
android:paddingRight="5sp" />
</TableRow>
</TableLayout>
</RelativeLayout>
适配器的.java如下:
public class CustomListAdapter extends ArrayAdapter<String> {
/** Global declaration of variables. As there scope lies in whole class. */
private Context context;
private String[] Listofdata;
ImageClass icon[] = null;
/** Constructor Class */
public CustomListAdapter (Context context,String[] mainList, ImageClass[] MenuIcon) {
super(context,R.layout.mainmenu_screen_adapter_layout,mainList);
this.context = context;
this.Listofdata= mainList;
this.icon = MenuIcon;
}
/** Implement getView method for customizing row of list view. */
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
// Creating a view of row.
View rowView = inflater.inflate(R.layout.mainmenu_screen_adapter_layout, parent, false);
TextView textView = (TextView)rowView.findViewById(R.id.textview);
ImageView imageView = (ImageView)rowView.findViewById(R.id.imageview);
textView.setText(menuListofAdapter[position]);
ImageClass MenuIcon = icon[position];
imageView.setImageResource(MenuIcon .icon);
return rowView;
}
}
在main.java中我完成了代码:
对于图像,我创建了一个图像数组:
ImageClass MenuIcon[] = new ImageClass []{
new ImageClass (R.drawable.veg),
new ImageClass (R.drawable.non_veg),
new ImageClass (R.drawable.liquor),
new ImageClass (R.drawable.desert),
new ImageClass (R.drawable.beverages)
};
调用自定义适配器是:
CustomListAdapter adapter = new CustomListAdapter (this,mainList,MenuIcon);
listView.setAdapter(adapter);
现在我将列表视图上的搜索应用为:
edittextSearch.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s){
// Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s,int start, int count, int after){
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s,int start, int before, int count){
searchedData.clear();
for (int i = 0, j = 0; i < mainMenuList.length; i++) {
if (((String)mainList[i].toLowerCase(Locale.getDefault())).contains(edittextSearch.getText().toString().toLowerCase())) {
searchedData.add(mainMenuList[i].toString());
}
}
menuSearchList = searchedData.toArray(new String[searchedData.size()]);
CustomListAdapter adapter = new CustomListAdapter (main.this,menuSearchList,MenuIcon);
listView.setAdapter(adapter);
}
但问题是:
当我搜索任何数据时,搜索已正确完成但相应的图像未被搜索,因为我不知道如何应用逻辑来获取搜索项目的图像。
所以请建议我该怎么做。
答案 0 :(得分:0)
试试这个
//put this where you declared searchedData
ArrayList<ImageClass> mImageSearched = new ArrayList<ImageClass>;
public void onTextChanged(CharSequence s,int start, int before, int count){
searchedData.clear();
mImageSearched.clear();
for (int i = 0, j = 0; i < mainMenuList.length; i++) {
if (((String)mainList[i].toLowerCase(Locale.getDefault())).contains(edittextSearch.getText().toString().toLowerCase())) {
searchedData.add(mainMenuList[i].toString());
mImageSearched.add(MenuIcon[i]);
}
}
menuSearchList = searchedData.toArray(new String[searchedData.size()]);
ImageClass mImageSearchArray[] = mImageSearched.toArray(new ImageClass[mImageSearched.size[]]);
CustomListAdapter adapter = new CustomListAdapter (main.this,menuSearchList,mImageSearchArray);
listView.setAdapter(adapter);
}
您还必须更改适配器构造函数
public CustomListAdapter (Context context,String[] mainList, ImageClass[] mMenuIcon) {
super(context,R.layout.mainmenu_screen_adapter_layout,mainList);
this.context = context;
this.Listofdata= mainList;
this.icon = mMenuIcon;
}