2013-03-15 71 views
0

我正在开发一个应用程序。其中我需要一个具有textview和图像的列表视图。我做到这一点的:在XMLListView具有TextView和图像ans搜索

列表视图:

<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); 

现在我申请搜索上的ListView为:

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); 
} 

但问题是:

当我搜索任何数据的搜索是正确完成,但通信ding图像不被搜索,因为我不知道如何应用逻辑来获取搜索到的图像。

所以请建议我该怎么做。

+0

使基础上,text.based文本搜索显示图像对应的列表视图 – Raghunandan 2013-03-15 09:29:37

+0

HTTP搜索:// stackoverflow.com/questions/13090046/how-to-implement-search-in-customlistview-based-on-class-item-of-pojo-class-in-a。 – Raghunandan 2013-03-15 09:44:46

回答

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; 
    }