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 7777891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:15:27+00:00 2026-06-01T18:15:27+00:00

Guys I m using a simple listview. I have two activities one main and

  • 0

Guys I m using a simple listview. I have two activities one main and second one to add friend
In the second activity i m taking userid of friend in textbox and adding that row to the database and updating the arraylist.
When i comeback to the mainactivity i call the notifyDataSetChanged() in onResume() function and i also log the arraylist value. Arraylist is very fine and also the row is inserted in database but listview is not getting updated . I donno the problem plz help. Here is d code.

public class VayuActivity extends Activity {
    /** Called when the activity is first created. */
    static boolean first_time=true;
    ListView friend_list;
    public static List<String> al;
    public  ArrayAdapter<String> aa;
    String dummyfriends[]={"prashant","monika","pramod","dhara"};
    public static MessengerDataSource mds;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
/*        if(first_time==true)
        {
            first_time=false;
            setContentView(R.layout.registration);
        }
        else */
            setContentView(R.layout.main);

        al=new ArrayList<String>();
        for(String i:dummyfriends)
            al.add(i);

        aa=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, al);

        friend_list=(ListView) findViewById(R.id.friend_list);

        friend_list.setAdapter(aa);

        mds= new MessengerDataSource(getApplicationContext());

        mds.open();

        System.out.println("I m in oncreate");

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        al=mds.getFriends();
        aa.notifyDataSetChanged();

        System.out.println("I m in on resume function");
        System.out.println("Value of arraylist in on resume fun"+al);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        System.out.println("I m in onpause function");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {


        MenuInflater mi=getMenuInflater();
        mi.inflate(R.menu.mainmenu, menu);

        return true;





    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId())
        {
        case R.id.add_friend:
            //Toast.makeText(this, "Add Friend", 2000).show();
            Intent i=new Intent(VayuActivity.this,AddFriendActivity.class);
            startActivity(i);
            break;

        case R.id.remove_friend:
            Toast.makeText(this, "Remove Friend", 2000).show();
            break;

        }


        return true;
    }
}



package com.pdd.vayu;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AddFriendActivity extends Activity implements OnClickListener{

    EditText et;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
         super.onCreate(savedInstanceState);
        setContentView(R.layout.addfriend);

        et=(EditText) findViewById(R.id.E_friendadd);

        Button b=(Button) findViewById(R.id.add_friend_button);
        b.setOnClickListener(this);
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        String userid=et.getText().toString();
        //VayuActivity.al.add(f);
        Friend f=new Friend();
        f.setUserid(userid);
        f.setFname("pratik");

        VayuActivity.mds.addFriend(f);
        VayuActivity.al=(ArrayList<String>) VayuActivity.mds.getFriends();
        System.out.println(VayuActivity.al);
        //VayuActivity.aa.notifyDataSetChanged();
    }
}

Database classes

