How to implement a view holder?
android, listview, view
Solution
You would need to write you own version of `ListView` to do that (which is bad). If the `ListView` doesn't work properly, it probably means that you are doing something wrong.
Where does the `id` element come from? You are getting the position in your `getView()` method, so you don't need to worry about exceeding list bounds. The position is linked to the element position in your list, so you can get the correct element like this:
myElements.get(position);
When the data in your list changes, you can call this:
yourAdapter.notifyDataSetChanged()
That will rebuild your list with new data (while keeping your scrolling and stuff).
Problem
i am using a viewholder to display from a dynamic arrayadapter.it works but the data displayed changes irregularly when i scroll the List.i want my List View to be populated only once ,Not all the time when i scroll my list. Any suggestion? Here is my Code ``` public View getView(int position, View convertView, ViewGroup parent) { // A ViewHolder keeps references to children views to avoid unneccessary calls // to findViewById() on each row. ViewHolder holder; // When convertView is not null, we can reuse it directly, there is no need // to reinflate it. We only inflate a new View when the convertView supplied // by ListView is null. if (convertView == null) { convertView = mInflater.inflate(R.layout.sample, null); // Creates a ViewHolder and store references to the two children views // we want to bind data to. holder = new ViewHolder(); holder.name = (TextView) convertView.findViewById(R.id.text); holder.icon = (ImageView) convertView.findViewById(R.id.icon); convertView.setTag(holder); } else { // Get the ViewHolder back to get fast access to the TextView // and the ImageView. holder = (ViewHolder) convertView.getTag(); } // Bind the data efficiently with the holder. if(_first==true) { if(id<myElements.size()) { holder.name.setText(myElements.get(id)); holder.icon.setImageBitmap( mIcon1 ); id++; } else { _first=false; } } //holder.icon.setImageBitmap(mIcon2); /*try{ if(id<myElements.size()) id++; else { id--; } } catch(Exception e) { android.util.Log.i("callRestService",e.getMessage()); }*/ return convertView; } static class ViewHolder { TextView name; ImageView icon; } ``` when the list is loaded it looks like this : https://i.stack.imgur.com/NrGhR.png after scrolling some data https://i.stack.imgur.com/sMbAD.png it looks like this, and again if i scroll to the beginning it looks https://i.stack.imgur.com/0KjMa.png P.S : my list have to be in alphabetic order