如何基于POJO类中的Android类项目,以实现CustomListView搜索? [英] How to implement search in CustomListView based on class item of POJO class in Android?
问题描述
我有一个自定义列表视图。我必须从Web服务器显示的数据。我需要的基础上,从输入的EditText实现搜索。在ListView中每一行包含图片,标题和消息。 根据来自Web服务器的响应影像学改变。看看在code。
类CustomListView扩展ArrayAdapter {
上下文语境;
LayoutInflater mInflater;
ArrayList的<&的PostData GT; mPostingData = NULL;
私人位图mIcon1;
私人位图mIcon2;
私人位图mIcon3;
熔点的PostData;
公共CustomListView(上下文C)
{
超级(C,0);
mInflater =(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mIcon1 = BitmapFactory.de codeResource(c.getResources(),R.drawable.text_icon);
mIcon2 = BitmapFactory.de codeResource(c.getResources(),R.drawable.image_icon);
mIcon3 = BitmapFactory.de codeResource(c.getResources(),R.drawable.video_icon);
}
公众诠释getCount将(){
// TODO自动生成方法存根
如果(mPostingData!= NULL){
返回mPostingData.size();
}其他{
返回0;
}
}
公共无效使用setData(ArrayList中<&的PostData GT; mPpst){
mPostingData = mPpst;
}
公共对象的getItem(INT为arg0){
// TODO自动生成方法存根
返回将arg0;
}
公共查看getView(INT位置,查看convertView,ViewGroup中父){
ViewHolder持有人;
///整型= getItemViewType(为arg0);
Log.i(阿鲁,获取视图);
如果(mPostingData == NULL){
返回null;
}
如果(convertView == NULL){
convertView = mInflater.inflate(R.layout.listviewimg,NULL);
convertView.setLayoutParams(新AbsListView.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//创建一个ViewHolder和存储引用的两个孩子意见
//我们希望将数据绑定到。
持有人=新ViewHolder();
holder.ll =(的LinearLayout)convertView.findViewById(R.id.lvid);
holder.text =(TextView中)convertView.findViewById(R.id.texttitle);
holder.text2 =(TextView中)convertView.findViewById(R.id.tvst);
holder.icon =(ImageView的)convertView.findViewById(R.id.llimage);
convertView.setTag(保持器);
} 其他 {
支架=(ViewHolder)convertView.getTag();
}
MP = mPostingData.get(位置);
字符串标题= mp.mType;
如果(mp.mTitle = NULL和放大器;!&安培; Name.equals(mp.mPostedBy)){
标题= mp.mTitle ++称号;
//holder.text.setBackgroundColor(Color.WHITE);
holder.ll.setBackgroundResource(R.drawable.listbkgme);
holder.text.setText(职称);
}
否则,如果(mp.mTitle = NULL和放大器;!&安培;!名称= mp.mPostedBy)
{
标题= mp.mTitle ++称号;
holder.text.setText(职称);
}
如果(mp.mMessage = NULL和放大器;!&安培; Name.equals(mp.mPostedBy)){
holder.ll.setBackgroundResource(R.drawable.listbkgme);
holder.text2.setText(mp.mMessage);
}
否则,如果(mp.mMessage = NULL和放大器;!&安培;!名称=(mp.mPostedBy))
{
holder.text2.setText(mp.mMessage);
}
如果(mp.mImageUrl!= NULL){
holder.icon.setImageBitmap(mIcon2);
}否则,如果(mp.mVideoUrl!= NULL){
holder.icon.setImageBitmap(mIcon3);
}其他{
holder.icon.setImageBitmap(mIcon1);
}
返回convertView;
}
类ViewHolder {
TextView的文字;
TextView的文本2;
ImageView的图标;
的LinearLayout LL;
}
众长getItemId(INT位置){
// TODO自动生成方法存根
返回的位置;
}
}
发表数据类
类{的PostData
字符串MID;
字符串mPostedBy;
字符串mTitle;
字符串mMessage;
字符串mImageUrl;
字符串mVideoUrl;
字符串MTYPE;
布尔我= FALSE;
}
排序基于标题。如何搜索列表视图的基础上标题??
搜索=(EditText上)findViewById(R.id.searchbox);
search.addTextChangedListener(新TextWatcher(){
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
。最后弦乐searchString = search.getText()的toString();
INT正文长度= searchString.length();
Collections.sort(
mTempPost,
新的比较<&的PostData GT;()
{
公众诠释比较(LHS的PostData,RHS的PostData){
的System.out.println(........................+ lhs.mTitle ++ rhs.mTitle);
返回lhs.mTitle.compareTo(rhs.mTitle);
}
}
);
mCustomListView.notifyDataSetChanged();
}
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
之后INT){
}
公共无效afterTextChanged(编辑S){
// mCustomListView.notifyDataSetChanged();
}
});
您希望您的适配器来实现的可筛选。然后覆盖用getFilter进行搜索
@覆盖
公共过滤用getFilter(){
返回新的过滤器(){
@覆盖
保护无效publishResults(CharSequence的约束,FilterResults结果){
如果(结果= NULL和放大器;!&安培; results.count> = 0){
使用setData(results.values);
} 其他 {
使用setData(mPostingData);
}
notifyDataSetInvalidated();
}
@覆盖
保护FilterResults performFiltering(CharSequence的约束){
FilterResults导致=新FilterResults();
如果(!TextUtils.isEmpty(约束)){
约束= constraint.toString()与toLowerCase()。
ArrayList的<&的PostData GT; currentItems =新的ArrayList<&的PostData GT;();
ArrayList的<&的PostData GT; foundItems =新的ArrayList<&的PostData GT;();
currentItems.addAll(mPostingData);
用于(后的PostData:currentItems){
//搜索位置的项目。如果我们得到一个匹配,添加到列表
如果(post.mType.toLowerCase()。包括(约束)){
foundItems.add(米);
}否则,如果... {
}
}
result.count = foundItems.size();
result.values = foundItems;
} 其他 {
result.count = -1;
}
返回结果;
}
};
}
您将调用来自有adapter.getFilter()活性的搜索。过滤器(搜索查询)。
此将与搜索结果太替换在列表视图的项目的列表。
I have a Custom Listview. I have to display data from the webserver. I need to implement search based on input from EditText. Each row in ListView Contains Image, Title and Message. Image Changes based on response from WebServer. Have a Look at the code.
class CustomListView extends ArrayAdapter {
Context context;
LayoutInflater mInflater;
ArrayList<PostData> mPostingData = null;
private Bitmap mIcon1;
private Bitmap mIcon2;
private Bitmap mIcon3;
PostData mp ;
public CustomListView(Context c)
{
super(c, 0);
mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mIcon1 = BitmapFactory.decodeResource(c.getResources(), R.drawable.text_icon);
mIcon2 = BitmapFactory.decodeResource(c.getResources(), R.drawable.image_icon);
mIcon3 = BitmapFactory.decodeResource(c.getResources(), R.drawable.video_icon);
}
public int getCount() {
// TODO Auto-generated method stub
if(mPostingData!=null){
return mPostingData.size();
}else{
return 0;
}
}
public void setData(ArrayList<PostData> mPpst) {
mPostingData = mPpst;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
///int type = getItemViewType(arg0);
Log.i("Aru","get View");
if(mPostingData == null ){
return null;
}
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listviewimg, null);
convertView.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.ll=(LinearLayout) convertView.findViewById(R.id.lvid);
holder.text = (TextView) convertView.findViewById(R.id.texttitle);
holder.text2 = (TextView) convertView.findViewById(R.id.tvst);
holder.icon = (ImageView) convertView.findViewById(R.id.llimage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
mp = mPostingData.get(position);
String title = mp.mType;
if(mp.mTitle!=null && Name.equals(mp.mPostedBy )){
title = mp.mTitle+" "+title;
//holder.text.setBackgroundColor(Color.WHITE);
holder.ll.setBackgroundResource(R.drawable.listbkgme);
holder.text.setText(title);
}
else if(mp.mTitle!=null && Name!=mp.mPostedBy)
{
title = mp.mTitle+" "+title;
holder.text.setText(title);
}
if(mp.mMessage!=null && Name.equals(mp.mPostedBy )){
holder.ll.setBackgroundResource(R.drawable.listbkgme);
holder.text2.setText(mp.mMessage);
}
else if(mp.mMessage!=null && Name!=(mp.mPostedBy))
{
holder.text2.setText(mp.mMessage);
}
if(mp.mImageUrl!=null ){
holder.icon.setImageBitmap(mIcon2);
}else if(mp.mVideoUrl!=null){
holder.icon.setImageBitmap(mIcon3);
}else{
holder.icon.setImageBitmap(mIcon1);
}
return convertView;
}
class ViewHolder {
TextView text;
TextView text2;
ImageView icon;
LinearLayout ll;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
Post Data Class
class PostData {
String mID;
String mPostedBy;
String mTitle;
String mMessage;
String mImageUrl;
String mVideoUrl;
String mType ;
boolean me=false;
}
Sorting Based on Title. How can i search the listview based on Title??
search= (EditText) findViewById(R.id.searchbox);
search.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
final String searchString=search.getText().toString();
int textLength=searchString.length();
Collections.sort(
mTempPost,
new Comparator<PostData>()
{
public int compare(PostData lhs, PostData rhs) {
System.out.println("........................"+lhs.mTitle+" "+rhs.mTitle);
return lhs.mTitle.compareTo(rhs.mTitle);
}
}
);
mCustomListView.notifyDataSetChanged();
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
// mCustomListView.notifyDataSetChanged();
}
});
You want your adapter to implement Filterable. Then override getFilter to perform the search
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count >= 0) {
setData(results.values);
} else {
setData(mPostingData);
}
notifyDataSetInvalidated();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults result = new FilterResults();
if (!TextUtils.isEmpty(constraint)) {
constraint = constraint.toString().toLowerCase();
ArrayList<PostData> currentItems = new ArrayList<PostData>();
ArrayList<PostData> foundItems = new ArrayList<PostData>();
currentItems.addAll(mPostingData);
for (PostData post: currentItems){
// Search for the items here. If we get a match, add to the list
if (post.mType.toLowerCase().contains(constraint)) {
foundItems.add(m);
} else if .... {
}
}
result.count = foundItems.size();
result.values = foundItems;
} else {
result.count = -1;
}
return result;
}
};
}
You would then call the search from the activity with adapter.getFilter().filter("Search Query").
This will replace the list of items in your listview with the search results too.
这篇关于如何基于POJO类中的Android类项目,以实现CustomListView搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!