博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Filtering ListView with custom (object) adapter
阅读量:3607 次
发布时间:2019-05-21

本文共 17715 字,大约阅读时间需要 59 分钟。

21
8

I'm trying to implement filtering of a ListView which is uses a custom object adapter, but I can't find any useful samples. The included code is very simplified, so no- keep in mind I can't use an regular ArrayAdapter. I have a EditText above the ListView, and when the user enters text in the EditText widget I would like to filter the ListView by the text written in the EditText. Any suggestions would be much appreciated!

Here is the snippet from the activity class:

public class management_objects extends Activity {
private static List
UserList;private EfficientAdapter adapter = null;private ListView objectListView = null;private EditText SearchText = null;private static class EfficientAdapter extends BaseAdapter implements Filterable{
private LayoutInflater mInflater; public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context); } public int getCount() {
return UserList.size(); } public Object getItem(int position) {
return position; } public long getItemId(int position) {
return position; } public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder; if (convertView == null) {
convertView = mInflater.inflate(R.layout.imagelayout_2lines, null); holder = new ViewHolder(); holder.text = (TextView) convertView.findViewById(R.id.managementObjectText); holder.subtext = (TextView) convertView.findViewById(R.id.managementObjectSubText); holder.icon = (ImageView) convertView.findViewById(R.id.managementObjectIcon); convertView.setTag(holder); } else {
holder = (ViewHolder) convertView.getTag(); } holder.text.setText(UserList.get(position).getFirstName()); holder.subtext.setText(UserList.get(position).getLastName()); holder.icon.setImageResource(R.drawable.user); return convertView; } static class ViewHolder {
TextView text; TextView subtext; ImageView icon; } @Override public Filter getFilter() {
return null; }}@Overridepublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.adobjectlist); Bundle extras = getIntent().getExtras(); SearchText = (EditText) findViewById(R.id.SearchBox); SearchText.addTextChangedListener(filterTextWatcher); objectListView = (ListView) findViewById(R.id.ObjectList); objectListView.setOnItemClickListener(Item_Click); adapter = new EfficientAdapter(this); ComputerName = extras.getString("COMPUTER_NAME"); //Get User list from webservice ShowUsers();}

Here is The User class:

public class User {
private int UserId; private String FirstName; private String LastName; public int getUserId() {
return UserId; } public void setUserId(int UserId) {
this.UserId = UserId; } public String getFirstName() {
return FirstName; } public void setFirstName(String FirstName) {
this.FirstName = FirstName; } public String getLastName() {
return LastName; } public void setLastName(String LastName) {
this.LastName = LastName; }}
|
   

2 Answers

22

You need to do a few things:

1) In your activity, register for a text change listener on your EditText that contains the value the user enters:

mSearchValue.addTextChangedListener(searchTextWatcher);

2) Create your searchTextWatcher and have it do something:

private TextWatcher searchTextWatcher = new TextWatcher() {
@Override public void onTextChanged(CharSequence s, int start, int before, int count) {
// ignore } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// ignore } @Override public void afterTextChanged(Editable s) {
Log.d(Constants.TAG, "*** Search value changed: " + s.toString()); adapter.getFilter().filter(s.toString()); } };

3) Override getFilter() in your custom adapter and have it filter the results and notify the listview that the dataset has changed.

@Override    public Filter getFilter() {
return new Filter() {
@SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence constraint, FilterResults results) {
Log.d(Constants.TAG, "**** PUBLISHING RESULTS for: " + constraint); myData = (List
) results.values; MyCustomAdapter.this.notifyDataSetChanged(); } @Override protected FilterResults performFiltering(CharSequence constraint) {
Log.d(Constants.TAG, "**** PERFORM FILTERING for: " + constraint); List
filteredResults = getFilteredResults(constraint); FilterResults results = new FilterResults(); results.values = filteredResults; return results; } }; }
|
 
2  
Why you say "override" in 3) ? BaseAdapter doesn't have getFilter(). –   
3  
Good catch -- not needed. My custom Adapter class (actually inheriting from AmazingAdapter from the AmazingListView open source widget) implements the android.widget.Filterable interface which requires getFilter(). –   
3  
 helped me. The answer had some unclear things, or maybe I didn't read it with enough concentration. –   
