我有一个listView,当您单击加号时,它会导致第二个活动,您可以在其中添加数据并单击确定。单击“确定”时,列表中上一个活动的数据应该会更新。我在各种地方尝试了 invalidateViews() 和 notifyDataSetChanged() ,但没有奏效。
这是列表活动:
public class EditActivity extends ListActivity {
EditAdapter adapter;
String itemName;
String sectionTopic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extra = getIntent().getExtras();
sectionTopic = extra.getString("topic");
View header = getLayoutInflater().inflate(R.layout.header, null);
getListView().addHeaderView(header);
adapter= new EditAdapter(getApplicationContext(),sectionTopic);
setListAdapter(adapter);
}
}
适配器:
public class EditAdapter extends BaseAdapter {
private List<EditSection> arrayList= new ArrayList<EditSection>();
private Context mContext;
String topicNameS;
public EditAdapter(Context mContext, String topicName) {
super();
this.mContext = mContext;
topicNameS = topicName;
arrayList = new ArrayList<EditSection>();
DataModel dataModel = new DataModel(mContext);
List<SectionItems> items = dataModel.getSectionsForTopic(topicName);
for (SectionItems item: items){
double sectionBudget = dataModel.getBudgetForSection(item);
double sectionExpense = dataModel.getSumOfExpensesForSection(item);
arrayList.add(new EditSection(item.getSectionItemsPic(),item.getSectionItemsName(), sectionBudget, sectionExpense));
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arrayList.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View vowView = inflater.inflate(R.layout.activity_edit, parent, false);
if (position%2==1){
vowView.setBackgroundColor(Color.rgb(170, 218, 203));
}
else{
vowView.setBackgroundColor(Color.rgb(147, 213, 212));
}
TextView name = (TextView) vowView.findViewById(R.id.name);
TextView budget = (TextView) vowView.findViewById(R.id.budget);
TextView expense = (TextView) vowView.findViewById(R.id.expense);
ImageView pic = (ImageView) vowView.findViewById(R.id.pic);
View itemV = vowView.findViewById(R.id.item);
TextView button = (TextView) vowView.findViewById(R.id.addBtn); return vowView;
}
}
以及数据更改的第二个活动中的 OK 按钮:
public void conformation (View v){
DataModel dataModel = new DataModel(getBaseContext());
Bundle extra = getIntent().getExtras();
String itemId = extra.getString("title");
SectionItems item = DataModel.getItemForId(itemId);
Calendar calendar = Calendar.getInstance();
calendar.set(yy, mm, dd);
Date dateNew= calendar.getTime();
if (isUpdate){
dataModel.updateExpense(dateNew, rowId, item, num, Double.parseDouble(sectionAmount));
} else if (num!=0 ){
dataModel.addExpense(dateNew, item, num);
}
finish();
}