krrish
krrish

Reputation: 343

Cannot update listview through activity

I have created an listview based app in which when I add data through another activity the data doesn't 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 list view.

mainactivity.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

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();
    }
}

timetrackeradapter.java

    package com.example.listviewdemo;

import java.util.ArrayList;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class TimeTrackerAdapter extends BaseAdapter {
    private ArrayList<TimeRecord> times = new ArrayList<TimeRecord>();
    public TimeTrackerAdapter() {
        times.add(new TimeRecord(
                "38:23", "Feeling good!"));
                times.add(new TimeRecord(
                "49:01", "Tired. Needed more caffeine"));
                times.add(new TimeRecord(
                "26:21", "I’m rocking it!"));
                times.add(new TimeRecord(
                "29:42", "Lost some time on the hills, but pretty good."));     
    }

    @Override
    public int getCount() {
        return times.size();
    }

    @Override
    public Object getItem(int index) {
        return getItem(index);
    }

    @Override
    public long getItemId(int index) {
        return index;
    }

    @Override
    public View getView(int index, View view, ViewGroup parent) {
        if (view == null) {
            LayoutInflater inflater =
                    LayoutInflater.from(parent.getContext());
            view = inflater.inflate(
                    R.layout.time_list_item, parent, false);
        }
        TimeRecord time = times.get(index);
        TextView timeTextView = (TextView)
                view.findViewById(R.id.time_view);
        timeTextView.setText(time.getTime());
        TextView notesTextView = (TextView)
                view.findViewById(R.id.notes_view);
        notesTextView.setText(time.getNotes());
        return view;

    }

    public void addTimeRecord(TimeRecord timeRecord) {
        // TODO Auto-generated method stub

    }





}

Upvotes: 0

Views: 138

Answers (2)

Gokhan Arik
Gokhan Arik

Reputation: 2766

Why is your addTimeRecord is empty?

Try this,

public void addTimeRecord(TimeRecord timeRecord) {
    times.add(timeRecord);
}

Upvotes: 0

PearsonArtPhoto
PearsonArtPhoto

Reputation: 39698

Your problem is this line:

public void addTimeRecord(TimeRecord timeRecord) {
    // TODO Auto-generated method stub

}

You haven't actually implemented this function, and so it won't do anything! My guess is you want to do this:

public void addTimeRecord(TimeRecord timeRecord) {
    times.add(timeRecord);
    notifyDataSetChanged();
}

Note that with this, you don't need the redundant notifyDataSetChanged() in your first activity.

Upvotes: 1

Related Questions