6

salam here a example

public Filter getFilter() {
return new Filter() {
@Override protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults oReturn = new FilterResults(); final ArrayList
results = new ArrayList
(); if (orig == null) orig = items; if (constraint != null) {
if (orig != null && orig.size() > 0) {
for (final station g : orig) {
if (g.getName().toLowerCase() .contains(constraint.toString())) results.add(g); } } oReturn.values = results; } return oReturn; } @SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence constraint, FilterResults results) {
items = (ArrayList
) results.values; notifyDataSetChanged(); } };}public void notifyDataSetChanged() {
super.notifyDataSetChanged(); notifyChanged = true;}

Add search function to Custom Listview

 [March 10, 2013 at 8:48 AM]
 
2,023 views

1. Introduce

Adding search functionality to listview will filters the list data with a matching string, hence provides user an easy way to find the information he needs. In this simple tutorial i am discussing how to enable search filter to android custom ListView.

In this tutorial,we will display list of picture and search picture by its name.

2. Filter Method in Adapter

To filter the list data with a matching string from user,we use a Filter method in Adapter.

     
/**
 
* Filter
 
* @author 9Android.net
 
*
 
*/
public
void
filter(String charText) {
    
charText = charText.toLowerCase();
    
picList.clear();
    
if
(charText.length() ==
0
) {
        
picList.addAll(listpicOrigin);
    
}
else
{
        
for
(Picture pic : listpicOrigin) {
            
if
(pic.getPicName().toLowerCase().contains(charText)) {
                
picList.add(pic);
            
}
        
}
    
}
    
notifyDataSetChanged();
}

   
 

3. Create XML layout

activity_main.xml

 

<
LinearLayout
xmlns:android
=
""
    
xmlns:tools
=
""
    
android:id
=
"@+id/LinearLayout1"
    
android:layout_width
=
"match_parent"
    
android:layout_height
=
"match_parent"
    
android:orientation
=
"vertical"
>
 
    
<
ViewFlipper
        
android:id
=
"@+id/viewFlipper1"
        
android:layout_width
=
"match_parent"
        
android:layout_height
=
"fill_parent"
>
 
        
<
include
            
android:id
=
"@+id/listpic_layout"
            
layout
=
"@layout/listpic_layout"
/>
 
        
<
include
            
android:id
=
"@+id/viewpic_layout"
            
layout
=
"@layout/view_pic_layout"
/>
    
</
ViewFlipper
>
 
</
LinearLayout
>

   
 

listpic_layout.xml

 

<
LinearLayout
xmlns:android
=
""
    
xmlns:tools
=
""
    
android:id
=
"@+id/LinearLayout1"
    
android:layout_width
=
"match_parent"
    
android:layout_height
=
"match_parent"
    
android:orientation
=
"vertical"
>
 
    
<
EditText
        
android:id
=
"@+id/search_edt"
        
android:layout_width
=
"match_parent"
        
android:layout_height
=
"wrap_content"
        
android:drawableRight
=
"@drawable/search"
        
android:ems
=
"10"
        
android:hint
=
"Search pic name"
>
 
        
<
requestFocus
/>
    
</
EditText
>
 
    
<
ListView
        
android:id
=
"@+id/listView1"
        
android:layout_width
=
"match_parent"
        
android:layout_height
=
"wrap_content"
        
android:dividerHeight
=
"3dp"
>
    
</
ListView
>
 
</
LinearLayout
>

   
 

list_item.xml

 

<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
""
    
android:id
=
"@+id/LinearLayout1"
    
android:layout_width
=
"match_parent"
    
android:layout_height
=
"wrap_content"
    
android:orientation
=
"horizontal"
>
 
    
<
ImageView
        
android:id
=
"@+id/pic_icon_img"
        
android:layout_width
=
"120dp"
        
android:layout_height
=
"120dp"
        
android:scaleType
=
"fitXY"
        
android:src
=
"@drawable/ic_launcher"
/>
 
    
<
LinearLayout
        
android:layout_width
=
"wrap_content"
        
android:layout_height
=
"match_parent"
        
android:layout_weight
=
"1"
        
