I am currently pulling 15 tweets into an ArrayList using a handler, and then passing the finished ArrayList to the updateUI method. So far, it worked perfectly, but when my ListView is full, the scroll experience is very volatile. When I remove the image link from my TwitterAdapter, the ListView has a fast / smooth user interface. I suspect my adapter is trying to reload every graphic when I scroll, but I can’t get the debugger to connect to this method to prove it.
Here is the key code inside the Activity:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Theme_BambooZen);
this.setContentView(R.layout.twitter_layout);
myListView = (ListView) findViewById(R.id.TwitterListView);
mHandler.removeCallbacks(mUpdateAdapterTask);
mHandler.postDelayed(mUpdateAdapterTask, 1000);
}
private Runnable mUpdateAdapterTask = new Runnable() {
public void run() {
try{
ArrayList<Tweet> tweets = getTweets("Formula1", 1);
updateUI(tweets);
mHandler.postDelayed(this, 45000);
}
catch (Exception e)
{
e.toString();
}
}
};
public void updateUI(ArrayList<Tweet> tweets){
adapter = new TwitterAdapter(this, R.layout.twitter_listitem, tweets);
myListView.setAdapter(adapter);
}
Item Adapter:
public class TwitterItemAdapter extends ArrayAdapter<Tweet> {
public TwitterItemAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public static Bitmap getBitmap(String bitmapUrl) {
try {
URL url = new URL(bitmapUrl);
return BitmapFactory.decodeStream(url.openConnection() .getInputStream());
}
catch(Exception ex) {return null;}
}
}
Twitter adapter:
public class TwitterAdapter extends ArrayAdapter<Tweet>{
private ArrayList<Tweet> tweets;
public TwitterAdapter(Context context, int textViewResourceId, ArrayList<Tweet> tweets) {
super(context, textViewResourceId, tweets);
this.tweets = tweets;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.twitter_listitem, null);
}
Tweet tweet = tweets.get(position);
if (tweet != null) {
TextView username = (TextView) v.findViewById(R.id.username);
TextView message = (TextView) v.findViewById(R.id.message);
ImageView image = (ImageView) v.findViewById(R.id.avatar);
if (username != null) {
username.setText(tweet.username);
}
if(message != null) {
message.setText(tweet.message);
}
if(image != null) {
image.setImageBitmap(TwitterItemAdapter.getBitmap(tweet.image_url));
}
}
return v;
}
}
source
share