Activity refresh command from custom listview - android

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();

Related

Requesting logic for listview fragment

I'm making an activity with a listView fragment in it. I am having a button click on the MainActivity load items for an rss feed that populate the listview fragment.
My question is, how should I go about setting it up? I already have the code for it.
My plan now is to have the button click on MainActivity point to my method in my RSSFragment which extends ListFragment. I'm confused about some things like which activity to pass here:
#Override
protected void onPostExecute(Exception s) {
super.onPostExecute(s);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, titles);
lvRss.setAdapter(adapter);
progressDialog.dismiss();
}
}
Should I use getActivity()? or Pass MainActivity or RSSFragment.this?
Also, how do I set list adapter to a listview fragment? Imagine lvRss isn't there.
I know it's a long question.
Thanks.
Should I use getActivity()? or Pass MainActivity or RSSFragment.this?
If you are in Activity the use this or Activity.this . IF you are in Fragment the use getActivity().
Also, how do I set list adapter to a listview fragment? Imagine lvRss isn't there.
Use setListAdapter method setting Adapter to ListFragment

How to refresh two listview at one moment in Android?

I have two listview, like listview_1 and listview_2. I wanna refresh the listview_2 while listview_1 is refreshed.
My code like this:
public void updateTwoListView() {
listview_1.getAdapter().notifyDataSetChanged();
listview_2.getAdapter().notifyDataSetChanged();
}
But it don't work, listview_1 can refresh but the listview_2 can't.
And at that moment what I found is that listview_1 was on focus.
And then I tried to set focus to other views before ran the method, both of them didn't refresh. It likes to refresh a listview only if the listview has focus.
What's more I found that when I called the method to refresh, listview_2 didn't, and then I set focus to listview_2, refreshed itself!
So, What all I want to ask is:
How to refresh two listview at one moment in Android?
What's more code:
//init two listview there
public void init() {
listview_1 = (ListView)findViewById(R.id.listView1);
listview_2 = (ListView)findViewById(R.id.listView2);
adapter1 = new MyListViewAdapter(mContext);
adapter2 = new MyListViewAdapter(mContext); //I have tried use different adapter, that also didn't work.
listview_1.setAdapter(adapter1);
listView_2.setAdapter(adapter2);
}
the real code of upside snippet is:
public void updateTwoListView(int currentPosition) {
adapter1.updateCurPos(currentPosition);
adapter2.updateCurPos(currentPosition);
}
and in MyListViewAdapter.java:
public void updateCurPos(int currentPosition) {
mCurrentPos = currentPosition;
notifyDataSetChanged();
}
And I will call method like listViewManager.updateTwoListView(1) outside to refresh.
Any reply is appreciated.
You have called listview_1 twice. Just change one of them to listview_2 as below:
public void updateTwoListView() {
listview_1.getAdapter().notifyDataSetChanged();
listview_2.getAdapter().notifyDataSetChanged();
}
It seems the problem of your code is that you call getAdapter().
Sets the data behind this ListView. The adapter passed to this method may be wrapped by a WrapperListAdapter, depending on the ListView features currently in use. For instance, adding headers and/or footers will cause the adapter to be wrapped.
Source: http://developer.android.com/reference/android/widget/ListView.html#setAdapter(android.widget.ListAdapter)
The solution is save your Adapter as member variable in your activity and call the notifyDataSetChanged()from there.
See more on this question's answer: https://stackoverflow.com/a/31893525/2742759

Android, How do i Update the selected item in my listview from my database?

Here's my code so far, but the application crashes when I press the update button.
I want to update a selected item in my list, I have already created the update activity that will allow me to load the values on my database but I can't figure out how to load the value of selected item in list.
{
ArrayAdapter<String> ard=new ArrayAdapter<String> (this,android.R.layout.simple_list_item_single_choice,list);
lv.setAdapter(ard);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
btnupdate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
SparseBooleanArray sba = lv.getCheckedItemPositions();
Intent intent = new Intent(HomeworkInfo.this, UpdateHomework.class);
startActivity(intent);
finish();
}
});
}
When an ArrayAdapter is constructed, it holds the reference for the List that was passed in. If you were to pass in a List that was a member of an Activity, and change that Activity member later, the ArrayAdapter is still holding a reference to the original List. The Adapter does not know you changed the List in the Activity.
Your choices are:
Use the functions of the ArrayAdapter to modify the underlying List (add(), insert(), remove(), clear(), etc.)
Re-create the ArrayAdapter with the new List data. (Uses a lot of resources and garbage collection.)
Create your own class derived from BaseAdapter and ListAdapter that allows changing of the underlying List data structure.
Use the notifyDataSetChanged() every time the list is updated. To call it on the UI-Thread, use the runOnUiThread() of Activity. Then, notifyDataSetChanged() will work.
I hope this helps you ..
Happy coding !!

Update gridview after download with asynctask

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

Update ListView from user input after setContentView call

I want to show an empty list view, which is then populated by user input. I have the UI flow working, and I populate a list of my custom objects after the user enters some information via a view which is invoked through setContentView (i.e. no a new Activity).
I take the input and add it to a list, which I want to be summarised on the ListView. However, whenever I add to the list and/or the ArrayAdapter and call adapter.notifyDataSetChanged() it does not do what I want. The ListView is still empty. Argh! It's driving me insane!
#Override
public void onCreate(Bundle blah) {
ListView listView = (ListView) findViewById(R.id.results_list);
listView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mnu_add:
final Activity act = this;
setContentView(R.layout.record_details);// the sub-view that takes the user input
// the button on the form to 'add' details:-
((Button) findViewById(R.id.recored_details_add_btn))
.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
// get input from widgets
list.add(someObject);
((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged();
setContentView(R.layout.list_view);
}
}
);
((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged();
break;
}
return true;
}
Please, save me from my misery and inform me of my stupidty?
Thanks in advance.
public void onClick(View v) {
// get input from widgets
list.add(someObject);
((ArrayAdapter<Object>) listView.getAdapter()).notifyDataSetChanged();
setContentView(R.layout.list_view);
Is it possible that this setContentView in the onClick handler is creating a new instance of the list view widget (with no adapter) or reinitializing the list view (clearing the adapter)?
Try putting something in the list initially in onCreate and then see if it disappears when you hit the button.
I haven't seen any code (although I'm a relative newbie) that switches views within the activity's lifetime to bring up essentially bring up different pages - most use a separate activity.
Edit:
OP asks:
Thanks...So how can I get what I want? The list I'm backing the adapter with is static; should I just use activities instead and rely on onCreate loading from the static field?
Some options:
Use separate activities
Re-associate the adapter (call setAdapter again) - probably a bad idea
Declare both layouts in the same file. You'll hide one and unhide the other to switch between views rather can calling setContentView. This is similar to how ListView layout works (one for when the list is empty and one for when it is not). I think I've seen an example of this somewhere on the net, but I don't have a reference right now.
You could relaunch the same activity by using Intent.FLAG_ACTIVITY_SINGLE_TOP flag while creating the intent and override the onNewIntent() method.
Inside the onNewIntent() you create the adapter with updated data and call setAdapter.
I think this will give you the intended behaviour.

Categories

Resources