我创建了一个基于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();
}
}
}
}
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
}
}
答案 0 :(得分:1)
你的问题就在这一行:
public void addTimeRecord(TimeRecord timeRecord) {
// TODO Auto-generated method stub
}
您还没有真正实现此功能,因此它无法做任何事情!我的猜测是你要这样做:
public void addTimeRecord(TimeRecord timeRecord) {
times.add(timeRecord);
notifyDataSetChanged();
}
请注意,在此过程中,您不需要在第一项活动中使用多余的notifyDataSetChanged()
。
答案 1 :(得分:0)
为什么addTimeRecord
为空?
试试这个,
public void addTimeRecord(TimeRecord timeRecord) {
times.add(timeRecord);
}