Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get rid of lag/jitter in listview? Can anybody help me with my code on how to cache it?
    primarykey
    data
    text
    <p>I am trying to understand the concept of cache. Can anybody help me with my code, as i my listview is having a lag whenever i scroll up or down. I tried understanding the android tutorial on this. But it didnt help me much. </p> <p>As Told by my people who helped me i changed my view to viewHolder.</p> <p>But now i am coming accross a new error. The return type is incompatible with ArrayAdapter.getView(int, View, ViewGroup)</p> <p>public class MovieRatingsActivity extends ListActivity</p> <p>{</p> <pre><code>private ArrayList&lt;Movie&gt; movies = new ArrayList&lt;Movie&gt;(); private LayoutInflater mInflater; private LruCache&lt;String, Bitmap&gt; cache; /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initializeUI(); } private void initializeUI() { mInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); InputStream inputStream = getResources().openRawResource( R.raw.ratings); movies = Movie.loadFromFile(inputStream); setListAdapter(new RowIconAdapter(this, R.layout.listrow, R.id.row_label, movies)); } /** Custom row adatper -- that displays an icon next to the movie name */ class RowIconAdapter extends ArrayAdapter&lt;Movie&gt; { private ArrayList&lt;Movie&gt; movies; public RowIconAdapter(Context c, int rowResourceId, int textViewResourceId, ArrayList&lt;Movie&gt; items) { super(c, rowResourceId, textViewResourceId, items); movies = items; } /* public View getView(int pos, View convertView, ViewGroup parent) { View row = mInflater.inflate(R.layout.listrow, parent, false); Movie currMovie = movies.get(pos); if (currMovie != null) { ImageView icon = (ImageView) row.findViewById(R.id.row_icon); TextView movieText = (TextView) row.findViewById(R.id.row_label); TextView votesText = (TextView) row.findViewById(R.id.row_subtext); movieText.setText(currMovie.getName()); String votesStr = currMovie.getVotes()+" votes"; votesText.setText(votesStr); Bitmap movieIcon = getMovieIcon(currMovie.getName(), currMovie.getRating()); icon.setImageBitmap(movieIcon); Log.w("MVMVMVMVMVMV", "Creating row view at position "+pos+" movie "+currMovie.getName()); } return row; } } </code></pre> <p>*/ /<em>NEW CODE WHERE ERROR IS THROWN</em>/</p> <p>public ViewHolder getView(int pos, View convertView, ViewGroup parent)</p> <p>{</p> <pre><code> ViewHolder holder = new ViewHolder(); Movie currMovie = movies.get(pos); if (currMovie != null) { holder.icon = (ImageView) convertView.findViewById(R.id.row_icon); holder.movieText = (TextView) convertView.findViewById(R.id.row_label); holder.votesText = (TextView) convertView.findViewById(R.id.row_subtext); holder.movieText.setText(currMovie.getName()); String votesStr = currMovie.getVotes()+" votes"; holder.votesText.setText(votesStr); Bitmap movieIcon = getMovieIcon(currMovie.getName(), currMovie.getRating()); holder.icon.setImageBitmap(movieIcon); Log.w("MVMVMVMVMVMV", "Creating row view at position "+pos+" movie "+currMovie.getName()); } return holder; } } /** Creates a unique movie icon based on name and rating */ private Bitmap getMovieIcon(String movieName, String movieRating) { int bgColor = getColor(movieName); Bitmap b = Bitmap.createBitmap(48, 48, Bitmap.Config.ARGB_8888); b.eraseColor(bgColor); // fill bitmap with the color Canvas c = new Canvas(b); Paint p = new Paint(); p.setAntiAlias(true); p.setColor(getTextColor(bgColor)); p.setTextSize(24.0f); c.drawText(movieRating, 8, 32, p); return b; } /** Construct a color from a movie name */ private int getColor(String name) { String hex = toHexString(name); String red = "#"+hex.substring(0,2); String green = "#"+hex.substring(2,4); String blue = "#"+hex.substring(4,6); String alpha = "#"+hex.substring(6,8); int color = Color.argb(Integer.decode(alpha), Integer.decode(red), Integer.decode(green), Integer.decode(blue)); return color; } /** Given a movie name -- generate a hex value from its hashcode */ private String toHexString(String name) { int hc = name.hashCode(); String hex = Integer.toHexString(hc); if (hex.length() &lt; 8) { hex = hex+hex+hex; hex = hex.substring(0,8); // use default color value } return hex; } /** Crude optimization to obtain a contrasting color -- does not work well yet */ private int getTextColor(int bg) { int r = Color.red(bg); int g = Color.green(bg); int b = Color.blue(bg); String hex = Integer.toHexString(r)+Integer.toHexString(g); hex += Integer.toHexString(b); int cDec = Integer.decode("#"+hex); if (cDec &gt; 0xFFFFFF/2) // go dark for lighter shades return Color.rgb(0, 0, 0); else { r = (r+128)%256; g = (g+128)%256; b = (b+128)%256; return Color.rgb(r,g,b); } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload