1

I have two activities and I'm implementing the update query in the next Activity and i want add to updated row in my List-view which is in Previous Activity. I tried database table row updated but list-view is not updated.How can i solve this issue.Can someone help me.

Here is my Populate list-view from Sq Lite in Database helper class

public List<All_Post> getAllDescriptions()
     {
        List<All_Post> allPostDescList = new ArrayList<All_Post>();
        String selectQuery = "select * from  " + Table_AllPost_Table + " where  ActiveStatus = '1' ORDER BY ActionDate DESC";

        db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do
            {
                All_Post allPostDesc = new All_Post();
                allPostDesc.setID(cursor.getInt(cursor.getColumnIndex("ALL_ACT_ID")));
                allPostDesc.setStrActiveStatus(cursor.getString(cursor.getColumnIndex("ActiveStatus")));
                allPostDesc.setStrActivityId(cursor.getString(cursor.getColumnIndex("ActivityId")));
                allPostDesc.setStrRemark(cursor.getString(cursor.getColumnIndex("Remark")));
                allPostDesc.setStringInspectorname(cursor.getString(cursor.getColumnIndex("inspectorName")));
                allPostDesc.setStrNotationNo(cursor.getString(cursor.getColumnIndex("HashTag")));
                allPostDesc.setStrShortName(cursor.getString(cursor.getColumnIndex("Type_ShortRGN")));
                allPostDesc.setStrUserId(cursor.getString(cursor.getColumnIndex("UserId")));
                allPostDesc.setStrvessel_Id(cursor.getString(cursor.getColumnIndex("VesselId")));
                String strSqliteDate = cursor.getString(cursor.getColumnIndex("ActionDate"));
                DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
                Date parsed = null;
                try
                {
                    parsed = outputFormat.parse(strSqliteDate);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String outputText = outputFormat.format(parsed);

                allPostDesc.setActiondate(outputText);

                allPostDescList.add(allPostDesc);
            } while (cursor.moveToNext());
        }
         cursor.close();
         db.close();
        return allPostDescList;
    }

Here is my Populate list-view from Sq Lite

@Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.all_post);

        ctx = this;

        dbhelper = new MyDbHelper(this);
        dbhelper.onOpen(db);

        imgLeftmenu = (ImageView) findViewById(R.id.imgLeftMenu);
        imgSearch = (ImageView) findViewById(R.id.imgSearch);
        imgAdd = (ImageView) findViewById(R.id.imgAdd);

        listView = (ListView) findViewById(R.id.listview_AllPost);
        Log.e(" After "," Filter Screen !!! ");
        populateList();

}

 @Override
    protected void onResume() {
        super.onResume();
        populateList();
    }
    private void populateList() {
            descArray.clear();

            List<All_Post> allDesc = dbhelper.getAllDescriptions();
            for (All_Post all_Post : allDesc)
            {
                String strInspectorname = all_Post.getStringInspectorname();
                Log.e("strInspectorname "," = " + strInspectorname);
                descArray.add(all_Post);
            }

            if (adapter == null)
            {
                Log.e("Adapter ", " == Null !!!! ");
                adapter = new MyListAdapter(this, R.layout.all_post_row, descArray);
                listView.setAdapter(adapter);

            }
            else if (adapter != null)
            {
                Log.e("Adapter ", " != Null !!!! ");
                adapter.notifyDataSetChanged();
                adapter = new MyListAdapter(this, R.layout.all_post_row, descArray);
                listView.setFastScrollEnabled(true);
                listView.setAdapter(adapter);
            }
        }

And here is my Another next activity code in onCreate()

  public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.filter_screen);
        ctx = this;
        Log.e("I m", " in Filter !!!");
        dbhelper = new MyDbHelper(this);
        dbhelper.onOpen(db);

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        str_Authentication_Token = sharedPreferences.getString("strAuthentication_Token", "");
        str_UserId = sharedPreferences.getString("strUserId", "");
        str_UserName = sharedPreferences.getString("strUserName", "");

        imgProfilePic = (ImageView) findViewById(R.id.img_ProfilePic);

        imgBtn_LogOut = (ImageView) findViewById(R.id.img_LogOut);
        listView_Filter = (ListView) findViewById(R.id.filter_List);
        txtUserName = (TextView) findViewById(R.id.txt_Username);

        txtUserName.setText(str_UserName);
        userAutoComplete_TextView = (AutoCompleteTextView) findViewById(R.id.autoComplete_UserList);

        db = dbhelper.getWritableDatabase();
        String query = "select * from Inspector";
        Cursor cursor = db.rawQuery(query, null);
        if (cursor.moveToFirst()) {
            do {
                Filter filter = new Filter();
                filter.setName(cursor.getString(cursor.getColumnIndex("Inspector_name")));
                String strActiveStatus = cursor.getString(cursor.getColumnIndex("ActiveStatus"));
                String strInspectorname = cursor.getString(cursor.getColumnIndex("Inspector_name"));

                if (strActiveStatus.equals("1")) {
                    userlist.add(strInspectorname);
                    Log.e("strInspectorname", " from Inspector = " + strInspectorname + "  & strActiveStatus  = " + strActiveStatus);
                }

            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();

        userAutoComplete_TextView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, userlist));
        userAutoComplete_TextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                String foundItem = arg0.getItemAtPosition(arg2).toString();
                Log.e("You have selected --->", "" + foundItem);
                Filter filter = new Filter();
                filter.setName(foundItem);
                filter_ArrayList.add(filter);
                filterAdapter = new FilterAdapter(Filter_Screen.this, R.layout.filter_list_item, filter_ArrayList);
                listView_Filter.setAdapter(filterAdapter);
                db = dbhelper.getWritableDatabase();
                db.execSQL("UPDATE ALL_Post SET ActiveStatus ='1' WHERE inspectorName ='" + foundItem + "'");
                Log.e("Updated ListView --->", "" + foundItem);

            }
        });
}

Thanks in advanced.

p. ld
  • 585
  • 3
  • 10
  • 23
  • follow this link from stackoverflow- [link](http://stackoverflow.com/questions/3669325/notifydatasetchanged-example) – sud Nov 17 '15 at 04:58
  • You need to reload your arraylist from db when you come back to main activity. As when you update the db your array list still not updated. So either reload your array list or just update the arraylist content which you have updated in your previous activity. Other wise please show code of both activity so i can look into it. – shreyash mashru Nov 17 '15 at 05:03
  • Can you post log also ? – shreyash mashru Nov 17 '15 at 05:21

1 Answers1

1

Try to adapter.notifyDataSetChanged(); in onResume in first Activity.

Nikul Rao
  • 83
  • 8
  • I have changes in onresume method even not update@Override protected void onResume() { super.onResume(); dbhelper = new MyDbHelper(this); dbhelper.onOpen(db); populateList(); } – p. ld Nov 17 '15 at 04:43
  • Can someone help me ? – p. ld Nov 17 '15 at 07:12