How can I refresh an adapter from an onClick event in Android - android

I have a ArrayAdapter onto which each row has a button which deletes the data associated to the row. So onClick for the "on row delete button" how do I delete the row from the display? I believe I need to refresh the parent view?
public void onClick(View v) {
deleteThisRowFromDB();
//ok now how do I delete this row from the ArrayAdapter display?
}

You can call notifyDataSetChanged() method for refreshing data. You might need to override the method as below,
#Override
public void notifyDataSetChanged()
{
super.notifyDataSetChanged();
}

You can use the simple cursor adapter to bind data to the listview or (parent view)... whenever you delete data from database cursor adapter will updates itself automatically
FYI
Check this example

update the database with deleting the data,
set notify to Adapter by using the method:
adapter.notifyDataSetChanged();

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

Listview doesn't update while inserting data into Database

My question is about Updating listview after data changed in SQLite Database.My Listview doesn't updated when i insert data into databse.On my onRestart() i want to do something like this:
onRestart(){
super.onRestart();
custom_adapter.notifyDataSetChanged();
}
It gives me NullPointerException.
But instead of this i have to write full code:
onRestart(){
super.onRestart();
cursor.requery();
listview.setadapter(custom_adapter);
listview.setonClickListener(new OnclickListener(){
......
}
)};
}
My custom listview contains Imageview and Two TextView.
Calling the notifyDataSetChanged() method from the Adapter checks changes in the list<Objects> that was received in the Adapter constructor, so changing data in sql database is not the point, you have to make sure that you changed the data in the same List<Object> that u sent to the Adapter. in that case the listView will refresh automatic

setText method not working for editText in listview after calling 'notifyDataSetChanged'

I'm developing an android application using eclipse. I have a listview and each row of this contains an EditText. When I click on an EditText I want two things to do.
1: Reload all the EditText with texts from my data array.
2: Set the text of the clicked EditText to a constant string(say "0").
For this I used the below onclicklisetner for EditText.
myEdittext.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
adapter.notifyDataSetChanged();//adapter is my arrayadapter of the listview
EditText clickedText = (EditText)v;
clickedText.setText("0");
}
});
As you can see I'm using 'notifyDataSetChanged' method to reload the EditTexts in the listview and 'setText' method to set the clicked EditText's text to "0".
But the 'setText' method is not working. If I comment the line 'adapter.notifyDataSetChanged()' the 'setText' method is working.
I also tried to do this by calling the setText method after a delay(to check if the notifyDataSetChanged is starting a new thread) but failed.
How can I make both the methods to work in my onClick method.
Thanks in Advance.
You can do it using another approach.
You might be setting the ListView adapter using an ArrayList say mList.
Now in the onItemClick listener of the list view you get the position of the item clicked in the ListView. In the onClick() set the value 0 at that position of the arrayList eg mList.set(position, 0). and then call notifyDataChanged()
try :
So the reason for such behavior is that when you call adapter.notifyDataSetChanged(), there is a good chance that your View V is recycled hence it no longer refers to the one that is clicked. What you really want to do is to change the list so that when notifyDataSetChanged is called then all the values will be loaded accordingly.
Please share your adapter code.
myEdittext.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//adapter is my arrayadapter of the listview
//EditText clickedText = (EditText)v;
//clickedText.setText("0");
//Do some thing to change the adapter data list.
adapter.notifyDataSetChanged();
}
});

Delete an item from the listview and refresh it on a button click in each row

Hi a am struggling with this part. I want to simply delete an item from the listview when the button on that row is clicked.
I have tried
holder.button.setText("End");
holder.button.setTag(position);
holder.button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Integer index = (Integer) v.getTag();
app_details.remove(index.intValue());
notifyDataSetInvalidated();
}
});
But it's behavior is unpredictable I mean when click on the button on a row it delete the another item from the listview.
Any one have some idea?
Thanks
I have faced the same problem & finally get rid out of it. Have to tried
holder.button.setOnItemClickListener Follow the steps to do what you want:
Implement OnItemClickListener in your current activity class.
set button which delete item from list view to setOnClickListener.
Set list view to setOnItemClickListener listener in your anonymous inner class (e.g., in OnClickListener).
notifyDataSetChanged()
Call this Activity once again Using Intent.
here for Example: I take one list view listVw ->
holder.button.setText("End");
holder.button.setTag(position);
holder.button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// your needed stuffs...
listView.setOnItemClickListener(this);
}
});
#Override
public void onItemClick(AdapterView arg0, View arg1, int position, long id) {
// TODO Auto-generated method stub
//Do your stuff here...
}
You want to uses an instance of the ArrayAdapter since that adapter has the remove method. Otherwise you will need to implement your own remove method and have you custom Adapter extend BaseAdapter. Here is an example of what methods you need to call to remove an item from the list and tell the Adapter to refresh the list.
m_adapter.remove(o);
m_adapter.notifyDataSetChanged();
You want to call notifyDataSetChanged() instead of notifyDataSetInvalidated(). Here is the difference ....
notifyDataSetChanged() - Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
notifyDataSetInvalidated() - Notifies the attached observers that the underlying data is no longer valid or available.

android:how to reload the listview in multiple selection time

Hi Friends I when i click the checkbox..i want to refresh the listview because when i check there are no effect in current view but when i scroll down that is perform so i want to reload the view so how can i reload the view in listview chckbox click event??
u can use listmethod ie notifyDataSetChanged()
You can use
cb.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
....
Where cb is a checkbox.
Within this listener you can refresh your list adapter, use lv.setAdapter, where lv is setAdapter.
You can also search refreshing a list adapter view you can search setadapter method. I hope that is what you want.

Categories

Resources