0

I am refering Android ListView Refresh Single Row

However, to use the above link technique, I have to make ListView visible to ArrayAdapter.

Currently, in order to have a very optimized way to update display in ListView, I need to do this to my ArrayAdapter

public PortfolioArrayAdapter(ListView listView, Activity activity, List<TransactionSummary> transactionSummaries) {
    super(activity, R.layout.portfolio_management_row_layout, transactionSummaries);
    this.activity = activity;
    this.listView = listView;
    ...
}

// Do not use notifyDataSetChanged, as it will refresh entire ListView.
// We only want to update certain row.
private void refreshView(final int row) {
    final int firstVisiblePosition = listView.getFirstVisiblePosition();
    final int lastVisiblePosition = listView.getLastVisiblePosition();
    if (row < firstVisiblePosition || row > lastVisiblePosition) {
        return;
    }
    this.activity.runOnUiThread(new Runnable() {
        public void run() {
            final View view = listView.getChildAt(row - firstVisiblePosition);
            // Refresh?
            getView(row, view, listView);                
        }
    });
}

However, I feel uncomfortable to pass ListView reference to ArrayAdapter, as I thought they should be independent from each others? (Due to MVC?)

Is there any better way I can let ListView Refresh Single Row?

In Java Swing, they did it in a pretty neat way. They provide the following method for a table model.

fireTableCellUpdated(row, col);

So, that model can tell view which row and col is being affected, without knowing who the view is.

In Andorid, my model (ArrayAdapter) needs to know, ListView is the view, only then my model can talk to the view.

Community
  • 1
  • 1
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • As far as i know, all you can do is update dataset and notify adapter via notifyDatasetChanged(). Still if there is a performance problem (hard to create one on list view :P), you might hold a weak reference to it for direct access. – Gökhan Barış Aker Jul 28 '12 at 18:12

1 Answers1

1

I'm not sure why you think your ArrayAdapter shouldn't be able to have a reference to your ListView. ListView is your View and ArrayAdapter your Model, so they need to interact. From wikipedia:

A model notifies its associated views and controllers when there has been a change in its state. This notification allows the views to produce updated output, and the controllers to change the available set of commands

Isn't this exactly how the principle of listeners work, you add the view to a list of listening objects and notify them on any change.

Joren Van Severen
  • 2,269
  • 2
  • 24
  • 30
  • Theory of MVC is that, model and view should be seperated from each others. If u did Java swing programming on their table, u will know what I mean. If you look at my updated post regarding Swing, you will know why I am having this concern. – Cheok Yan Cheng Jul 28 '12 at 17:51
  • Yes, separated as in different classes and be able to use the model for different views and vica versa, you can still use a different view. But that doesn't change when you reference the view in the model. This site explains it well: http://www.moock.org/lectures/mvc/ Look at object references in mvc. – Joren Van Severen Jul 28 '12 at 17:57