Refresh listview onpost execute asynctask - android

i know this question has been posted multiple times and i browsed almost all of them but there is not result, i am performing a deleting an item from mysql database but it is not refreshing, here is the code of the onclicklistener and the button:
onClick listener:
holder.void_button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
adapter = new CustomListViewVoidAdapter(context,R.layout.mytemp, items);
item_selected= items.get(position);
new DeleteOrder().execute();
}});
vi.setTag(holder);
}
OnPostExecute from AsyncTask:
protected void onPostExecute(String unused){
adapter.remove(item_selected);
adapter.notifyDataSetChanged();
}
the adapter is instatiated globally, can you please check where the problem might be?
it is not returning any error, just deleting the item and not refreshing.
Regards
Ralph

Throwing it out there, but, have you tried adapter.notifyDataSetInvalidated();? That forces an update.
Also, put the code in the asynctask!
Like such:
protected void onPostExecute() {
adapter.notifyDataSetChanged();
adapter.notifyDataSetInvalidated();
}

You better set the adapter again to the list view in onPostExecute with the new values. And you don't need to call notifyDataSetChangedin this case. Also don't re-intialize the adapter in onClick, this is not neccessary.

Add below line in postExecute.
if(adapter != null) {
adapter = new CustomListViewVoidAdapter(context,R.layout.mytemp, items);
YourListviewObject.setAdapter(adapter);
}

Related

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

Android- Listview- Buttons

I've a Listview with add button when i click add new row of buttons
created dynamically. When i scroll listview these new buttons are
visible. How can i click add buttons then the buttons are immediately
visible. Why it is happened. There is any way to handle this issue.
I've try invalidateViews() invalidate() it doesn't work. Please help me to solve this .
My sample code here
a
dd_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
button = new Button(getApplicationContext());
linear.addView(button1, lparams);
listview.invalidateViews();
}
}
To refresh a Listview, create a ListAdapter and populate it with your items (buttons). Every time you put in it a new set of items, your list gets updated.
Check this reference on ListViews: http://developer.android.com/guide/topics/ui/layout/listview.html
you can use listview.notifyDatasetChanged() method to refresh your list.
I've solve my issue like this. Call this method
//Here running boolean value
//lv listview
private void scrollMyListViewToBottom() {
lv.post(new Runnable() {
public void run() {
if (!running) {
// Select the last row so it will scroll into view...
iv.setSelection(daily_dairy_2.getCount() - 1);
lv.invalidateViews();
running = true;
}
}
});
}

android refresh listview adapter within getview()

I have got a custom list view adapter and an image button in the adapter class. When i click on the image button, the listener should reload the list view. I need to reload the list view within getview() of adapter class. So I need to know other options than using notifyDataSetChanged() in my listActivity class.
Thanks
You want to refresh a cell inside the listview or do you want to refresh the whole listview, if a single row is loaded inside getView() ?
Check this out:
Android ListView Refresh Single Row
Create a static handler inside the activity which calls a method which reloads the listview and send a message to this handler from the adapter whenever required.
handler = new Handler() {
public void handleMessage(Message paramAnonymousMessage) {
switch (paramAnonymousMessage.what) {
case 1:
populateList();
break;
}
}
};
public void populateBill() {
MyBasketAdapter adapter = new MyBasketAdapter(this, basketList);
listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(adapter);
}
Inside the adapter class. for example,
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Message msg = Message.obtain();
msg.what = 1;
MyActivity.handler.sendMessage(msg);
}
});
That is very Simple just write a method in your adapter class and call it get view when you deleting or adding anything in your list which you are binding to your adapter.and use notifyDataSetChanged after change in list
public void updateResults(ArrayList<CustomList> results) {
// assign the new result list to your existing list it will work
notifyDataSetChanged();
}

How to refresh listview in android?

I have used ListView added and delete option but when i clicked delete button database value is deleted but ListView is not refreshed. I have put
adapter.notifyDataSetChanged();
but the list doesn't refresh.
Try this, call adapter.notifyDataSetChanged() inside runOnUiThread.
YourActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
adapter.notifyDataSetChanged();
}
});
UI is update outside the main thread. Put all the logic inside an asynctask and in postexecute, call the adapter.notifyDataSetChanged();
You have to call .invalidate(); on the ListView to tell the framework the the view is outdated and should be redrawen!

ListView doesn't refresh on notifyDataSetChanged

ListView doesn't seem to refresh when notifyDataSetChanged(); is called, but does refresh when its adapter gets set again.
In my Activity onCreate I initialize my ListView and my adapter. Then I have this Hanler that checks for new values every second. listview.setAdapter(arrayAdapter); works but arrayAdapter.notifyDataSetChanged(); doesn't do anything.
Here is the code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);
arrayAdapter = new ArrayAdapter<Integer>(this,android.R.layout. simple_list_item_1, myIntegers);
lv.setAdapter(arrayAdapter);
}
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
if (msg.what == DIS){
handler.sendEmptyMessageDelayed(DIS, 1000);
if(!refresh()){
handler.removeMessages(DIS);
}
}
}
};
public boolean refresh(){
if(ports.isEmpty()){
return false;
}else{
listview.setAdapter(arrayAdapter); //WORKS
arrayAdapter.notifyDataSetChanged(); // DOESN'T WORK
return true;
}
}
So I was wondering how to make that work with notifyDataSetChanged, because I read that it is the right way to do it, and anyway even if the setAdapter does work it makes my listview jump to beginning every time it refreshes it.
EDIT:
To clarify things, I am adding more values to myIntegers.
Try instead adding the values using the adapter.add() method. I dont think you can modify the array that was used to create the adapter and then expect it to hold an updated instance to update the views from. Best of Luck!

Categories

Resources