android:orientation
=
"vertical"
>
 
        
<
TextView
            
android:id
=
"@+id/pic_name_txt"
            
android:layout_width
=
"fill_parent"
            
android:layout_height
=
"wrap_content"
            
android:layout_gravity
=
"center"
            
android:layout_marginTop
=
"10dp"
            
android:layout_weight
=
"1"
            
android:gravity
=
"center_vertical"
            
android:text
=
"Pic Name"
            
android:textAppearance
=
"?android:attr/textAppearanceLarge"
            
android:textStyle
=
"bold"
/>
 
        
<
TextView
            
android:id
=
"@+id/pic_type_txt"
            
android:layout_width
=
"fill_parent"
            
android:layout_height
=
"wrap_content"
            
android:layout_weight
=
"1"
            
android:text
=
"Pic Type"
            
android:textAppearance
=
"?android:attr/textAppearanceMedium"
/>
    
</
LinearLayout
>
 
    
<
ImageView
        
android:id
=
"@+id/imageView2"
        
android:layout_width
=
"wrap_content"
        
android:layout_height
=
"wrap_content"
        
android:layout_gravity
=
"center_vertical"
        
android:src
=
"@drawable/arrow"
/>
 
</
LinearLayout
>

   
 

view_pic_layout.xml

 

<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
""
    
android:layout_width
=
"match_parent"
    
android:layout_height
=
"match_parent"
    
android:orientation
=
"vertical"
>
 
    
<
ImageView
        
android:id
=
"@+id/viewpic_img"
        
android:layout_width
=
"fill_parent"
        
android:layout_height
=
"fill_parent"
        
android:scaleType
=
"fitCenter"
        
android:src
=
"@drawable/bikinigirl_3"
/>
 
</
LinearLayout
>

   
 

4. Code application

PicListAdapter.java

 

public
class
PicListAdapter
extends
BaseAdapter {
    
private
Context mContext;
    
private
LayoutInflater mInflater;
    
private
List<Picture> picList =
null
;
    
private
ArrayList<Picture> listpicOrigin;
 
    
public
PicListAdapter(Context context, List<Picture> picList) {
        
mContext = context;
        
this
.picList = picList;
        
mInflater = LayoutInflater.from(mContext);
        
this
.listpicOrigin =
new
ArrayList<Picture>();
        
this
.listpicOrigin.addAll(picList);
    
}
 
    
public
class
ViewHolder {
        
TextView picName;
        
TextView picType;
        
ImageView picIcon;
    
}
 
    
public
View getView(
int
position, View view, ViewGroup parent) {
        
final
ViewHolder holder;
        
if
(view ==
null
) {
            
holder =
new
ViewHolder();
            
view = mInflater.inflate(R.layout.list_item,
null
);
            
holder.picName = (TextView) view.findViewById(R.id.pic_name_txt);
            
holder.picType = (TextView) view.findViewById(R.id.pic_type_txt);
            
holder.picIcon = (ImageView) view.findViewById(R.id.pic_icon_img);
            
view.setTag(holder);
        
}
else
{
            
holder = (ViewHolder) view.getTag();
        
}
 
        
holder.picName.setText(picList.get(position).getPicName());
        
holder.picType.setText(picList.get(position).getPicType());
        
holder.picIcon.setImageResource(picList.get(position).getPicSource());
 
        
return
view;
    
}
 
    
public
int
getCount() {
        
return
picList.size();
    
}
 
    
public
Picture getItem(
int
position) {
        
return
picList.get(position);
    
}
 
    
public
long
getItemId(
int
position) {
        
return
position;
    
}
 
    
/**
     
* Filter
     
* @author 9Android.net
     
*
     
*/
    
public
void
filter(String charText) {
        
charText = charText.toLowerCase();
        
picList.clear();
        
if
(charText.length() ==
0
) {
            
picList.addAll(listpicOrigin);
        
}
else
{
            
for
(Picture pic : listpicOrigin) {
                
if
(pic.getPicName().toLowerCase().contains(charText)) {
                    
picList.add(pic);
                
}
            
}
        
}
        
notifyDataSetChanged();
    
}
 
}

   
 

MainActivity.java

 

