在不活动的活动之间传递数据

时间:2015-08-18 06:48:20

标签: java android adapter

我在一个简单的应用程序中有以下场景: 1-用户单击MainActivity上的菜单按钮以在全新屏幕上打开另一个活动:addTimeActivity,他们可以在其中输入注释及其时间。 2-新活动有两个编辑文本:时间和注释,用户输入数据然后单击保存,或者他们可以在不想保存注释时单击取消按钮。 3- addTimeActiviy通过intent传递数据。 4- addTimeActivity完成。 5-数据应该保存在适配器中,而适配器又将其显示为MainActivity上的列表,TimeTrackerAdapter中的方法addTimeRecord将String数据添加到时间ArrayList。 现在,当单击保存按钮时,会出现NullPointerException。

这是MainActivity:

public class MainActivity extends ActionBarActivity {

ListView listView;
TimeTrackerAdapter timeTrackerAdapter;
public static final int TIME_ENTRY_REQUEST_CODE=1; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    timeTrackerAdapter=new TimeTrackerAdapter();
     listView=(ListView) findViewById(R.id.recordList);
    listView.setAdapter(timeTrackerAdapter);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    super.onCreateOptionsMenu(menu);
    MenuInflater menuInflater=getMenuInflater();
    menuInflater.inflate(R.menu.menu_list_item, menu);
    return true;
}
protected void onActivityResult(int requestCode,int resultCode,Intent data)
{
    if(requestCode==TIME_ENTRY_REQUEST_CODE)
        {
        if(resultCode==RESULT_OK)
        {   
        Bundle extras=getIntent().getExtras();
        String time=extras.getString("time");
        String notes=extras.getString("notes");
        timeTrackerAdapter.addTimeRecord(time, notes);
        timeTrackerAdapter.notifyDataSetChanged();  
        }
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
   if(item.getItemId()==R.id.add_time_menu_item)
   {
       Intent intent=new Intent(getApplicationContext(),AddTimeActivity.class);
       startActivityForResult(intent,TIME_ENTRY_REQUEST_CODE);
       return true;
   }
    return super.onOptionsItemSelected(item);
} }

这是addTimeActivity:

public class AddTimeActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_time);
}
public void onCancel(View view)
{
    finish();
}
public void onSave(View view)
{
    Intent intent = new Intent(AddTimeActivity.this,MainActivity.class);
    EditText editTextNotes=(EditText) findViewById(R.id.notes_edit_text);
    intent.putExtra("notes",editTextNotes.getText().toString());
    EditText editTextTime=(EditText) findViewById(R.id.time_edit_text);
    intent.putExtra("time", editTextTime.getText().toString());
    this.setResult(RESULT_OK,intent);
    startActivity(intent);
    finish();

}
}

适配器:

public class TimeTrackerAdapter extends BaseAdapter{

ArrayList<TimeRecord> times=new ArrayList<TimeRecord>();
TextView timeView,noteView;
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return times.size();
}
public TimeTrackerAdapter()
{
}

@Override
public Object getItem(int index) {
    // TODO Auto-generated method stub
    return getItem(index);
}

@Override
public long getItemId(int index) {
    // TODO Auto-generated method stub
    return index;
}

public void addTimeRecord(String time,String notes)
{
    TimeRecord timeRecord=new TimeRecord(time,notes);
    times.add(timeRecord);
}
@Override
public View getView(int index, View view, ViewGroup parent) {
    // TODO Auto-generated method stub
    if(view==null)
    {
        LayoutInflater inflater=LayoutInflater.from(parent.getContext());
        view=inflater.inflate(R.layout.time_tracker, parent,false);
    }
    TimeRecord time=times.get(index);
    TextView timeText=(TextView) view.findViewById(R.id.timeText);
    timeText.setText(time.getTime());
    TextView noteText=(TextView) view.findViewById(R.id.noteText);
    noteText.setText(time.getNote());
    return view;
}
}

1 个答案:

答案 0 :(得分:1)

您没有初始化您的成员变量timeTrackerAdapter!在您的onCreate中执行此操作TimeTrackerAdapter timeTrackerAdapter=new TimeTrackerAdapter();但应该是timeTrackerAdapter=new TimeTrackerAdapter();

你也在onActivityResult中使用了错误的意图。它应该是这样的:

protected void onActivityResult(int requestCode,int resultCode,Intent data) // Intent data is the intent that includes the data of your AddTimeActivity
{
  if(requestCode==TIME_ENTRY_REQUEST_CODE)
  {
    if(resultCode==RESULT_OK)
    {   
      Bundle extras=data.getExtras(); // not getIntent().getExtras()
      String time=extras.getString("time");
      String notes=extras.getString("notes");
      timeTrackerAdapter.addTimeRecord(time, notes);
      timeTrackerAdapter.notifyDataSetChanged();  
    }
  }
}