Java 无法通过活动更新listview

Java 无法通过活动更新listview,java,android,listview,android-listview,android-activity,Java,Android,Listview,Android Listview,Android Activity,我创建了一个基于listview的应用程序,当我通过另一个活动添加数据时,数据不会在listview中更新,因为它应该在listview中更新 当我在“活动”中的“保存”按钮上创建了一个onSave方法,但它似乎没有在列表视图中得到更新时 mainactivity.java package com.example.listviewdemo; import android.app.Activity; import android.content.Intent; import android.os

我创建了一个基于
listview
的应用程序,当我通过另一个活动添加数据时,数据不会在
listview
中更新,因为它应该在listview中更新

当我在“活动”中的“保存”按钮上创建了一个
onSave
方法,但它似乎没有在列表视图中得到更新时

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



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

    }





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



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

    }





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



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

    }





}
package com.example.listviewdemo;
导入java.util.ArrayList;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.TextView;
公共类TimeTrackerAdapter扩展BaseAdapter{
private ArrayList times=new ArrayList();
公共时间跟踪器适配器(){
添加(新的时间记录)(
“38:23”,“感觉很好!”;
添加(新的时间记录)(
“49:01”,“累了,需要更多的咖啡因”);
添加(新的时间记录)(
“26:21”,“我在摇它!”;
添加(新的时间记录)(
“29:42”,“在山上耽误了一些时间,但还不错。”);
}
@凌驾
public int getCount(){
返回次数。size();
}
@凌驾
公共对象getItem(int索引){
返回项目(索引);
}
@凌驾
公共长getItemId(int索引){
收益指数;
}
@凌驾
公共视图getView(int索引、视图视图、视图组父级){
如果(视图==null){
充气机=
LayoutInflater.from(parent.getContext());
视图=充气机。充气(
R.layout.time\u list\u项,父项,false);
}
TimeRecord time=times.get(索引);
TextView timeTextView=(TextView)
view.findViewById(R.id.time\u视图);
timeTextView.setText(time.getTime());
TextView notesTextView=(TextView)
视图。findViewById(R.id.notes\u视图);
notesTextView.setText(time.getNotes());
返回视图;
}
public void addTimeRecord(TimeRecord TimeRecord){
//TODO自动生成的方法存根
}
}

您的问题是这一行:

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

}
你还没有真正实现这个函数,所以它不会做任何事情!我猜你想这样做:

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

请注意,这样一来,在第一个活动中就不需要冗余的
notifyDataSetChanged()

为什么
addTimeRecord
是空的

试试这个

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

开始倒转。有人打电话给你吗?你是在你的if声明里说的吗?继续这样做,直到找到问题。我没有得到问题,我甚至尝试在断点的帮助下进行调试,但是@PearSonartPhoto没有任何用处您从什么角度确认了事情似乎在运行?事情一直在运行,除了数据没有添加到列表视图中,因为它应该是这样的。您确实需要包含
TimeTrackerAdapter
的代码,因为您拥有的一切似乎都在正常运行,但是我不确定什么是
TimeTrackerAdapter
,更不用说它是否做了正确的事情了。呃,这就解决了,当创建onActivityResult AddTimeRecord时产生了错误,为此我生成了自动生成的方法来抑制错误。谢谢你是的,因为我说它变成了一个自动生成的方法,所以我没有太多的关注它,我的无知。它与时间一起工作。添加(时间记录);非常感谢。