我是android编程的新手,并尝试将getFilter()实现到我的自定义ArrayAdapter中,按名称过滤List。 我没有找到任何与我的适配器匹配的示例。
有人可以帮我一个例子(用我的代码),thx !!!
也许有另一种可能来过滤List?
java代码:
public class c_DTArtikel_allforsearch extends Activity implements AdapterView.OnItemClickListener {
private ListView ListViewLayout;
private EditText search;
private String[] Name;
private String[] Number;
private int[] ImagesID;
private ArrayList<Article> articleList = new ArrayList<Article>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_lv_search);
Resources res = getResources();
String[] saugname = res.getStringArray(R.array.SauglanzeName);
String[] pumpmname = res.getStringArray(R.array.PumpMName);
String[] saugnummer = res.getStringArray(R.array.SauglanzeNumber);
String[] pumpmnummer = res.getStringArray(R.array.PumpMNummer);
TypedArray saugimg = res.obtainTypedArray(R.array.SauglanzeImg);
TypedArray pumpmimg = res.obtainTypedArray(R.array.PumpeMImg);
ArrayList<String> name = new ArrayList<String>();
name.addAll(Arrays.asList(saugname));
name.addAll(Arrays.asList(pumpmname));
Name = name.toArray(new String[saugname.length+pumpmname.length]);
ArrayList<String> number = new ArrayList<String>();
number.addAll(Arrays.asList(saugnummer));
number.addAll(Arrays.asList(pumpmnummer));
Number = number.toArray(new String[saugnummer.length+pumpmnummer.length]);
ArrayList<Integer> imgInts = new ArrayList<Integer>();
int nr = -1;
while ( ++nr < saugimg.length() )
imgInts.add (saugimg.getResourceId(nr, 0));
nr = -1;
while ( ++nr < pumpmimg.length() )
imgInts.add (pumpmimg.getResourceId(nr, 0));
ImagesID = new int[imgInts.size()];
for (int i=0; i < imgInts.size(); i++)
{
ImagesID[i] = ((Integer) imgInts.get(i)).intValue();
}
Search = (EditText) findViewById(R.id.EditTextSearch);
ListViewLayout = (ListView) findViewById(R.id.listViewLayoutSearch);
final TIAdapter adapter = new TIAdapter(articleList, this);
ListViewLayout.setAdapter(adapter);
ListViewLayout.setOnItemClickListener(this);
//has been missing
for (int i = 0; i < Name.length; i++)
{
Article a = new Article(Name[i], Number[i], ImagesID[i]);
// Binds all strings into an array
articleList.add(a);
}
// Add Text Change Listener to EditText
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
System.out.println("Text ["+s+"] - Start ["+start+"] - Before ["+before+"] - Count ["+count+"]");
if (count < before) {
// We're deleting char so we need to reset the adapter data
adapter.resetData();
}
adapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
类文章
public class Article {
public String Name;
public String Number;
public int ImagesID;
public Article(String Name, String Number, int ImagesID) {
this.Name = Name;
this.Number = Number;
this.ImagesID = ImagesID;
}
public String getName() {
return Name;
}
public void setName(String name) {
this.Name = Name;
}
public String getNumber() {
return Number;
}
public void setNumber(String number) {
this.Name = Number;
}
public int getImgID() {
return ImagesID;
}
public void setImgID(int imgID) {
this.ImagesID = ImagesID;
}
}
类自定义适配器:
class TIAdapter extends ArrayAdapter<Article> implements Filterable {
private List<Article> articleList;
private Context context;
private Filter articleFilter;
private List<Article> origArticleList;
TIAdapter(List<Article> articleList, Context ctx) {
super (ctx, R.layout.layout_text_image, articleList);
this.articleList = articleList;
this.context = ctx;
this.origArticleList = articleList;
}
public int getCount() {
return articleList.size();
}
public Article getItem(int position) {
return articleList.get(position);
}
public long getItemId(int position) {
return articleList.get(position).hashCode();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ArticleHolder holder = new ArticleHolder();
if (convertView == null) {
// This a new view we inflate the new layout
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.layout_text_image, null);
// Now we can fill the layout with the right values
holder.textViewArtikel = (TextView) v.findViewById(R.id.textViewArtikel);
holder.textViewArtikelNummer = (TextView) v.findViewById(R.id.textViewArtikelNummer);
holder.imageViewArtikel = (ImageView) v.findViewById(R.id.imageViewArtikel);
v.setTag(holder);
}
else {
holder = (ArticleHolder) v.getTag();
holder.imageViewArtikel = (ImageView) v.findViewById(R.id.imageViewArtikel);
DecodeTask dt1 = (DecodeTask)holder.imageViewArtikel.getTag(R.id.imageViewArtikel);
if(dt1 != null)
dt1.cancel(true);
}
final Article n = articleList.get(position);
holder.imageViewArtikel.setImageBitmap(null);
DecodeTask dt2 = new DecodeTask(getContext(), holder.imageViewArtikel, n.getImgID());
dt2.execute();
holder.textViewArtikel.setText(n.getName());
holder.textViewArtikelNummer.setText(n.getNumber());
holder.imageViewArtikel.setTag(n.getImgID(), dt2);
return v;
}
public void resetData() {
articleList = origArticleList;
}
private class ArticleHolder {
TextView textViewArtikel;
TextView textViewArtikelNummer;
ImageView imageViewArtikel;
}
@Override
public Filter getFilter() {
if (articleFilter == null)
articleFilter = new ArticleFilter();
return articleFilter;
}
private class ArticleFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// We implement here the filter logic
if (constraint == null || constraint.length() == 0) {
// No filter implemented we return all the list
results.values = origArticleList;
results.count = origArticleList.size();
}
else {
// We perform filtering operation
List<Article> nArticleList = new ArrayList<Article>();
for (Article a : articleList) {
if (a.getName().toLowerCase().contains(constraint.toString().toLowerCase()))
nArticleList.add(a);
}
results.values = nArticleList;
results.count = nArticleList.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// Now we have to inform the adapter about the new list filtered
if (results.count == 0)
notifyDataSetInvalidated();
else {
articleList = (List<Article>) results.values;
notifyDataSetChanged();
}
}
}
}
}
}
这是来自旧getView();
的代码 @Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageView imageView = null;
if(row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.layout_text_image, parent, false);
imageView = (ImageView)row.findViewById(R.id.imageViewArtikel);
}
else {
imageView = (ImageView)row.findViewById(R.id.imageViewArtikel);
DecodeTask dt1 = (DecodeTask)imageView.getTag(R.id.imageViewArtikel);
if(dt1 != null)
dt1.cancel(true);
}
imageView.setImageBitmap(null);
DecodeTask dt2 = new DecodeTask(getContext(), imageView, ImagesID[position]);
dt2.execute();
TextView textViewArtikel = (TextView) row.findViewById(R.id.textViewArtikel);
TextView textViewArtikelNummer = (TextView) row.findViewById(R.id.textViewArtikelNummer);
imageView.setTag(R.id.imageViewArtikel, dt2);
textViewArtikel.setText(Name[position]);
textViewArtikelNummer.setText(Number[position]);
return row;
}
解码任务
public class DecodeTask extends AsyncTask<String, Void, Bitmap> {
Context mContext;
ImageView v;
int resId;
public DecodeTask(Context context, ImageView iv, int res_id) {
mContext = context;
v = iv;
resId = res_id;
}
protected Bitmap doInBackground(String... params) {
return decodeBitmapFromResource(resId, 300, 300);
}
protected void onPostExecute(Bitmap result) {
v.setImageBitmap(result);
}
private Bitmap decodeBitmapFromResource(int resId, int reqWidth, int reqHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(mContext.getResources(), resId, options);
options.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeResource(mContext.getResources(), resId, options);
return bmp;
}
}
错误logcat:
05-03 10:56:43.922 28792-28792/com.kreussler.krapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.kreussler.krapp.c_DTArtikel_allforsearch$TIAdapter.getView(c_DTArtikel_allforsearch.java:286)
at android.widget.AbsListView.obtainView(AbsListView.java)
at android.widget.ListView.measureHeightOfChildren(ListView.java)
at android.widget.ListView.onMeasure(ListView.java)
at android.view.View.measure(View.java)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java)
at android.view.View.measure(View.java)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
at android.widget.FrameLayout.onMeasure(FrameLayout.java)
at android.view.View.measure(View.java)
at android.widget.LinearLayout.measureVertical(LinearLayout.java)
at android.widget.LinearLayout.onMeasure(LinearLayout.java)
at android.view.View.measure(View.java)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
at android.widget.FrameLayout.onMeasure(FrameLayout.java)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java)
at android.view.View.measure(View.java)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java)
at android.view.Choreographer.doCallbacks(Choreographer.java)
at android.view.Choreographer.doFrame(Choreographer.java)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java)
at android.os.Handler.handleCallback(Handler.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
当我发现你的问题很有趣时,我用你的代码做了一个活动。 我遇到的最大问题是在哪里可以获得大约250张图像。我首先想要的 问你,但设法将306张图片调整为100x100并放入 他们在drawable文件夹中。这是改变的getView()。
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ArticleHolder holder = new ArticleHolder();
final Article n = articleList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.activity_sergej_lazy_res_listitem, null);
holder.textViewArtikel = (TextView) v.findViewById(R.id.textViewArtikel);
holder.textViewArtikelNummer = (TextView) v.findViewById(R.id.textViewArtikelNummer);
holder.imageViewArtikel = (ImageView) v.findViewById(R.id.imageViewArtikel);
v.setTag(holder);
}
else {
holder = (ArticleHolder) v.getTag();
DecodeTask dt1 = (DecodeTask)holder.imageViewArtikel.getTag( );
if(dt1 != null)
dt1.cancel(true);
}
holder.textViewArtikel.setText(n.getName());
holder.textViewArtikelNummer.setText(n.getNumber());
holder.imageViewArtikel.setImageBitmap(null);
DecodeTask dt2 = new DecodeTask(getContext(), holder.imageViewArtikel, n.getImgID(), position );
holder.imageViewArtikel.setTag(dt2);
dt2.execute();
return v;
}
使用100x100的图像时,无需调整大小。 这里改变了AsyncTask:
public class DecodeTask extends AsyncTask<String, Void, Bitmap> {
Context mContext;
ImageView v;
int resId;
Bitmap bitmap = null;
int position;
public DecodeTask(Context context, ImageView iv, int res_id, int _position ) {
mContext = context;
v = iv;
resId = res_id;
position = _position;
v.setTag( this);
}
protected Bitmap doInBackground(String... params) {
bitmap = BitmapFactory.decodeResource(mContext.getResources(), resId);
return null;
}
protected void onPostExecute(Bitmap result) {
if( position < ListViewLayout.getFirstVisiblePosition() || position > ListViewLayout.getLastVisiblePosition())
return;
if ( v.getTag() == this )
v.setImageBitmap(bitmap);
}
}