Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6070729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:57:58+00:00 2026-05-23T09:57:58+00:00

I am trying to call notifyDataSetChanged() on my custom adapter but it seems not

  • 0

I am trying to call notifyDataSetChanged() on my custom adapter but it seems not to be working.

Here is my activity :-

public class ThreadData extends ListActivity
{
private static final Uri SMS_URI = Uri.parse("content://sms");
HashMap<Long, BodyType> bodies = new HashMap<Long, BodyType>();
private String name, number;
private ListView listView;
private Activity activity;
ThreadDataAdapter adapter;
Handler handler;
private Context context;
private static final String PHONE_NUMBER_SEPARATORS = " ()-./";
static HashMap<String, ContactInfo.ContactDetail> info = new HashMap<String, ContactInfo.ContactDetail>();

public void onCreate(Bundle bundle)
{
    super.onCreate(bundle);
    setContentView(R.layout.chats);
    Bundle extras = getIntent().getExtras();
    activity = this;
    context = this;
    if(extras != null)
    {
        number = extras.getString("address");
        ContactInfo.ContactDetail nameInfo = getContactsDetailWithNumber(number);
        if(nameInfo != null)
        {
            name = nameInfo.name;
        }
        else
        {
            name = number;
        }
    }
    TextView person = (TextView) findViewById(R.id.chat_person);
    person.setText(name);
    ImageButton callPerson = (ImageButton) findViewById(R.id.call_person);
    callPerson.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
            popUpDialerAlert();
        }
    });

    buildMessageList();

    ArrayList<BodyType> items = sortBodies();
    adapter = new ThreadDataAdapter(this, items);
    listView = getListView();
    listView.setAdapter(adapter);
    listView.setStackFromBottom(true);

    getContentResolver().registerContentObserver(SMS_URI, true, new MyContentObserver(handler));

}

public void buildMessageList()
{
    Cursor cursor = getContentResolver().query(SMS_URI, null, null, null, "date ASC");
    startManagingCursor(cursor);
    while (cursor.moveToNext())
    {
        BodyType bodyInfo = new BodyType();
        String address = cursor.getString(cursor.getColumnIndex("address"));
        bodyInfo.body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
        Long date = cursor.getLong(cursor.getColumnIndexOrThrow("date"));
        bodyInfo.date = date;
        String type =  cursor.getString(cursor.getColumnIndexOrThrow("type"));
        if(type.equals("1"))
        {
            bodyInfo.type = "received";
        }
        else if(type.equals("2"))
        {
            bodyInfo.type = "sent";
        }
        else if(type.equals("3"))
        {
            bodyInfo.type = "draft";
        }

        String number = filterPhoneNumber(address);
        ContactInfo.ContactDetail nameInfo = getContactsDetailWithNumber(number);
        String personName = number;
        if(nameInfo != null)
        {
            personName = nameInfo.name;
        }
        else
        {
            personName = number;
        }
        if(personName.equals(name))
       {
           bodies.put(date, bodyInfo);
       }
    }
}

public void popUpDialerAlert()
{
    AlertDialog.Builder dialerAlert = new AlertDialog.Builder(this);
    dialerAlert.setTitle("Confirm");
    dialerAlert.setMessage("Call" + " " + name + "?");
    dialerAlert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
            Uri uri = Uri.parse("tel:" + number);
            Intent intent = new Intent((Intent.ACTION_CALL));
            intent.setData(uri);
            context.startActivity(intent);
        }
    });
    dialerAlert.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {

        }
    });
    dialerAlert.show();
}

public ArrayList<BodyType> sortBodies()
{
    ArrayList<Long> dates = new ArrayList<Long>(bodies.keySet());
    Collections.sort(dates);
    ArrayList<BodyType> items = new ArrayList<BodyType>();
    for(Long date : dates)
    {
        for(Long key : bodies.keySet())
        {
            if(date.equals(key))
            {

                items.add(bodies.get(key));
            }
        }
    }
    return items;
}