package com.pdd.vayu;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class MessengerDataSource {

    private SQLiteDatabase database;
    private MyDatabaseHelper mydbh;

    public static String USER_ID="userid";
    public static String F_NAME="fname";
    public static String CHAT_COUNT="chat_count";

    public static String M_TEXT="mtext";
    public static String SENDER="sender";
    public static String RECEIVER="receiver";
    public static String M_TYPE="type";



    public MessengerDataSource(Context c)
    {
        mydbh=new MyDatabaseHelper(c);
    }

    public void open()
    {
        try
        {
            database=mydbh.getWritableDatabase();   
        }
        catch(Exception e)
        {
            System.out.println(e.getLocalizedMessage());
        }
    }

    public void close() {
        mydbh.close();
    }

    public void addFriend(Friend f)

    {
        ContentValues cv = new ContentValues();
        cv.put(USER_ID,f.getUserid());
        cv.put(F_NAME, f.getFname());
        cv.put(CHAT_COUNT, f.getChat_count());

        try
        {
            database.insert(MyDatabaseHelper.TABLE_FRIEND, null, cv);
        }
        catch(Exception e)
        {
            System.out.println("Error while inserting  "+e.getLocalizedMessage());
        }

    }

    public void addMessage(Message m)

    {
        ContentValues cv = new ContentValues();
        cv.put(M_TEXT, m.getText());
        cv.put(SENDER,m.getSender());
        cv.put(RECEIVER,m.getReceiver());
        cv.put(M_TYPE, m.getType());


        database.insert(MyDatabaseHelper.TABLE_MESSAGE, null, cv);
    }

    public void deleteFriend(String fname)
    {
        database.delete(MyDatabaseHelper.TABLE_FRIEND,F_NAME+"="+fname , null);

    }

    public void deleteMessage(String text)

    {
        database.delete(MyDatabaseHelper.TABLE_FRIEND,M_TEXT+"="+text , null);
    }

    public List<String> getFriends()

    {
        List<String> friends= new ArrayList<String>();
        Cursor c=null;
        try
        {
        c=database.query(MyDatabaseHelper.TABLE_FRIEND, new String[]{F_NAME}, null, null, null, null,CHAT_COUNT);
        }
        catch(Exception e){System.out.println("Error while retrieving frnds list"+e.getLocalizedMessage());
        }

        if(c.moveToFirst())
        {
            do
            {
                String friend=c.getString(c.getColumnIndex(F_NAME));
                friends.add(friend);
            }
            while(c.moveToNext());
        }




        return friends;
    }

    public List<String> getMessages(String from)

    {
        List<String> messages= new ArrayList<String>();

        String where=SENDER+"="+from;

        Cursor c=database.query(MyDatabaseHelper.TABLE_MESSAGE, new String[]{M_TEXT},where , null, null, null,null);

        if(c.moveToFirst())
        {
            do
            {
                String message=c.getString(c.getColumnIndex(M_TEXT));
                messages.add(message);
            }
            while(c.moveToNext());
        }


        return messages;
    }




}

Database Helper

package com.pdd.vayu;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDatabaseHelper extends SQLiteOpenHelper {


    public static String TABLE_FRIEND="friend";

    public static String TABLE_MESSAGE="message";

    public static String CREATE_FRIEND="create table if not exists "+ 
    TABLE_FRIEND+" (userid varchar primary key,fname varchar,chat_count int);";

    public static String CREATE_MESSAGE="create table if not exists "+
    TABLE_MESSAGE+"(mid integer primary key autoincrement," +
    "mtext varchar," +"sender varchar , receiver varchar , type varchar)";

    //public static String TABLE_FRIENDS="friends";

    public static String DB_NAME="MessengerDB.db";

    Context context;


    public MyDatabaseHelper(Context context) {
        super(context, DB_NAME, null, 1);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        db.execSQL(CREATE_FRIEND);
        db.execSQL(CREATE_MESSAGE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}

Pls help

  • 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-06-01T18:15:30+00:00Added an answer on June 1, 2026 at 6:15 pm

    For notifyDataSetChanged() to work, you must add the object to the ArrayAdapter itself using it’s add/remove methods and not the actual list.

    Your onResume should look something like this:

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    
        //al=mds.getFriends();
        aa.clear();
        aa.addAll(mds.getFriends());
        aa.notifyDataSetChanged();
    
        System.out.println("I m in on resume function");
        System.out.println("Value of arraylist in on resume fun"+al);
    }
    

    The ArrayAdapter creates a copy of the list when initiated, so when calling notifyDataSetChanged it only looks in it’s own copy, not the source list.

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

Sidebar

Related Questions

Simple one guys. I have an XML parsed using simplexml_load_file(). The following code: <?php
Guys i am very new to jQuery. I have started using the auto complete
Hay guys, i've made a simple drawing application using the canvas tag. However i
HI Guys. I wanna know how can I make a simple search using named
hey guys, I have no luck making a simple background image animation working without
Hey guys, this should be quite simple to answer.... Im using XSB Prolog...trying to
I have made a simple application for my iphone using jQuery Mobile and Phonegap.
Guys. How to I do to using association of the simple_form gem, but do
I would like to know what guys are using to make diagram of your
Hi guys I'm using this method to check if my app is in background

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.