Update Header View of listView on image download complete - android

I am working on an app that is fetching data from server, I have a list View containing a header view i am downloading an image and on its completion I want to update my header view, how can i achieve this ?
I am also updating my items and they are updating successfully but I don't know how to get my header View in the adapter and update it ...

Hold reference to the text view used in the list view header.
I assume you add list view header like below:
View header = getLayoutInflater().inflate(R.layout.list_header, null);
TextView headerText = (TextView) header.findViewById(R.id.my_textview);
headerText.setText("This is my header!");
myListView.addHeaderView(header);
once the image download completes, you can notify using some interface/listeners and then update the text view.

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
ListView listView = (ListView) findViewById(R.id.list);
View header = getLayoutInflater().inflate(R.layout.header, null);
listView.addHeaderView(header);
}
}
I hope this may help you :)

I just got a solution and i am posting here so it can be useful for every one
I just simply send the reference of the headerView in the constructor of the list adapter and download image there and set the image there to its zero index item that is header view
public class MainListViewAdapter extends ArrayAdapter<DataObjects>{
private int layoutResourceID;
private ArrayList<DataObjects> dataObjects;
private Context context;
private ListHolder listHolder;
private View headerView;
private Bitmap bitmap;
private Boolean flagBitmap = false;
public MainListViewAdapter(Context context, int layoutResourceID, ArrayList<DataObjects> objects, View headerView) {
super(context, layoutResourceID, objects);
this.layoutResourceID = layoutResourceID;
this.dataObjects = objects;
this.context = context;
this.headerView = headerView;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
new DownloadImageTask(this.dataObjects.get(0).getImageURL()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);
else
new DownloadImageTask(this.dataObjects.get(0).getImageURL()).execute();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
listHolder = new ListHolder();
if(row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceID, parent, false);
row.setTag(listHolder);
} else {
listHolder = (ListHolder) row.getTag();
}
DataObjects list = dataObjects.get(position);
if(position == 0) {
ImageView imageViewHeader = (ImageView) headerView.findViewById(R.id.imageViewHeaderImage);
if(flagBitmap)
imageViewHeader.setImageBitmap(bitmap);
else
imageViewHeader.setImageResource(R.drawable.sample);
}
return row;
}
private class ListHolder {
TextView textViewDate;
ImageView imageViewMagazine;
Button buttonPreview, buttonDownload;
}
private class DownloadImageTask extends AsyncTask<Void, Void, Bitmap> {
private String URL;
public DownloadImageTask(String URL) {
this.URL = URL;
}
protected Bitmap doInBackground(Void... urls) {
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(URL).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
if(result != null) {
setThumbBitmap(result);
setThumbAvailable(true);
}
}
public void setThumbBitmap(Bitmap thumbBitmap) {
bitmap = thumbBitmap;
}
public void setThumbAvailable(boolean thumbAvailable) {
flagBitmap = thumbAvailable;
}
}
}

Related

Fail to AsyncTask update progressBar's progress when first enter the fragment ListView, then okay after scroll ListView