public void printBodies(ArrayList<BodyType> items)
{
    for(BodyType bodyType : items)
    {
        log(bodyType.date.toString());
        log(bodyType.body);
        log(bodyType.type);
    }
}

static class BodyType
{
    public String type;
    public String body;
    public Long date;

}


public static ContactInfo.ContactDetail getContactsDetailWithNumber(String number)
{
    if(info.containsKey(number))
    {
        return info.get(number);
    }
    return null;
}

public static String filterPhoneNumber(String phoneNumber)
{
    if (phoneNumber == null)
    {
        return null;
    }

    int length = phoneNumber.length();
    StringBuilder builder = new StringBuilder(length);

    for (int i = 0; i < length; i++)
    {
        char character = phoneNumber.charAt(i);

        if (PHONE_NUMBER_SEPARATORS.indexOf(character) == -1)
        {
           builder.append(character);
        }
    }
    return builder.toString();
}

public class MyContentObserver extends ContentObserver
{

    public MyContentObserver(Handler handler)
    {
        super(handler);
    }

    @Override
    public void onChange(boolean selfChange)
    {
        buildMessageList();
        adapter.notifyDataSetChanged();
        super.onChange(selfChange);

    }
}

public void log(String msg)
{
    Log.e("ThreadData", msg);
}
}

Here is my custom adapter :-

public class ThreadDataAdapter extends BaseAdapter
{
Activity activity;
ArrayList<ThreadData.BodyType> data;
LayoutInflater inflater;

public ThreadDataAdapter(Activity a, ArrayList<ThreadData.BodyType> list)
{
    activity = a;
    data = list;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    return data.size();  //To change body of implemented methods use File | Settings | File Templates.
}

public Object getItem(int i) {
    return data.get(i);  //To change body of implemented methods use File | Settings | File Templates.
}

public long getItemId(int i) {
    return i;  //To change body of implemented methods use File | Settings | File Templates.
}

public static class ViewHolder
{
    public TextView message;
}

public View getView(int i, View view, ViewGroup viewGroup)
{

    View row = view;
    final ViewHolder holder;

    ThreadData.BodyType bodyInfo = data.get(i);
    if(bodyInfo.type.equals("sent"))
        {
            Log.e("MMS", bodyInfo.body);
            row = inflater.inflate(R.layout.sentitem, viewGroup, false);
            holder = new ViewHolder();
            holder.message =  (TextView)row.findViewById(R.id.sent);
            holder.message.setText(bodyInfo.body);

        }
        else
        {
            row = inflater.inflate(R.layout.chatitems, viewGroup, false);
            holder = new ViewHolder();
            holder.message =  (TextView)row.findViewById(R.id.received);
            holder.message.setText(bodyInfo.body);
        }
    return row;
}
@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();  
}
}

What i am trying to do is, i am registering a ContentObserver and calling notifyDataSetChanged() in its onChange() whenever there is some change in device’s SMS database. But it isn’t working at all.

Any help would be appreciated. Please HELP!

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:57:59+00:00Added an answer on May 23, 2026 at 9:57 am

    The List you use in your adapter (data) is a reference on a List that you create in sortBodies. It is never updated. Thus your adapter is stuck on a copy of items that is not updated by your OnChange method.

    In other words, whether or not OnChange is called, getView will always return the same views…

    You adapter should use bodies, as this MashMap is updated by your OnChange method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to call notifyDataSetChanged for my custom ExpandableListView. what happens is for my
I'm trying call qsort in Cython with a custom compare function but I don't
I am trying to call a class method through reflection. public class A {
I am trying to call cleartool from an java application, but cleartool hangs even
I'm trying to call the HttpServerUtuility.URLDecode function in C# using Visual Studio 2005, but
I am trying to execute the following AsyncTask : private class TimeUpdateTask extends AsyncTask<List<mObject>,
I'm trying to call a user-defined MATLAB function from a C application, but I'm
Im trying to write a single activity app, extending ListActivity. I have a class
Hi im trying call this function from an element, but the element id is
When trying to call on a method from another class my method as a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.