我有一个自定义列表视图。我必须显示来自网络服务器的数据。我需要根据来自 EditText 的输入来实现搜索。ListView 中的每一行都包含图像、标题和消息。基于来自 WebServer 的响应的图像更改。看看代码。
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;
}
}
发布数据类
class PostData {
String mID;
String mPostedBy;
String mTitle;
String mMessage;
String mImageUrl;
String mVideoUrl;
String mType ;
boolean me=false;
}
根据标题排序。如何根据标题搜索列表视图?
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();
}
});