更新列表视图条目

时间:2013-12-29 14:30:31

标签: android android-listview

我的自定义ListView更新条目存在问题。我已实现自定义ArrayAdapter,其填充ListView,如下所示:

public class MealPartCustomAdapter extends ArrayAdapter<MealPart> {
    private final Context context;
    private final ArrayList<MealPart> mealParts;

    public MealPartCustomAdapter(Context context, ArrayList<MealPart> mealParts) {
        super(context, R.layout.add_meal_list_view_row, mealParts);
        this.context = context;
        this.mealParts = mealParts;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.add_meal_list_view_row, parent, false);

        AutoCompleteTextView mealPartProductNameTextView = (AutoCompleteTextView) rowView.findViewById(R.id.mealPartAutoCompleteTextView);
        mealPartProductNameTextView.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, values));
        EditText quantityEditText = (EditText) rowView.findViewById(R.id.mealQuantityEditText);
        mealPartProductNameTextView.setText(mealParts.get(position).productName);
        quantityEditText.setText(Float.toString(mealParts.get(position).foodQuantity));

        return rowView;
    }
}

MealPart由两个字段组成 - food中的foodName和foodQuantity。如何实施此类数据的更新行,并查看我传递给自定义ArrayList<MealPart>的{​​{1}}中的更改?

更新好看了几个答案之后,我想先尝试一下。我知道这些数据会显示出来,我可以编辑它。我还可以通过以下代码添加新行:

ArrayAdapter

但是我无法达到在其中一行中的edittext之内连接到文本更改的事件处理程序以及如何在底层中反映此更改ArrayList 以及在哪里触发public void onAddMealPartButtonClick(View view) { mealPartsAdapter.add(new MealPart()); }

这是onCreate方法。我猜这里什么都没有。

notifyDatasetChanged

3 个答案:

答案 0 :(得分:1)

YOUR_ADAPTER.notifyDatasetChanged();

答案 1 :(得分:0)

如果要在自定义适配器mealPartCustomAdapterInstance.notifyDataSetChanged()的现有集合中添加/删除/操作项,那么就可以了。但请确保您在UI Thread中调用它(如果您尝试在单独的帖子中刷新它,请在runOnUiThread()内调用它)

答案 2 :(得分:0)

您只需要调用notifyDatasetChanged()方法,它就会更新,就像那样简单。