public
class
MainActivity
extends
Activity
implements
TextWatcher,
        
OnItemClickListener {
 
    
private
static
final
int
LIST_PIC_SCREEN =
0
;
    
private
static
final
int
VIEW_PIC_SCREEN =
1
;
 
    
private
String[] listPicName = {
"androidgirl_1"
,
"androidgirl_2"
,
            
"androidgirl_3"
,
"beautifulgirl_1"
,
"beautifulgirl_2"
,
            
"bikinigirl_1"
,
"bikinigirl_2"
,
"bikinigirl_3"
,
"cutegirl_1"
,
            
"cutegirl_2"
,
"cutegirl_3"
,
"cutegirl_4"
,
"cutegirl_5"
,
            
"hotgirl_1"
,
"hotgirl_2"
,
"hotgirl_3"
,
"nudegirl_1"
,
"nudegirl_2"
,
            
"nudegirl_3"
,
"nudegirl_4"
,
"sexygirl_1"
,
"sexygirl_2"
,
            
"sexygirl_3"
,
"sportgirl_1"
,
"sportgirl_2"
,
"sportgirl_3"
,
            
"sportgirl_4"
};
 
    
private
String[] listPicType = {
"android"
,
"android"
,
"android"
,
            
"beautiful"
,
"beautiful"
,
"bikini"
,
"bikini"
,
"bikini"
,
"cutegirl"
,
            
"cutegirl"
,
"cutegirl"
,
"cutegirl"
,
"cutegirl"
,
"hotgirl"
,
            
"hotgirl"
,
"hotgirl"
,
"nude"
,
"nude"
,
"nude"
,
"nude"
,
"sexy"
,
            
"sexy"
,
"sexy"
,
"sport"
,
"sport"
,
"sport"
,
"sport"
};
    
private
int
[] listPicDrawable = { R.drawable.androidgirl_1,
            
R.drawable.androidgirl_2, R.drawable.androidgirl_3,
            
R.drawable.beautifulgirl_1, R.drawable.beautifulgirl_2,
            
R.drawable.bikinigirl_1, R.drawable.bikinigirl_2,
            
R.drawable.bikinigirl_3, R.drawable.cutegirl_1,
            
R.drawable.cutegirl_2, R.drawable.cutegirl_3,
            
R.drawable.cutegirl_4, R.drawable.cutegirl_5, R.drawable.hotgirl_1,
            
R.drawable.hotgirl_2, R.drawable.hotgirl_3, R.drawable.nudegirl_1,
            
R.drawable.nudegirl_2, R.drawable.nudegirl_3,
            
R.drawable.nudegirl_4, R.drawable.sexygirl_1,
            
R.drawable.sexygirl_2, R.drawable.sexygirl_3,
            
R.drawable.sportgirl_1, R.drawable.sportgirl_2,
            
R.drawable.sportgirl_3, R.drawable.sportgirl_4
 
    
};
 
    
private
ArrayList<Picture> listPic =
new
ArrayList<Picture>();
    
private
ListView listview;
    
private
PicListAdapter adapter;
    
private
EditText searchEdt;
    
private
ViewFlipper fliper;
    
private
ImageView viewpic;
 
    
@Override
    
public
void
onCreate(Bundle savedInstanceState) {
        
super
.onCreate(savedInstanceState);
        
setContentView(R.layout.activity_main);
        
fliper = (ViewFlipper) findViewById(R.id.viewFlipper1);
        
listview = (ListView) findViewById(R.id.listView1);
        
for
(
int
i =
0
; i < listPicName.length; i++) {
            
Picture pic =
new
Picture(listPicName[i], listPicType[i],
                    
listPicDrawable[i]);
            
listPic.add(pic);
        
}
        
adapter =
new
PicListAdapter(
this
, listPic);
        
listview.setAdapter(adapter);
        
listview.setOnItemClickListener(
this
);
 
        
searchEdt = (EditText) findViewById(R.id.search_edt);
        
searchEdt.addTextChangedListener(
this
);
        
viewpic = (ImageView) findViewById(R.id.viewpic_img);
    
}
 
    
@Override
    
public
boolean
onCreateOptionsMenu(Menu menu) {
        
getMenuInflater().inflate(R.menu.activity_main, menu);
        
return
true
;
    
}
 
    
/**
     
* @author 9Android.net
     
*/
    
public
void
beforeTextChanged(CharSequence s,
int
start,
int
count,
            
int
after) {
        
// TODO Auto-generated method stub
 
    
}
 
    
public
void
onTextChanged(CharSequence s,
int
start,
int
before,
int
count) {
        
// TODO Auto-generated method stub
 
    
}
 
    
public
void
afterTextChanged(Editable s) {
        
// TODO Auto-generated method stub
        
String text = searchEdt.getText().toString().toLowerCase();
        
adapter.filter(text);
    
}
 
    
/**
     
*  @author 9Android.net
     
*/
    
public
void
onItemClick(AdapterView<?> parent, View view,
int
position,
            
long
id) {
        
// TODO Auto-generated method stub
        
Sliding.slideFromRightToLeft(VIEW_PIC_SCREEN, fliper);
        
viewpic.setImageResource(listPic.get(position).getPicSource());
 
    
}
 
    
/**
     
* @author 9Android.net
     
* @param : event, keycode of downed button
     
* @Objective : Handle keyevent on main activity
     
*/
    
public
boolean
onKeyDown(
int
keyCode, KeyEvent event) {
 
        
if
(keyCode == KeyEvent.KEYCODE_BACK) {
            
int
screen = fliper.getDisplayedChild();
 
            
if
(screen == VIEW_PIC_SCREEN) {
                
Sliding.slideFromLeftToRight(LIST_PIC_SCREEN, fliper);
                
return
true
;
            
}
        
}
        
return
super
.onKeyDown(keyCode, event);
    
}
 
}

   
 

