public class MainActivity extends ListActivity { private ArrayList<Friend> friendList; private EditText et; private int[] imageBlock; private String[] nameBlock; private String[] descBlock; int textlength=0; private Resources res; private ListView lv; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); res = getResources(); et = (EditText) findViewById(R.id.EditText01); lv = (ListView) findViewById(android.R.id.list); friendList = new ArrayList<Friend>(); nameBlock = res.getStringArray(R.array.names); descBlock = res.getStringArray(R.array.descriptions); imageBlock = res.getIntArray(R.array.images); if(friendList == null) { Log.i("NullPointer", "Null Pointer in Friend List"); } int size = nameBlock.length; for(int i = 0 ; i < size; i++) { friendList.add(new Friend(nameBlock[i], descBlock[i], imageBlock[i])); } setListAdapter(new bsAdapter(this)); et.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { friendList.clear(); textlength = et.getText().length(); for (int i = 0; i < nameBlock.length; i++) { if (textlength <= nameBlock[i].length()) { if(nameBlock[i].toLowerCase().contains(et.getText().toString().toLowerCase().trim())) { Log.i("NullPointer", "Null Pointer in Friend List"); friendList.add(new Friend(nameBlock[i], descBlock[i], imageBlock[i])); } } } AppendList(friendList); } }); } public void AppendList(ArrayList<Friend> freind) { setListAdapter(new bsAdapter(this)); } public class bsAdapter extends BaseAdapter { Activity cntx; public bsAdapter(Activity context) { this.cntx=context; } public int getCount() { return friendList.size(); } public Object getItem(int position) { return friendList.get(position); } public long getItemId(int position) { return friendList.size(); } public View getView(final int position, View convertView, ViewGroup parent) { View row=null; LayoutInflater inflater=cntx.getLayoutInflater(); row=inflater.inflate(R.layout.search_list_item, null); TextView tv = (TextView) row.findViewById(R.id.title); ImageView im = (ImageView) row.findViewById(R.id.imageview); tv.setText(friendList.get(position).getName()); im.setImageDrawable(getResources().getDrawable(friendList.get(position).getImage())); return row; } } }
I keep getting an ArrayIndexOutOfBounds, but my lack of experience in LogCat kind of doesn't help... Could somebody just have a look at my code. And maybe assist me in someway? I'm building a FriendsList using a ListView and intergrating a search filter feature.
Thanks, Peter.