Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have created an listview based app in which when i add data through another activity the data doesnt get updated in the listview where as it should get updated in the listview !!

when i have created an onSave method on the save button in the activity but it doesnt seem to get updated in the lsit view.

mainactivity.java

Java
package com.example.listviewdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;

public class MainActivity extends Activity {
    TimeTrackerAdapter timeTrackerAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView = (ListView)
                findViewById(R.id.times_list);
                timeTrackerAdapter = new TimeTrackerAdapter();
                listView.setAdapter(timeTrackerAdapter);
    }

    public boolean onCreateOptionsMenu(Menu m) {
        super.onCreateOptionsMenu(m);
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.time_list_menu, m );
        return true;
    }

    public static final int TIME_ENTRY_REQUEST_CODE = 1;
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        if (item.getItemId() == R.id.add_time_menu_item) {
            Intent intent = new Intent(this, AddTimeActivity.class);
            startActivityForResult(intent, TIME_ENTRY_REQUEST_CODE);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == TIME_ENTRY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                String notes = data.getStringExtra("notes");
                String time = data.getStringExtra("time");

                timeTrackerAdapter.addTimeRecord( new TimeRecord(time, notes));
                timeTrackerAdapter.notifyDataSetChanged();
            }
            }
    }



}


addtimeactivity.java

Java
package com.example.listviewdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class AddTimeActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_time);
        }
    public void onCancel(View view) {
        finish();
    }
    public void onSave(View view) {
        Intent intent = getIntent();

        EditText timeView = (EditText)findViewById(R.id.time_view);
        intent.putExtra("time", timeView.getText().toString());

        EditText notesView = (EditText)findViewById(R.id.notes_view);
        intent.putExtra("notes", notesView.getText().toString());

        this.setResult(RESULT_OK, intent);
        finish();
    }
}
Posted
Comments
dhara.jogi 6-May-14 9:02am    
can you post your adapter class or what does this method do
timeTrackerAdapter.addTimeRecord( new TimeRecord(time, notes));
can you please add details

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900