The progressbar cannot show progress if without scroll down and back to same position visit again, detail please check this demo:
https://www.youtube.com/watch?v=wGu8MyUHidQ&feature=youtu.be
No exception or error, maybe ListView bug or logic error, anyone have any idea?
DownloadInfo class:
private final static String TAG = DownloadInfo.class.getSimpleName();
public enum DownloadState {
NOT_STARTED,
QUEUED,
DOWNLOADING,
COMPLETE
}
private volatile DownloadState mDownloadState = DownloadState.NOT_STARTED;
private final String mFilename;
private volatile Integer mProgress;
private final Integer mFileSize;
private volatile ProgressBar mProgressBar;
public DownloadInfo(String filename, Integer size) {
mFilename = filename;
mProgress = 0;
mFileSize = size;
mProgressBar = null;
}
//Follow by getters & setters
DownloadInfoArrayAdapter Class:
public class DownloadInfoArrayAdapter extends ArrayAdapter<DownloadInfo> {
private static class ViewHolder {
TextView textView;
ProgressBar progressBar;
Button button;
DownloadInfo info;
}
public DownloadInfoArrayAdapter(Context context, List<DownloadInfo> objects) {
super(context, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
final DownloadInfo info = getItem(position);
ViewHolder holder = null;
if (null == row) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.file_download_row, parent, false);
holder = new ViewHolder();
holder.textView = (TextView) row.findViewById(R.id.downloadFileName);
holder.progressBar = (ProgressBar) row.findViewById(R.id.downloadProgressBar);
holder.button = (Button) row.findViewById(R.id.downloadButton);
holder.info = info;
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
holder.info.setProgressBar(null);
holder.info = info;
holder.info.setProgressBar(holder.progressBar);
}
holder.textView.setText(info.getFilename());
holder.progressBar.setProgress(info.getProgress());
holder.progressBar.setMax(info.getFileSize());
info.setProgressBar(holder.progressBar);
holder.button.setEnabled(info.getDownloadState() == DownloadState.NOT_STARTED);
final Button button = holder.button;
holder.button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
info.setDownloadState(DownloadState.QUEUED);
button.setEnabled(false);
button.invalidate();
FileDownloadTask task = new FileDownloadTask(info);
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
return row;
}
}
FileDownloadTask class:
#Override
protected void onProgressUpdate(Integer... values) {
mInfo.setProgress(values[0]);
ProgressBar bar = mInfo.getProgressBar();
if (bar != null) {
bar.setProgress(mInfo.getProgress());
bar.invalidate();
}
}
#Override
protected Void doInBackground(Void... params) {
Log.d(TAG, "Starting download for " + mInfo.getFilename());
mInfo.setDownloadState(DownloadState.DOWNLOADING);
for (int i = 0; i <= mInfo.getFileSize(); ++i) {
try {
Thread.sleep(16);
} catch (InterruptedException e) {
e.printStackTrace();
}
publishProgress(i);
}
mInfo.setDownloadState(DownloadState.COMPLETE);
return null;
}
#Override
protected void onPostExecute(Void result) {
mInfo.setDownloadState(DownloadState.COMPLETE);
}
#Override
protected void onPreExecute() {
mInfo.setDownloadState(DownloadState.DOWNLOADING);
}
In the fragment add click listener
lvStickerGroup = (ListView) activity.findViewById(R.id.lvStickerGroup);
List<DownloadInfo> downloadInfo = new ArrayList<DownloadInfo>();
for (int i = 0; i < 50; ++i) {
downloadInfo.add(new DownloadInfo("File " + i, 1000));
}
adapter = new DownloadInfoArrayAdapter(activity, downloadInfo);
lvStickerGroup.setAdapter(adapter);
lvStickerGroup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(activity, "bla" + i, Toast.LENGTH_SHORT).show();
}
});
//Testing all below, no luck
((BaseAdapter) adapter).notifyDataSetChanged();
lvStickerGroup.invalidate();
lvStickerGroup.invalidateViews();
lvStickerGroup.refreshDrawableState();
lvStickerGroup.post(new Runnable() {
public void run() {
for (int a = 0; a < lvStickerGroup.getCount(); a++) {
lvStickerGroup.setSelection(a);
}
for (int a = lvStickerGroup.getCount() - 1; a >= 0; a--) {
lvStickerGroup.setSelection(a);
}
}
});
I tried to programmatically scroll to bottom and back to top, same no luck, except programmatically scroll to the item position will not show in the first page when enter the fragment initially.
Besides, I tried to invalidate(), invalidateView() notifyDataSetChanged on the adapter, same problem occurs, is it possibly a ListView bug?
Nothing wrong with the code above, fully functional. I found the clue in Google ListView IO conference
https://www.youtube.com/watch?v=wDBM6wVEO70
ListView cannot wrap content, else will have bugs showing up. That why I experience such issue.

Update Images in Gridview via custom adapter

