在另一个视图中更改后更新列表视图

时间:2013-09-09 12:37:55

标签: android listview

我有一个listView,当你点击加号时,它会导致第二个活动,你可以在其中添加数据并点击OK。单击“确定”后,应更新列表中上一个活动的数据。 我在各种地方尝试了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();

}

1 个答案:

答案 0 :(得分:0)

最简单的建议(不优雅)就是

adapter= new EditAdapter(getApplicationContext(),sectionTopic);
setListAdapter(adapter);

在EditActivity的onResume()函数中。

正确的方法是调用startActivityForResult,这样你的第二个活动就可以传回添加的内容