5. Conclusion

If you want to hide keyboard when load Activity,you put android:windowSoftInputMode=”stateHidden”

into AndroidManifest.xml

 

<
manifest
xmlns:android
=
""
    
package
=
"com.example.searchinginlistview"
    
android:versionCode
=
"1"
    
android:versionName
=
"1.0"
>
 
    
<
uses-sdk
        
android:minSdkVersion
=
"8"
        
android:targetSdkVersion
=
"15"
/>
 
    
<
application
        
android:icon
=
"@drawable/icon"
        
android:label
=
"@string/app_name"
        
android:theme
=
"@style/AppTheme"
>
        
<
activity
            
android:name
=
".MainActivity"
            
android:label
=
"@string/title_activity_main"
            
android:windowSoftInputMode
=
"stateHidden"
            
>
            
<
intent-filter
>
                
<
action
android:name
=
"android.intent.action.MAIN"
/>
 
                
<
category
android:name
=
"android.intent.category.LAUNCHER"
/>
            
</
intent-filter
>
        
</
activity
>
    
</
application
>
 
</
manifest
>

   
 

In this tutorial, we have known about how to enable search functionality to custom Listview. You can improve it to enable search functionality to custom Expandable Listview.

转载地址:http://hsxzn.baihongyu.com/

你可能感兴趣的文章
疏漏总结(九)——http与https
查看>>
疏漏总结(十)
查看>>
线程池
查看>>
Mysql(条件,常用函数,分组)
查看>>
servlet的其他作用,git的使用方法
查看>>
Oracle数据库sql*plus常用命令
查看>>
Oracle中表的简单查询
查看>>
Linux-进程管理
查看>>
Linux-ssh服务及服务管理、文件传输
查看>>
Linux-网络配置
查看>>
开发中浏览器兼容的问题总结
查看>>
Vue初体验
查看>>
Vue学习之二(vue指令)
查看>>
人力资源项目-角色模块
查看>>
Matrixport首席执行官葛越晟:区块链市场具有充足的流动性及高溢价
查看>>
量子链创始人帅初:平台和应用需要具备区块链特征,但不一定需要去中心化...
查看>>
印度加密交易所解禁:交易量暴增6倍,全球Buy in了吗?
查看>>
明年3月实施!韩国通过特别金融法案,加密货币完全合法化
查看>>
7种启动Spring Boot项目的方式,一次性打包说给你听
查看>>
《Spring Cloud与Docker微服务架构实战》.pdf
查看>>