I am able to show images (set by default) in the gridview properly using a custom adapter.
However when I try to change the string[] in ForecastFragment (snippets below), the updateList function is not getting called. I would like the all the views in the grid to be recreated.
ImageAdapter.java :
public class ImageAdapter extends BaseAdapter {
public Context mContext; ImageAdapter(Context c) {
mContext = c;
String LOG_TAG = ImageAdapter.class.getSimpleName();
Log.e(LOG_TAG,"Constructor Context : "+mContext);
}
public int getCount() {
return this.eatFoodyImages.length;
}
public int size1;
public int size2;
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public void updateList(String[] string)
{
this.eatFoodyImages=string;
this.notifyDataSetChanged();
String LOG_TAG = ImageAdapter.class.getSimpleName();
Log.e(LOG_TAG, "\n\nupdatelist : " + this.eatFoodyImages);
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
size1 = (int) this.mContext.getResources().getDimension(R.dimen.image_size1);
size2 = (int) this.mContext.getResources().getDimension(R.dimen.image_size2);
imageView.setLayoutParams(new GridView.LayoutParams(size1,size2));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
String LOG_TAG = ImageAdapter.class.getSimpleName();
Log.e(LOG_TAG, "\n\n\nPosition : " + position + " Strings Array : \n\n" + eatFoodyImages[position].toString());
//System.out.println();
Picasso.with(mContext)
.
load(eatFoodyImages[position])
.
into(imageView);
return imageView;
}
// references to our images
public String[] eatFoodyImages = {
"http://i.imgur.com/rFLNqWI.jpg", //Default Random Data!!!
"http://i.imgur.com/C9pBVt7.jpg",
"http://i.imgur.com/rT5vXE1.jpg",
"http://i.imgur.com/aIy5R2k.jpg",
"http://i.imgur.com/MoJs9pT.jpg",
"http://i.imgur.com/S963yEM.jpg",
"http://i.imgur.com/rLR2cyc.jpg",
"http://i.imgur.com/SEPdUIx.jpg",
"http://i.imgur.com/aC9OjaM.jpg",
"http://i.imgur.com/76Jfv9b.jpg",
"http://i.imgur.com/fUX7EIB.jpg",
"http://i.imgur.com/syELajx.jpg",
"http://i.imgur.com/COzBnru.jpg",
"http://i.imgur.com/Z3QjilA.jpg",
};
}
ForecastFragment.java :
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
GridView gridview = (GridView) rootView.findViewById(R.id.gridview);
movieadapter = new ImageAdapter(this.getActivity());
gridview.setAdapter(movieadapter);
});
return rootView;
}
Calling updateList :
public class FetchWeatherTask extends AsyncTask<String, Void, String[]>
{//Showing only relevant code
IA.updateList(eatFoodyImages); //Here eatFoodyImages contains the new String[] to be used for image loading.
Here it goes, i have a adapter like below
public View getView(int position, View convertView, ViewGroup parent) {
Movie movie = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.movie_tile, parent, false);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.movie_image);
Picasso.with(this.getContext()).load(Constants.MOVIE_POSTER_PATH_SMALL + movie.getPosterPath()).placeholder(ContextCompat.getDrawable(this.getContext(), R.drawable.movie)).error(ContextCompat.getDrawable(this.getContext(), R.drawable.movie)).into(imageView);
return convertView;
}
which sets a tile in a gridview.
I trigger a asynctask and get the movies from API then notify the dataset which loads the movies and refreshes the view.
So essentially your notifyDataSet() should be in onPostExecute() of AsyncTask.
Here is my Async task code for better understanding.
public class CallMovieDBAPI extends AsyncTask<String, Void, List<Movie>> {
ProgressDialog dialog;
private Activity activity;
private GridView gridView;
public CallMovieDBAPI(Activity activity, GridView gridView) {
this.activity = activity;
this.gridView = gridView;
}
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(getActivity());
dialog.setMessage(activity.getString(R.string.progressDialogText));
dialog.show();
}
#Override
protected List<Movie> doInBackground(String... params) {
InputStream in = null;
String jsonResponse = null;
String apiURL = params[0];
try {
URL url = new URL(apiURL);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
in = new BufferedInputStream(urlConnection.getInputStream());
jsonResponse = getJsonObject(in);
movieList = JsonParser.parse(jsonResponse);
} catch (Exception e) {
Log.e("CallMovieDBAPI", "Exception occurred in AsyncTask - CallMovieDBAPI", e);
}
return movieList;
}
#Override
protected void onPostExecute(List<Movie> movies) {
Log.d("Popular Movies", "Got data successfully");
if (dialog.isShowing()) {
dialog.dismiss();
}
movieAdapter = new MovieAdapter(activity, movieList);
gridView.setAdapter(movieAdapter);
movieAdapter.notifyDataSetChanged();
if (isTablet) {
((MovieClickedCallback) getActivity()).onMovieClicked(0, movieAdapter);
}
}
Let me know if this solves.

