I got this problem several days ago, I'm working on a program where a ListView takes data from a database via a custom SimpleCursorAdapter, I used to use listView.setAdapter(adapter)to update the data in the ListView every time the data is changed. But that causes a problem, that everytime you update the data, the listView will automatically scroll to the top, so I decided to use swapCursor() instead, like this:
Cursor cursor = database.getData();
if(listView.getAdapter() == null){
//Toast.makeText(getApplicationContext(),"null",Toast.LENGTH_SHORT).show();
adapter = (new CustomSimpleCursorAdapter(this,R.layout.todolist,cursor,new String[] {LIST_TEXT},new int[]{R.id.text}));
listView.setAdapter(adapter);
}else {
Runnable runnable =new Runnable() {
#Override
public void run() {
Cursor finalCs = database.getData();
todoListAdapter.swapCursor(finalCs);
todoListAdapter.notifyDataSetChanged();
}
};
runOnUiThread(runnable);
}
But then the error appears every time I try to add a data to the database and display it in the listView:
**CursorIndexOutOfBoundsException: Index 12 requested, with a size of 12**
(Note that the data is added to the database, and no problem has been found previously using listView.setAdapter(adapter), I tried to remove todoListAdapter.notifyDataSetChanged();, but that didn't help at all. Also, when I try to remove data from the database and also update the listView with the change, it's always the last item in the listView got removed no matter which one I selected (the data by now is actually correctly removed from the database), and when I try to add data again, I can now add the equal amount of data like the previously deleted data amount(if I removed 4 item, then I can only add 4 item after that or the error will appear). Anyways, swapCursor()seems pretty broken as of my use of it, did I do something wrong? or I need something else for swapCursor() to work? Thanks in advance!
Update: Error traceback context:
android.database.CursorIndexOutOfBoundsException: Index 13 requested, with a size of 13
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at android.support.v4.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:139)
Update: Custom SimpleCursorAdapter:
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import java.util.ArrayList;
public class TodoListAdapter extends SimpleCursorAdapter {
private Cursor c;
private Context context;
private ArrayList<Long> itemChecked = new ArrayList<>();
protected int[] mFrom;
protected int[] mTo;
LayoutInflater inflater;
private int mStringConversionColumn = -1;
private CursorToStringConverter mCursorToStringConverter;
private ViewBinder mViewBinder;
MainActivity main;
String[] mOriginalFrom;
public TodoListAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to,1);
this.c = c;
this.context = context;
mTo = to;
mOriginalFrom = from;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public ViewBinder getViewBinder() {
return mViewBinder;
}
public ArrayList returnSelected(){
return itemChecked;
}
public interface MyInterface{
public void foo();
}
public void setViewBinder(ViewBinder viewBinder) {
mViewBinder = viewBinder;
}
public View getView(final int pos, View inView, ViewGroup parent) {
c.moveToPosition(pos);
if (inView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inView = inflater.inflate(R.layout.todolist, null);
}
final TextView todoText = (TextView) inView.findViewById(R.id.titleText);
final CheckBox cBox = (CheckBox) inView.findViewById(R.id.multiSelectionBox);
System.out.println(c.getCount());
newView(context,c,parent);
bindView(inView,context,c);
final long id = getItemId(pos);
cBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v.findViewById(R.id.multiSelectionBox);
if (cb.isChecked()) {
if(context.toString().contains("MainActivity")){
((MainActivity)context).addSelectedId(id);
}else if(context.toString().contains("HistoryActivity")){
((HistoryActivity)context).addSelectedId(id);
}
System.out.println("checked " + id);
} else if (!cb.isChecked()) {
System.out.println("unchecked " + id);
if(context.toString().contains("MainActivity")){
((MainActivity)context).removeSelectedId(id);
}else if(context.toString().contains("HistoryActivity")){
((HistoryActivity)context).removeSelectedId(id);
}
}
}
});
return inView;
}
}
I finally got it, it's simply because I didn't change the cursor in the adapter to the passed in cursor, override bindView method and add this.c = cursor into the original code, then when calling getView, the cursor will be the new cursor. That's just a stupid mistake I made by myself.
Special thanks to #pskink for helping me to realize that, I don't need to override bindView or newView after all, but you mentioned the c.moveToPosition() made me realize the mistake.
Related
I have an application that uses sqlite, it stores information about hardware store items and displays them in a ListView, this list view shows the name of the item, the price, the quantity, and the supplier. And each list item also has a Sell button and when I click the button it is supposed to subtract 1 from that specific item's quantity and update the database, but since the button is created in the CursorAdapter Im not sure how to access the database and update it.
This is my CursorAdapter:
package com.example.android.inventoryapp;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.android.inventoryapp.data.InventoryContract.InventoryEntry;
public class InventoryCursorAdapter extends CursorAdapter {
public InventoryCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView itemNameView = view.findViewById(R.id.item_name);
TextView itemPriceView = view.findViewById(R.id.item_price);
TextView itemQuantityView = view.findViewById(R.id.item_quantity);
TextView itemSupplierView = view.findViewById(R.id.item_supplier);
ImageView sellButton = view.findViewById(R.id.sell_button);
int nameColumnIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_ITEM_NAME);
int priceColumnIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_ITEM_PRICE);
int quantityColumnIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_ITEM_QUANTITY);
int supplierColumnIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_ITEM_SUPPLIER);
int quantity = cursor.getInt(quantityColumnIndex);
String name = cursor.getString(nameColumnIndex);
String price = String.valueOf(cursor.getInt(priceColumnIndex)) + context.getString(R.string.currency_symbol);
String quantityStr = String.valueOf(quantity);
String supplier = cursor.getString(supplierColumnIndex);
itemNameView.setText(name);
itemPriceView.setText(price);
itemQuantityView.setText(quantityStr);
itemSupplierView.setText(supplier);
}
}
In your activity that holds the adapter reference, create an inner class something like:
public class MyClickListener {
public void handleClick(Item item) {
// access your DB here, {item} is available if you need the data
}
}
then when you create your adapter
myAdapter = new InventoryCursorAdapter(context, cursor, new MyClickListener());
save the reference to that click listener in your adapter.
then in the adapter's BindView method (if you need the item data to update the database, pass it through the click listener)
sellButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Item item = myItemSet.get(position);
myListener.handleClick(item);
}
});
So I have 2 activities.
The first (ActivityOne) displays a listview with data from SQLite cursor, and a button.
On click of that button, I want to add an item to the listview, so I display the second activity (ActivityTwo), that contains a number of editTexts and a save Button, that does the saving in the Database.
But what I want is:
after saving the new item to the DB, the ActivityTwo should close and the ActivityOne should be displayed with the refreshed content from the DB
.
This seems a reasonable workflow. How do I achieve it?
Code for ActivityOne:
public class ActivityOne extends Activity {
private ArrayList<String> idclient = new ArrayList<String>();
private ArrayList<String> numeclient = new ArrayList<String>();
private ArrayList<String> tipclient = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView mylist = (ListView) findViewById(R.id.lv_clienti);
LoadList();
Button btnex = (Button) findViewById(R.id.btnNewCli);
btnex.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View aView)
{
Toast.makeText(getApplicationContext(), "Add new client... " , Toast.LENGTH_SHORT).show();
Intent toAnotherActivity = new Intent(aView.getContext(), NewClientActivity.class);
startActivity(toAnotherActivity);
}
}
);
}
public void LoadList(){
SQLiteDatabase db = new myDbHelper(getApplicationContext()).getWritableDatabase();
Cursor mCursor = db.rawQuery("select idclient,nameclient,typeclient from clienti order by numeclient" , null);
idclient.clear();
numeclient.clear();
tipclient.clear();
if (mCursor.moveToFirst()) {
do {
idclient.add(Integer.toString(mCursor.getInt(0)));
nameclient.add(mCursor.getString(1));
typeclient.add(mCursor.getString(2));
} while (mCursor.moveToNext());
}
DisplayClientiAdapter disadpt = new DisplayClientiAdapter(ClientiActivity.this,idclient,nameclient, typeclient);
ListView lv = (ListView) findViewById(R.id.lv_clienti);
lv.setAdapter(disadpt);
mCursor.close();
db.close();
}
}
And in the ActivityTwo, I have in a button click:
db.execSQL("insert into clients (idclient, nameclient,typeclient,...");
DisplayClientiAdapter da = new DisplayClientiAdapter(getApplicationContext());
da.notifyDataSetChanged();
finish();
Also the displayAdapter is something like:
public class DisplayClientiAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> idclient;
private ArrayList<String> numeclient;
private ArrayList<String> tipclient;
public DisplayClientiAdapter(Context c){
this.mContext = c;
}
public DisplayClientiAdapter(Context c, ArrayList<String> idclient, ArrayList<String> numeclient, ArrayList<String> tipclient) {
this.mContext = c;
this.idclient = idclient;
this.numeclient = numeclient;
this.tipclient = tipclient;
}
public int getCount() {
return idclient.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int pos, View child, ViewGroup parent) {
Holder mHolder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.clienti_item, null);
mHolder = new Holder();
mHolder.txt_idclient = (TextView) child.findViewById(R.id.tv_cl_id);
mHolder.txt_numeclient = (TextView) child.findViewById(R.id.tv_cl_nume);
mHolder.txt_tipclient = (TextView) child.findViewById(R.id.tv_cl_tip);
child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
mHolder.txt_idclient.setText(idclient.get(pos));
mHolder.txt_numeclient.setText(numeclient.get(pos));
mHolder.txt_tipclient.setText(tipclient.get(pos));
return child;
}
public class Holder {
TextView txt_idclient;
TextView txt_numeclient;
TextView txt_tipclient;
}
Of course it does not work like this. The list is not refreshed... I assume it has to do with the displayAdapter !?!?!
I cannot call the LoadList method since it is static or something like that...
Please help.
Thank you
Its not a problem with your adapter. You have to call Loadlist() in onresume method instead of oncreate method in ActivityOne. It will work then.
First of all, have a look at this two articles:
http://www.doubleencore.com/2013/05/layout-inflation-as-intended/
http://www.doubleencore.com/2013/06/context/
You shouldn't inflate your views with null in your inflate method if you have parent view available.
Also, using application context for inflating may cause strange behaviour, as it may not use correct theme you may've set in app manifest for your Activity.
On the other hand - why don't you use CursorAdapter instead of BaseAdapter?
The problem with your adapter is, that you don't set the data in it! :)
///EDIT:
I checked the wrong activity - why do you create second adapter in there?
The easiest solution would be to move the LoadList() to onStart.
If you want to do it right, you should use ContentObserver and (probably) CursorAdapter.
I am using the library found at:
https://github.com/tjerkw/Android-SlideExpandableListView
to provide slidedown views on my listitems. The problem I am running into is that each of my list items has a delete button which will remove the item from the underlying adapter. When this happens if the item that was removed was expanded then the item below it will become expanded. After digging through the source for the expandablelistview library I found that the culprit is a BitSet which is being used to keep track of the states of the listviews ( expanded 1, not expanded 0 ). When I remove an item from the list the list of states does not get updated. It needs to shift all values down. The problem is I am not sure how to notify the library that my adapter has had an item removed from it.
My custom list adapter extends array adapter and when I remove an item I call notifyDataSetChanged. Somehow I need to detect that call in the slideExpandablelistview which wraps my adapter so that I can update the BitSet. IF anyone has worked with this library before or care to take a look I would love some help.
I create the expanandablelistview by doing
myAdapter= new SlideExpandableListAdapter(new CustomAdapter(getActivity(), new ArrayList<CustomObject>()), R.id.contact_row, R.id.expandable);
Thanks,
Nathan
"When this happens if the item that was removed was expanded then the item below it will become expanded."
After deleting the item from your ArrayList and then doing adapter.notifyDataSetChanged(), you can do something like:
((ActionSlideExpandableListView) list).collapse();
Here is a full example that I made by modifying the author's sample that comes with the library:
package com.tjerkw.slideexpandable.sample;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.tjerkw.slideexpandable.library.ActionSlideExpandableListView;
public class ExampleActivity extends Activity
{
PersonDB db;
ArrayList<String> people;
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedData)
{
super.onCreate(savedData);
this.setContentView(R.layout.single_expandable_list);
final ActionSlideExpandableListView list = (ActionSlideExpandableListView) this.findViewById(R.id.list);
db = PersonDB.getInstance();
people = db.getPeople();
adapter = new PersonArrayAdapter(people);
list.setAdapter(adapter);
// listen for events in the two buttons for every list item.
// the 'position' var will tell which list item is clicked
list.setItemActionListener(new ActionSlideExpandableListView.OnActionClickListener() {
#Override
public void onClick(View listView, View buttonview, int position)
{
switch (buttonview.getId())
{
case R.id.buttonA:
people.remove(position);
((ArrayAdapter<String>) adapter).notifyDataSetChanged();
((ActionSlideExpandableListView) list).collapse();
break;
case R.id.buttonB:
Toast.makeText(ExampleActivity.this, "You pressed buttonB", Toast.LENGTH_SHORT).show();
}
}
// note that we also add 1 or more ids to the setItemActionListener
// this is needed in order for the listview to discover the buttons
}, R.id.buttonA, R.id.buttonB);
}
private class PersonArrayAdapter extends ArrayAdapter<String>
{
public PersonArrayAdapter(ArrayList<String> people)
{
super(ExampleActivity.this, R.layout.expandable_list_item, people);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
// if we weren't given a view, inflate one
if (convertView == null)
{
convertView = getLayoutInflater().inflate(R.layout.expandable_list_item, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.text);
textView.setText(people.get(position));
return convertView;
}
#Override
public int getCount()
{
return people.size();
}
}
}
Here is the dummy database class:
package com.tjerkw.slideexpandable.sample;
import java.util.ArrayList;
public class PersonDB
{
private static PersonDB db = null;
private ArrayList<String> people;
private PersonDB()
{
people = new ArrayList<String>();
// fill "database" with dummy data
for (int i = 0; i < 20; i++)
people.add(i, "Person " + i);
}
public static PersonDB getInstance()
{
if (db == null)
db = new PersonDB();
return db;
}
public ArrayList<String> getPeople()
{
return people;
}
public String getPerson(int i)
{
return people.get(i);
}
public void deletePerson(int i)
{
people.remove(i);
}
}
EDIT: I noticed a bug with this code. When a row is deleted and the row below it moves up to fill that space, the text on the new row's buttonA appears left-justified. The only way I could think of to solve this was to replace this line:
((ArrayAdapter<String>) adapter).notifyDataSetChanged();
with these two lines:
adapter = new PersonArrayAdapter(people);
list.setAdapter(adapter);
If anyone has a better solution I would love to see it.
I have switched from a ResourceCursorAdapter where I used newView and bindView to a SimpleCursorAdapter where I am using only the getView method.
Now I have an error in onLoaderFinished. Although it gives me NullPointerException on adapter.swapCursor(cursor) both my adapter and cursor object are NOT null. I will post all of my code below. Any help is greatly appreciated (not got much hair left to pull out).
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.ResourceCursorAdapter;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
public class ContactSelect extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private static final int LOADER_ID = 1;
private MyAdapter adapter;
private ListView list;
private View row;
private SparseBooleanArray checkedState = new SparseBooleanArray();
#SuppressLint({ "NewApi", "NewApi" })
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_contact_select);
adapter = new MyAdapter(this, R.layout.contacts_select_row, null, null, null, 0);
getSupportLoaderManager().initLoader(LOADER_ID, null, this);
list = (ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setEmptyView(findViewById(R.id.empty));
}
#SuppressLint("NewApi")
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
final String projection[] = new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME};
final Uri uri = ContactsContract.Contacts.CONTENT_URI;
final String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1" +
" AND " + ContactsContract.Contacts.IN_VISIBLE_GROUP + " =1";
final String order = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
final CursorLoader loader = new CursorLoader(this, uri, projection, selection, null, order);
return loader;
}
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
for(int i=0;i<cursor.getCount();i++){
checkedState.put(i, false);
}
adapter.swapCursor(cursor);
}
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
private class MyAdapter extends SimpleCursorAdapter implements OnClickListener{
private CheckBox markedBox;
private TextView familyText;
private Context context;
private Cursor cursor;
public MyAdapter(Context context, int layout, Cursor c, String[] from,
int[] to, int flags) {
super(context, layout, c, from, to, flags);
this.context = context;
this.cursor = getCursor();
}
#Override
public View getView(int position, View view, ViewGroup group) {
final LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = li.inflate(R.layout.contacts_select_row, group, false);
view.setTag(cursor.getPosition());
view.setOnClickListener(this);
familyText = (TextView)view.findViewById(R.id.contacts_row_family_name);
markedBox = (CheckBox)view.findViewById(R.id.contacts_row_check);
familyText.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
boolean currentlyChecked = checkedState.get(cursor.getPosition());
markedBox.setChecked(currentlyChecked);
setProgressBarIndeterminateVisibility(false);
return super.getView(position, view, group);
}
public void onClick(View view) {
int rowId = (Integer)view.getTag();
Log.d("OnClick", String.valueOf(rowId));
boolean currentlyChecked = checkedState.get(rowId);
markedBox.setChecked(!currentlyChecked);
checkedState.put(rowId, !currentlyChecked);
Log.d("checkedState", "checkedState(" + rowId + ") = " + checkedState.get(rowId));
}
}
}
A call of the swapCursor method of the SimpleCursorAdapter class will trigger a function which maps the column names from the String array provided to the constructor(the 4th parameter) to an array of integers representing the columns indexes. As you pass null in the constructor of MyAdapter for the String array representing the columns names from the cursor, this will throw a NullPointerException later when the swapCursor will try to make the mapping(the NullPointerException should appear in a method findColumns, which is the actual method that uses the column names String array).
The solution is to pass a valid String array, you may also want to do this for the int array representing the ids for the views in which to place the data:
String[] from = {ContactsContract.Contacts.DISPLAY_NAME};
int[] to = {R.id.contacts_row_family_name, R.id.contacts_row_check};
adapter = new MyAdapter(this, R.layout.contacts_select_row, null, from, to, 0);
I don't know what you are trying to do but your implementation of the getView method is not quite right:
You do the normal stuff for the getView method(creating layouts, searching views, binding data) and then you simply return the view from the superclass(?!?), you'll probably just see the default layout with nothing in it.
The way you wrote the getView method is not very efficient, you may want to look into view recycling and the view holder pattern.
cursor.getPosition() will not do what you want as you don't move the cursor to the correct position. By default cursors based adapters will do this for you in the getView method, but, as you overrode the method it's your job to move the cursor's position.
You should leave the getView method and use the two methods newView and bindView as they offer a better separation of the logic.
adapter = new MyAdapter(this, R.layout.contacts_select_row, null, null, null, 0);
and your MyAdapter class you are passing null cursor
public MyAdapter(Context context, int layout, Cursor c, String[] from,
int[] to, int flags) {
super(context, layout, c, from, to, flags);
this.context = context;
this.cursor = getCursor();
}
I have a little problem with ListView. How do I clear a ListView content, knowing that it has a custom adapter?
edit -
the custom adapter class extends BaseAdapter, it looks like this:
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private Activity activity;
private String[] data;
private static LayoutInflater inflater = null;
public MyAdapter(Activity a, String[] str) {
activity = a;
data = str;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public static class ViewHolder {
public TextView text;
}
#Override
public int getCount() {
return data.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
View v = view;
ViewHolder holder;
if (v == null) {
v = inflater.inflate(R.layout.rowa, null);
holder = new ViewHolder();
holder.text= v.findViewById(R.id.dexter);
v.setTag(holder);
} else {
holder = v.getTag();
}
holder.text.setText(data[position]);
return v;
}
}
Simply write
listView.setAdapter(null);
I guess you passed a List or an Array to the Adapter. If you keep the instance of this added collection, you can do a
collection.clear();
listview.getAdapter().notifyDataSetChanged();
this'll work only if you instantiated the adapter with collection and it's the same instance.
Also, depending on the Adapter you extended, you may not be able to do this. SimpleAdapter is used for static data, thus it can't be updated after creation.
PS. not all Adapters have a clear() method. ArrayAdapter does, but ListAdapter or SimpleAdapter don't
It's simple .First you should clear your collection and after clear list like this code :
yourCollection.clear();
setListAdapter(null);
As of Android versions M and N, following works for me and would be the correct approach. Emptying the ListView or setting the Adapter to null is not the right approach and would lead to null pointer issue, invalid ListView and/or crash of the app.
Simply do:
mList.clear();
mAdapter.notifyDataSetChanged();
i.e. first you clear the list altogether, and then let the adapter know about this change. Android will take care of correctly updating the UI with an empty list. In my case, my list is an ArrayList.
In case you are doing this operation from a different thread, run this code on the UI thread:
runOnUiThread(mRunnable);
Where mRunnable would be:
Runnable mRunnable = new Runnable() {
public void run() {
mList.clear();
mAdapter.notifyDataSetChanged();
}
};;
Simple its works me:)
YourArrayList_Object.clear();
Remove your items from your custom adapter and call notifyDataSetChanged().
There is a solution for the duplicate entry in listview.
You have to declare the onBackPress()-method on your activity and write down the highlight code given below:
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
**
attendence_webdata.clear(); list.setAdapter(null);
--------------------------------------------------
**
}
Just put the code ListView.Items.Clear(); on your method
Call clear() method from your custom adapter .
You need to call both clear() from ArrayAdapter and notifyDataSetChanged() both.
Below is link
click