Update gridview after download with asynctask - android

How to update a button within a GridView after downloading file with AsyncTask.
My MainActivity load a ArrayList data with "filename, imagename and URLFile", then calls the adapter to populate the items of GridView:
gridView.setAdapter(new AdapterFiles(getApplicationContext(), mArray);
My adapter extends BaseAdapter, and, for each item, adds name, image and a button.
If the PDF file does not exist, the application adds the button with the name of "DOWNLOAD":
if (!pdfFile.exists(){
viewHolder.imageButon.setImageResource(R.drawable.btn_download);
} else {
viewHolder.imageButon.setImageResource(R.drawable.btn_read);
}
When clicked in this button, a new AsyncTask class is called to download the file:
viewHolder.imageButon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FileDownloadTask task = new FileDownloadTask(item, localdownload);
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
The problem is that, I have not found a way to reload the adapter, and "re-check" that the file exists and change the button with the name "READ".
I need re-call my adapter onPostExecute of FileDownloadTask, but I do not know the best way to do this.
PS: I believe that notifyDataSetChanged() is not the solution, because I don't change my ArrayList.
Thank you very much

Related

Update ListView in ArrayAdapter when a row button clicked

I have a class OwnAdapter which extends the ArrayAdapter.
Data to it is passed in a arrayList.
There is a custom layout contaning 2 TextViews and 2 Buttons.
I have defined the onClick actions on the buttons in the getView method of the adapter itself.
When the button of the rows are clicked there are some changes made to the database.Now I am not getting how to update the listview when either of the 2 buttons are clicked and show the new data.
bAttend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//works fine
upDateAttend(position);
//code for updating view-What to add here?
notifyDataSetChanged();
}
});
after updating your arraylist make this line
youradapter.notifyDataSetChanged();
if you updating the arraylist in the adpater just add notifyDataSetChanged();
see this link ,talk about recyclerview but will help you
https://guides.codepath.com/android/using-the-recyclerview#notifying-the-adapter

Android - Update GridView on button click

I am trying to set up a gridview that will refresh when a new search is executed.
this is my call to set up the gridview
final GridElement_Results adapter = new GridElement_Results(this, savedInstanceState, track_name, album_name, track_num, album_id, album_link);
grid = (GridView)findViewById(R.id.grid_results);
grid.setAdapter(adapter);
As you can see, I am passing it a bunch of arrays.
On a button click, the function getTracks() is called which gets new data from the server. The arrays are updated with the new data, which I have verified is working.
icon_activesearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getTracks("newsearchterm");
adapter.notifyDataSetChanged();
grid.invalidateViews();
}
}
);
And then I call adapter.notifyDataSetChanged() and grid.invalidateViews() to update and redraw the grid. But obviously I'm doing something wrong here.
Is it because I'm setting the adapter as final? To access it within the onClick method I need to set it to final.
What should I change to get it to work.
you should wait until new data fetch then notify adapter but you call notify just after getTracks() without any delay

Remove all items in custom BaseAdapter and add new ones

I'm getting trouble by doing some simple task with my adapter.
I've class that implementing BaseAdapter, associated to a ListView.
After I've created this adapter and showed it to the user, if some event occours, I would remove all elements inside adapter, and adding new ones.
I have implement this method inside my BaseAdapter:
public void clearAndBuild(ArrayList<PictureObject> new_pics, String some_text) {
pics.clear();
pics.addAll(new_pics);
this.hashtag = hashtag;
//do something else
}
(where pics is an ArrayList containing items to put inside ListView). In my activity, fo refresh all i call:
adapter.clearAndBuild(pics, tag);
adapter.notifyDataSetChanged();
but it doesn't work.
After i call that method, old items inside listView are removed, but new ones is not showed (i get an empty listview).
What's wrong?
the only reason could be that you are reusing the same references for pics and new_pics. Make a deep copy of new_pics before calling clear() on pics
you can do this:
public void clearAndBuild(ArrayList<PictureObject> new_pics, String some_text) {
pics. = new ArrayList<PictureObject>(new_pics);
this.hashtag = hashtag;
//do something else
}

Activity refresh command from custom listview

Environment: Android 2.2+
I have an Activity. The Activity creates a custom listview with its adapter and populates it. When the user clicks on an item (an ImageView thumbnail) in the list, a context menu is shown. The repercussion of selection is that the view of Activity needs to be refreshed.
My question is: How can I send a command from the onClick of the custom adapter to the Activity asking to refresh itself?
Activity
...
ListView lv;
lv = (ListView) findViewById(R.id.lvlist);
lv.setAdapter(null);
MyCustomAdapter lvAdapter = new MyCustomAdapter(...);
lv.setAdapter(lvAdapter);
lv.setOnItemClickListener(myClickListener);
...
MyCustomAdapter (There is a thumbnail ImageView inside each list item called ivLogo)
...
ivLogo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Carry out some tasks
// At this point inform Activity that it needs to refresh
}
});
It must be noted that I'm not looking for anything in myClickListener. I want a signal from the thumbnail's onClickListener.
I scoured the internet, but could not find anything helpful for this specific case.
Any help/pointers will be much appreciated.
TIA
-sph
add this line to your onClick()
lvAdapter.notifyDataSetChanged();
you write his code whenever you need adpater refreshed...its worked
lvAdapter .notifyDataSetChanged();

how can we dynamically add only 20 images from webservice in the grid view

I want 20 images on one page and upon clicking on the next button, I will get another images on the same page.
That means, like in email programs, we have "older" & "oldest" by clicking "older" we will get the next emails.
same way i want images here by XML Parser.
GridView gView = (GridView)findViewById(R.id.gridview);
btnNext = (Button)findViewById(R.id.btnNext);
btnPrevious = (Button)findViewById(R.id.btnPrevious);
adap = new EfficientAdapter(this);
gView.setAdapter(adap);
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
adap.notifydatasetchanged();/*asking for create notifydatasetchanged()*/
EfficientAdapter adap = new EfficientAdapter(this);/* shows the error and asking for create constructor EfficientAdapter(OnClickListener)*/
gView.setAdapter(adap);
}
});
First and above all if you want to parse all the images from XML together at at time and show them, It will be a bed approach , Instead let your server file handle this i.e. each time you hit a url it should return you 20 records .
For example
String urlToHit=Config.basicUrl+"serverfile.php?showperpage="+Config.showPerPage+"&pageindex="+Config.currentPage;
where showPerPage is the number of records you want in per page and currentPage is the index of the page (it will be incremented for each new page).
Now suppose you have a button and now onClick of button parse the url(hit the url) and call setAdapter:
gridview.setAdapter(new ImageAdapter(getApplicationContext()));
and return 20 by getCount() of your BaseAdapter.
Hope this will solve your problem.
The error you mark in the code (EfficientAdapter adap = new EfficientAdapter(this);) is because this, in the context of that callback, refers to the new OnClickListener, instead of the view.
You can either create a new one as EfficientAdapter adap = new EfficientAdapter(v); or, much better, reuse the one you created before:
Remove the two lines
EfficientAdapter adap = new EfficientAdapter(this);/* shows the error and asking for create constructor EfficientAdapter(OnClickListener)*/
gView.setAdapter(adap);
and use the onClick callBack to ask the server for the next 20 images. Fill those images to your adapter and then adap.notifyDataSetChanged() (mind the Caps) "Notifies the attached View that the underlying data has been changed and it should refresh itself."

Categories

Resources