different items in listview

I'm trying to create a listview with different items within it. I want to use a switch statement to do this but I'm struggling to produce this I have created this with one XML layout how can I use multiple XML Layouts. I've attached my code - thank you in advance!
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_postlist);
this.generateDummyData();
ListView listView = (ListView) this.findViewById(R.id.postListView);
final PostItemAdapter itemAdapter = new PostItemAdapter(getApplicationContext(), this.generateDummyData());
listView.setAdapter(itemAdapter);
}
private ArrayList<PostData> generateDummyData() {
PostData data = null;
ArrayList<PostData> listData = new ArrayList<PostData>();
for (int i = 0; i < 20; i++) {
data = new PostData();
data.postTitle = "Person " + (i + 1)
+ " Name Surname";
data.postThumbUrl = null;
listData.add(data);
}
return listData;
}
// Get dummy data for Activity Feed
class PostDataTask extends AsyncTask<Integer, Void, Void> {
ArrayList<PostData> recentTracks;
#Override
protected Void doInBackground(Integer... page) {
try {
// Get a page of 15 tracks
// Simplified - getPage accepts 'page' and 'limit' parameters and returns a Collection<Track>
recentTracks = new ArrayList<PostData>();
Thread.sleep(3000);
PostData data = null;
for (int i = 0; i < 10; i++) {
data = new PostData();
data.postTitle = "Person " + (i + 1)
+ " Name is the Post Title from RSS Feed";
data.postThumbUrl = null;
recentTracks.add(data);
}
} catch (Exception e) {
}
return null;
}
#Override
protected void onPostExecute(Void result) {
ListView listView = (ListView) findViewById(R.id.postListView);
PostItemAdapter adapter = ((PostItemAdapter) ((HeaderViewListAdapter) listView.getAdapter()).getWrappedAdapter());
if (adapter == null) {
adapter = new PostItemAdapter(getApplicationContext(), recentTracks);
listView.setAdapter(adapter);
} else {
adapter.addAll(recentTracks);
adapter.notifyDataSetChanged();
}
}
}
public class PostItemAdapter extends ArrayAdapter<PostData> {
private final Context context;
private final ArrayList<PostData> items;
private int currentPage = 0;
public PostItemAdapter(Context context, ArrayList<PostData> recentTrackArrayList) {
super(context, 0, recentTrackArrayList);
this.context = context;
this.items = recentTrackArrayList;
}
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.postitem,
parent, false);
}
final PostData item = items.get(position);
rowView = convertView;
ImageView thumbImageView = (ImageView) rowView
.findViewById(R.id.postThumb);
if (items.get(position).postThumbUrl == null) {
thumbImageView.setImageResource(R.drawable.postthumb_loading);
}
TextView postTitleView = (TextView) rowView
.findViewById(R.id.postTitleLabel);
postTitleView.setText(items.get(position).postTitle);
if (position == getCount() - 1) {
currentPage++;
new PostDataTask().execute(currentPage);
}
return convertView;
}
}
}
At first use ViewHolder pattern.
At second make interfase of Item which will include two methods:
getType() - which return the type of your item;
getView(LayoutInflater inflater, View convertView) - which return the view of row. Inside this method create ViewHolder instance, then inflate View parameter to VH and do some action with result view.
. Then create 2 (or how much u need) classes which would implement Item. Define methods. Then in ListAdapter in getView() call getView() of items and return it to list.
Using the view holder pattern won't work if your items are completely different.
For every item, you need to check what kind of item you want to display and inflate a different view depending on it (for example, using a switch as you said)

ListView shows wrong placement of images taken from URL

