Update ListView in ArrayAdapter when a row button clicked - android

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

Related

notifyDataSetChanged not working from within custom adapter

I'm faced with strange problem. I've made a ListView, populated it with adapter and everything works fine until I want to delete any item. After I press confirmation button view really deletes, but text inside of it doesn't changes on the text from next view.
For example I've got such items in ListView:
Item 1
Item 2
Item 3
When I press delete Item 2, Item 3 disappears but text in view on position 2 doesn't changes. I know that Item 2 was deleted as I use database for storing data and after app restart everything show as expected. I suppose that problem is in reuse of views by View Holder but I can't figure out how to change text in second view.
Here is code that doesn't work (at least I think that problem is here)
confirmDeleteButton.setOnClickListener(new View.OnClickListener() {//this button is in dialog
#Override
public void onClick(View v) {
DBController.init(context).delete("main", "id=" + item.get(position).getId(), null);//deleting item from database
remove(getItem(position)); //removing view from adapter
notifyDataSetChanged(); //notifying adapter
dialog.dismiss(); //closing dialog
}
});
SOLUTION
Just use RecyclerView instead of ListView.
First you have to remove the data from adapter like adapter.remove(adapter.getItem(position));
and then call notifyDataSetChanged()
don't use the get position to delete the datas, first setid() to confirm buttons like, confirmDeleteButton.setid(position) used in getview adapter class.
then remove the id based on getid() like:
DBController.init(context).delete("main", "id=" + item.get(v.getid()).getId(), null);//deleting item from database
remove(v.getid()); //removing view from adapter
Is better if you use:
adapter.notifyItemRangeRemoved(int positionStart, int itemCount)
adapter.notifyItemRemoved(int position)
More info: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html

Swap two elements in ArrayList inside custom adapter to show in Listview

I have a Listview and a custom adapter, I am trying to change swap two arraylist items inside the apdapter in onclick of button as listview item.
My code is
viewHolder.btnUp.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Integer pos = (Integer) v.getTag();
if (!pojos.get(pos).equals(pojos.get(0))) {
Collections.swap(pojos, pos, pos - 1);
notifyDataSetChanged();
}
}
});
But I can not see the changes in listview, though arraylist has modified, but UI changes has not reflected.
Thats because your adapter has already finished its work. The adapter will turn the data into views and pass those views to the list view. Notice that changing the order in the original collection wont change the views inside the list view. What you could do is remove the views and add them at the correct positions. Get access to the list view by doing viewHolder.getParent()
If pojos is a local final variable, make sure the one the adaptor uses still points to the same collection otherwise the anonymous class will swap 2 elements in an collection that's not being used.
I would recommend to pass the arrayList to the adapter again and setting adapter to the list view, all this before the notifyDataSetChanged(); method.

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

Selected Item of row in ListView

Hi I am using a Listview in my application and I created separate xml for the layout of each row of that ListView. Each row contains two ImageView and one One TextView. I want to get which ImageView is clicked on that row.
You can set OnClickListener in Adapter class itself inside getView() and get the Click Listener working for both the images. As, you haven't posted any code it not feasible to guess.
put the tag with each imageView in getView function of the adapter....
imageView.setTag(Postion);
and get in onclick with view that you received......
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
system.out.println("position"+position)
}});

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