Updating the list view when the adapter data changes
substitute:
mMyListView.invalidate();
for:
((BaseAdapter) mMyListView.getAdapter()).notifyDataSetChanged();
If that doesnt work, refer to this thread:
Android List view refresh
Updating the list view when the adapter data changes, in visual-studio android sdk
The problem is that tables
instance (in c# section) is the one that gets updated but TableItemAdapter
keeps a separate List
internally and that's the one that should get updated instead.
Then NotifyDataSetChanged()
will work! So...
My custom adapter
public class TableItemAdapter : BaseAdapter<Table>
{
List<Table> items;
...
public void Update(IEnumerable<Table> tables)
{
items.Clear();
items.AddRange(aItems);
NotifyDataSetChanged();
}
}
c#
...
tables.Add(new Table() { Name = "Table A1" });
lvAdapter.Update(tables); // <-- call this everytime tables change
The code can be optimized, so that only one list of objects is used, by adding a method in the custom adapter to return its List<Table> items
and using that instead of creating another List
for tables
.
How to refresh Android listview?
Call notifyDataSetChanged()
on your Adapter
object once you've modified the data in that adapter.
Some additional specifics on how/when to call notifyDataSetChanged()
can be viewed in this Google I/O video.
ListView adapter data change without ListView being notified
You have to do all your updating of Adapter data on the UI thread so the synchronized block isn't necessary. It's also useless since your synchronizing on the AsyncTask
which is created new each time it's executed.
Another problem is that you are calling notifyDataSetChanged
external to the Adapter
. You should call it at the end of your setListItems
method. That shouldn't be causing errors though since it's being executed on the UI thread but it should not be called in that way.
You should make sure that your getReports
method is not modifying the backing store of the Adapter
in any way. Since it is running on a separate thread it cannot modify anything the Adapter
has access too. Even if it's protected by locks. What you need to do is in your doInBackground
method is generate the list of updates or a new list, etc., and pass that into onPostExecute
which then commits the new data to the Adapter
on the UI thread. So, if your getReports
function is changing report_list
and your Adapter
has a reference to report_list
you are doing it wrong. getReports
must create a new report_list
and then pass that back to your Adapter
when it's finished creating it on the UI thread.
To reiterate, you can only modify data the Adapter
and subsequently the ListView
has access too on the UI thread. Using synchronization/locks does not change this requirement.
Updating background of ListView Items when Adapter data changes
You have to update the cursor using adapter.changeCursor
when data changes. See this answer: https://stackoverflow.com/a/1986071/198996
Refresh List View (Custom adapter)
public View getView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.no_icon_list_row,parent,false);
return row;
}
}
not right.you can modify below:
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = inflater.inflate(R.layout.no_icon_list_row,parent,false);
}
TextView myTitle = (TextView) convertView.findViewById (R.id.noIconListTitles);
return convertView,
}
last. if you want refresh data just invoke adapter.notifyDataSetChanged
.Hope help you.
How to update ListView after updating database?
Change following code:
public class SettingsActivity extends AppCompatActivity {
private Context context;
private DogDatabaseHelper dbHelper;
private ListView mListView;
private ArrayList<String> names = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.settings);
mListView = (ListView)findViewById(R.id.listforall);
context = this;
DogDatabaseHelper dbHelper = new DogDatabaseHelper(getApplicationContext());
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.rawQuery("select name from dog",null);
int count = cursor.getColumnIndex("name");
if(cursor.moveToFirst()){
do{
names.add(cursor.getString(cursor.getColumnIndex("name")));
}while (cursor.moveToNext());
AdapterForNames namesAdapter = new AdapterForNames(this,names);
namesAdapter.notifyDataSetChanged();
mListView.setAdapter(namesAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Intent intent = new Intent(SettingsActivity.this,SetupActivity.class);
intent.putExtra("name",names.get(position));
startActivity(intent);
}
});
}
}
With below code:
public class SettingsActivity extends AppCompatActivity {
private Context context;
private DogDatabaseHelper dbHelper;
private ListView mListView;
private ArrayList<String> names = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.settings);
context = this;
AdapterForNames namesAdapter = new AdapterForNames(this,names);
mListView = (ListView)findViewById(R.id.listforall);
mListView.setAdapter(namesAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Intent intent = new Intent(SettingsActivity.this,SetupActivity.class);
intent.putExtra("name",names.get(position));
startActivity(intent);
}
});
DogDatabaseHelper dbHelper = new DogDatabaseHelper(getApplicationContext());
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.rawQuery("select name from dog",null);
int count = cursor.getColumnIndex("name");
if(cursor.moveToFirst()){
do{
names.add(cursor.getString(cursor.getColumnIndex("name")));
namesAdapter.notifyDataSetChanged();
} while (cursor.moveToNext());
}
}
Hope it helps!
How to refresh ListView custom adapter data from the event listener that is in the adapter in Android?
I got the answer, actually it is too simple. What I need to do is just add this code inside the click listener of done button in the custom adapter.
This is the click listener of the doneBtn in custom adapter:
doneBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper.markAsDone(values.get(position).getId());
//How can I update the listview items here ?
if(values!=null)
{
Task oldTask = values.get(position);
oldTask.setDone(Boolean.TRUE);
values.set(position,oldTask);
notifyDataSetChanged();
}
}
});
Related Topics
How to Animate Addition or Removal of Android Listview Rows
Navigation Drawer Semi-Transparent Over Status Bar Not Working
What Is the Use of the Res/Values/Public.Xml File on Android
Cardview Layout_Width="Match_Parent" Does Not Match Parent Recyclerview Width
Android Type_Linear_Acceleration Sensor - What Does It Show
Actionbaractivity Cannot Resolve a Symbol
Android: Background Image Size (In Pixel) Which Support All Devices
Why Do Most Fields (Class Members) in Android Tutorial Start with 'M'
Creating a Softkeyboard with Multiple/Alternate Characters Per Key
Fragments in Android 2.2.1, 2.3, 2.0. Is This Possible
Is Secure.Android_Id Unique for Each Device
How to Get Android Device Screen Size
How to Perform Collection Group Query Using Document Id in Cloud Firestore