My listview doesn't show exact placement of images on its individual custom adapters. I am trying to load the images using AsyncTask. Sometimes the images duplicates and I don't have any idea why. Here is my ArrayAdapter code:
public class NewsPromoAdapter extends ArrayAdapter<NewsPromosDTO> {
private final Context context;
List<NewsPromosDTO> npList;
LayoutInflater inflater;
Bitmap src;
public NewsPromoAdapter(Context context, List<NewsPromosDTO> npList) {
super(context, R.layout.news_and_promos, npList);
this.context = context;
this.npList = npList;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public static class ViewHolder {
TextView nameText;
TextView date1Text;
ImageView imageView;
ProgressBar prg;
LoadImage loadImg;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.news_and_promos, parent,
false);
holder = new ViewHolder();
holder.nameText = (TextView) convertView
.findViewById(R.id.newspromo_name);
holder.date1Text = (TextView) convertView
.findViewById(R.id.newspromo_date);
holder.imageView = (ImageView) convertView
.findViewById(R.id.newspromo_imageView);
holder.prg = (ProgressBar) convertView
.findViewById(R.id.progressBar1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
holder.loadImg.cancel(true);
}
if (!(npList.get(position).getName().equals(""))) {
holder.nameText.setVisibility(View.VISIBLE);
holder.nameText.setText(npList.get(position).getName());
} else {
holder.nameText.setVisibility(View.GONE);
}
if (!(npList.get(position).getDate().equals(""))) {
holder.date1Text.setVisibility(View.VISIBLE);
holder.date1Text.setText(npList.get(position).getDate());
} else {
holder.date1Text.setVisibility(View.GONE);
}
if (!(npList.get(position).getImageURL().equals(""))) {
holder.imageView.setVisibility(View.VISIBLE);
holder.loadImg = new LoadImage(holder.imageView, npList.get(
position).getImageURL(), holder);
holder.loadImg.execute();
} else {
holder.imageView.setVisibility(View.GONE);
holder.prg.setVisibility(View.GONE);
}
return convertView;
}
class LoadImage extends AsyncTask<Object, Void, Bitmap> {
ViewHolder viewH;
private ImageView imv;
private String url;
public LoadImage(ImageView imv, String imageURL, ViewHolder viewH) {
this.imv = imv;
this.url = imageURL;
this.viewH = viewH;
}
#Override
protected Bitmap doInBackground(Object... params) {
BitmapFromURL bmpURL = new BitmapFromURL();
src = bmpURL.getBitmapFromURL(url);
return src;
}
#Override
protected void onPostExecute(Bitmap result) {
// imv.setImageBitmap(result);
// viewH.prg.setVisibility(View.GONE);
viewH.prg.setVisibility(View.GONE);
imv.setImageBitmap(result);
}
}
}
Here is the Activity that handles the ADAPTER. I am implementing the Endless listview approach that some big sites uses. Like Facebook, Twitter, etc..New Items loaded into the listview when scrolling into the bottom. I am not sure if I did it well.
public class ListNewsPromoActivity extends Activity {
List<NewsPromosDTO> npList;
String url, user_email;
Bundle extras;
int user_points;
List<Integer> npIDs;
private ProgressDialog progressDialog;
BitmapFromURL bmpURL;
JSONArray users = null;
String TAG_ID = "id";
String TAG_NAME = "name";
String TAG_PHOTO = "photo";
String TAG_DATE = "date";
String TAG_DESC = "description";
ListView list;
NewsPromoAdapter newsAdapter;
boolean loadingMore = false;
private Runnable returnRes;
int itemsPerPage = 3;
int currentIndex = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.news_promo_listview);
list = (ListView) findViewById(R.id.mainView);
extras = getIntent().getExtras();
if (extras != null) {
user_points = extras.getInt("user_points");
user_email = extras.getString("user_email");
}
url = getString(R.string.app_url_news_promos);
npIDs = new ArrayList<Integer>();
npList = new ArrayList<NewsPromosDTO>();
View footerView = ((LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
R.layout.footer, null, false);
newsAdapter = new NewsPromoAdapter(ListNewsPromoActivity.this, npList);
list.addFooterView(footerView);
list.setAdapter(newsAdapter);
list.setOnScrollListener(new OnScrollListener() {
// useless here, skip!
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// what is the bottom item that is visible
int lastInScreen = firstVisibleItem + visibleItemCount;
// is the bottom item visible & not loading more already ? Load
if ((lastInScreen == totalItemCount) && !(loadingMore)) {
Thread thread = new Thread(null, getRunnable());
thread.start();
}
}
});
// Runnable to load the items
// Since we cant update our UI from a thread this Runnable takes care of
// that!
returnRes = new Runnable() {
#Override
public void run() {
// Loop thru the new items and add them to the adapter
for (NewsPromosDTO np : npList) {
System.out.println(np.getName() + " andito ako!!");
newsAdapter.add(np);
}
newsAdapter.notifyDataSetChanged();
// Done loading more.
loadingMore = false;
}
};
}
protected class loadNewsPromo extends AsyncTask<Void, Void, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(Void... params) {
// Creating JSON Parser instance
loadingMore = true;
JSONParser jParser = new JSONParser();
JSONObject json;
if (jParser.checkServer(url)) {
try {
// Getting Array of UserNews
json = jParser.getJSONFromUrl(url);
users = json.getJSONArray(user_email);
// looping through All UserNews
for (int i = currentIndex; i < itemsPerPage; i++) {
if (itemsPerPage == i) {
// currentIndex--;
break;
} else {
JSONObject c = users.getJSONObject(i);
// Storing each json item in variable
NewsPromosDTO npDTO = new NewsPromosDTO();
npDTO.setImageURL(c.getString(TAG_PHOTO));
npDTO.setId(c.getInt(TAG_ID));
npDTO.setName(c.getString(TAG_NAME));
npDTO.setDate(c.getString(TAG_DATE));
npDTO.setDescription(c.getString(TAG_DESC));
npList.add(npDTO);
currentIndex++;
}
}
} catch (JSONException e) {
e.printStackTrace();
return null;
} finally {
}
}
}
return null;
}
#Override
protected void onPostExecute(String result) { //
// progressDialog.dismiss();
loadingMore = false;
list();
}
}
public void list() {
// add the footer before adding the adapter, else the footer will not
// load!
View footerView = ((LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
R.layout.footer, null, false);
newsAdapter = new NewsPromoAdapter(ListNewsPromoActivity.this, npList);
list.addFooterView(footerView);
list.setAdapter(newsAdapter);
}
public Runnable getRunnable() {
Runnable loadMoreListItems = new Runnable() {
#Override
public void run() {
// Set flag so we cant load new items 2 at the same time
loadingMore = true;
// Reset the array that holds the new items
// Simulate a delay, delete this on a production environment!
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
// Get 15 new listitems
JSONParser jParser = new JSONParser();
JSONObject json;
npList = new ArrayList<NewsPromosDTO>();
if (jParser.checkServer(url)) {
try {
// Getting Array of Contacts
json = jParser.getJSONFromUrl(url);
users = json.getJSONArray(user_email);
// looping through All Contacts
// if (itemsPerPage > users.length() - currentIndex) {
// itemsPerPage = users.length();
// }
npList = new ArrayList<NewsPromosDTO>();
System.out.println(users.length() + " Laman ng users");
int counter = 0;
for (int i = currentIndex; i < users.length(); i++) {
if (itemsPerPage == counter) {
break;
} else {
JSONObject c = users.getJSONObject(i);
// Storing each json item in variable
NewsPromosDTO npDTO = new NewsPromosDTO();
npDTO.setImageURL(c.getString(TAG_PHOTO));
npDTO.setId(c.getInt(TAG_ID));
npDTO.setName(c.getString(TAG_NAME));
npDTO.setDate(c.getString(TAG_DATE));
npDTO.setDescription(c.getString(TAG_DESC));
npList.add(npDTO);
currentIndex++;
}
counter++;
}
} catch (JSONException e) {
e.printStackTrace();
} finally {
}
}
// Done! now continue on the UI thread
runOnUiThread(returnRes);
}
};
return loadMoreListItems;
}
}

Using AsyncTask to load Images in ListView

I have one ListView which can hold an image. It depends if image exists or not in SDCARD.
Here my example code:
public class MainActivity extends Activity {
ListView mListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mListView = new ListView(this);
setContentView(mListView);
String[] arr = new String[] {
"/example/images/1.jpg", "/example/images/2.jpg",
"/example/images/3.jpg", "/example/images/4.jpg",
"/example/images/5.jpg", "/example/images/6.jpg",
"/example/images/7.jpg", "/example/images/8.jpg",
"/example/images/9.jpg", "/example/images/1.jpg",
"/example/images/2.jpg", "/example/images/3.jpg",
"/example/images/4.jpg", "/example/images/5.jpg",
"/example/images/6.jpg", "/example/images/7.jpg",
"/example/images/8.jpg", "/example/images/9.jpg",
"/example/images/1.jpg", "/example/images/2.jpg",
"/example/images/3.jpg", "/example/images/4.jpg",
"/example/images/5.jpg", "/example/images/6.jpg",
"/example/images/7.jpg", "/example/images/8.jpg",
"/example/images/9.jpg", "/example/images/1.jpg",
"/example/images/2.jpg", "/example/images/3.jpg",
"/example/images/4.jpg", "/example/images/5.jpg",
"/example/images/6.jpg", "/example/images/7.jpg",
"/example/images/8.jpg", "/example/images/9.jpg"};
List<String> list = Arrays.asList(arr);
MyAdapter adapter = new MyAdapter(this, R.layout.listitem_imv, list);
mListView.setAdapter(adapter);
}
class MyAdapter extends ArrayAdapter<String>{
List<String> mList;
LayoutInflater mInflater;
int mResource;
public MyAdapter(Context context, int resource,
List<String> objects) {
super(context, resource, objects);
mResource = resource;
mInflater = getLayoutInflater();
mList = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if(convertView == null){
view = mInflater.inflate(mResource, null);
}else{
view = convertView;
}
ImageView imageView = (ImageView) view.findViewById(R.id.imv);
TextView textView = (TextView) view.findViewById(R.id.txv);
imageView.setTag(mList.get(position));//tag of imageView == path to image
new LoadImage().execute(imageView);
textView.setText(mList.get(position).toString());
return view;
}
}
class LoadImage extends AsyncTask<Object, Void, Bitmap>{
private ImageView imv;
private String path;
#Override
protected Bitmap doInBackground(Object... params) {
imv = (ImageView) params[0];
path = imv.getTag().toString();
Bitmap bitmap = null;
File file = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() + path);
if(file.exists()){
bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
}
return bitmap;
}
#Override
protected void onPostExecute(Bitmap result) {
if(result != null && imv != null){
imv.setVisibility(View.VISIBLE);
imv.setImageBitmap(result);
}else{
imv.setVisibility(View.GONE);
}
}
}
}
The 'sdcard/example/images' directory has the images: 1.jpg, 2.jpg, 3.jpg, 4.jpg, 6.jpg, 7.jpg and 9.jpg.
the expected result is:
But, if I scroll the list quickly, some images are inserted in the wrong items.
It happens due to use of convertView in getView() method.
If I use the following code, the code works fine:
//if(convertView == null){
// view = mInflater.inflate(mResource, null);
//}else{
// view = convertView;
//}
view = mInflater.inflate(mResource, null);
When list scrolled quickly, two asyncTasks can reference one same View, due to use of convertView.
How Can I cancel AsyncTask when the View is no longer visible?(and is useb by another item of ListView)
edit
#Override
protected void onPostExecute(Bitmap result) {
if(result != null && imv != null){
if(imv.getTag().equals(path)){
imv.setVisibility(View.VISIBLE);
imv.setImageBitmap(result);
}else{
imv.setVisibility(View.GONE);
}
}else{
imv.setVisibility(View.GONE);
}
}
You can send in the ImageView to the task constructor and keep a reference to the image path there. Now at onPostExecute, check if the current tag of the ImageView is the same as the one that you started with. If yes, then set the image. If no, don't do anything.
However, this means that the image will be downloaded in any case. You'll just not set the wrong image on the view.
EDIT:
First pass the ImageView to the task constructor:
new LoadImage(imageView).execute()
Then save a reference to the ImageView and image path in LoadImage constructor. It is important to save the path in the constructor and not in doInBackground to ensure that we don't run into multi threading problems. Then at onPostExecute we check the current path.
class LoadImage extends AsyncTask<Object, Void, Bitmap>{
private ImageView imv;
private String path;
public LoadImage(ImageView imv) {
this.imv = imv;
this.path = imv.getTag().toString();
}
#Override
protected Bitmap doInBackground(Object... params) {
Bitmap bitmap = null;
File file = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() + path);
if(file.exists()){
bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
}
return bitmap;
}
#Override
protected void onPostExecute(Bitmap result) {
if (!imv.getTag().toString().equals(path)) {
/* The path is not same. This means that this
image view is handled by some other async task.
We don't do anything and return. */
return;
}
if(result != null && imv != null){
imv.setVisibility(View.VISIBLE);
imv.setImageBitmap(result);
}else{
imv.setVisibility(View.GONE);
}
}
}
This Android Developers Blog post will give you a complete reference project for this complete with caching. Just replace the Http access code with SD card file reads.
I hope this helps.
After lot of search I have this working solution.
public class CustomAdapter extends ArrayAdapter<String>{
/*
public CustomAdapter(Context context , String[] video) {
super(context,R.layout.custom_row, video);
}
*/
private final Activity context;
private final String[] video;
static class ViewHolder {
public TextView videoTitle;
public ImageView videoThumbnail;
public int position;
public String path;
}
public CustomAdapter(Activity context, String[] video) {
super(context, R.layout.custom_row, video);
this.context = context;
this.video = video;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater videoInflator = LayoutInflater.from(getContext());
View customView = videoInflator.inflate(R.layout.custom_row, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.position = position;
viewHolder.path = video[position];
viewHolder.videoTitle = (TextView) customView.findViewById(R.id.videoTitle);
viewHolder.videoThumbnail = (ImageView) customView.findViewById(R.id.videoThumbnail);
//rowView.setTag(viewHolder);
//}
customView.setTag(viewHolder);
final String videoItem = video[position];
int index=videoItem.lastIndexOf('/');
String lastString=(videoItem.substring(index +1));
index = lastString.indexOf(".mp4");
lastString=(lastString.substring(0,index));
viewHolder.videoTitle.setText(lastString);
new AsyncTask<ViewHolder, Void, Bitmap>() {
private ViewHolder v;
#Override
protected Bitmap doInBackground(ViewHolder... params) {
v = params[0];
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(videoItem, MediaStore.Images.Thumbnails.MINI_KIND);
return thumb;
}
#Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if (v.position == position) {
// If this item hasn't been recycled already, hide the
// progress and set and show the image
v.videoThumbnail.setImageBitmap(result);
}
}
}.execute(viewHolder);
return customView;
}
}
Maybe you should try:
view = mInflater.inflate(mResource,parent,null);
Check this blog it explains the similar issue:
http://www.doubleencore.com/2013/05/layout-inflation-as-intended/
What I would do (unless you have thousands of images):
1. create a data structure - a simple class holding a String name to be displayed and a bitmap
2. create an adapter for it
3. in the getView method assign the correct bitmap to the correct ImageView.
In your case though you can create a similar data structure but holding not a bitmap but an AsyncTask. Anyway you need to bind the asynctask to the string into one item. An array (or arraylist) of such items will be fed to your adapter. Displayed will be an imageview and a textview.
AsyncTask can be cancelled with cancel().
Hey I found the solution to this problem just use following function instead of your function
#Override
protected void onPostExecute(Bitmap result) {
if (!imv.getTag().toString().equals(rec_id)) {
return;
}
if(result != null && imv != null){
int index = id.indexOf(imv.getTag().toString());
if(list.getFirstVisiblePosition()<=index && index<=list.getLastVisiblePosition())
{
imv.setVisibility(View.VISIBLE);
imv.setImageBitmap(result);
}
}else{
imv.setImageBitmap(icon);
imv.setVisibility(View.GONE);
}
}
Here list is the object of listview. Just pass your list view object to your adapter and paste this function instead of your onPostExecute function.

Categories

Resources