Permalink
Browse files

Merge pull request #3386 from pingwang2011/timob-11523

Timob 11523: Android: TableView - delete row cannot delete a row without giving it a number, but iOS can
  • Loading branch information...
2 parents ddc7b52 + 3505aab commit acf0f29f92c44803c73675ff2a74648aaaaf2e30 @vishalduggal vishalduggal committed Nov 20, 2012
@@ -205,7 +205,14 @@ public void updateSection(Number index, Object data, @Kroll.argument(optional =
}
try {
ArrayList<TableViewSectionProxy> currentSections = getSectionsArray();
+ TableViewSectionProxy oldSection = currentSections.get(sectionIndex);
currentSections.set(sectionIndex, sectionProxy);
+ if (sectionProxy != oldSection) {
+ sectionProxy.setParent(this);
+ if (oldSection.getParent() == this) {
+ oldSection.setParent(null);
+ }
+ }
getTableView().setModelDirty();
updateView();
} catch (IndexOutOfBoundsException e) {
@@ -247,9 +254,8 @@ private void handleAppendRow(Object rows)
TableViewSectionProxy addedToSection = addRowToSection(rowProxy, lastSection);
if (lastSection == null || !lastSection.equals(addedToSection)) {
sections.add(addedToSection);
+ addedToSection.setParent(this);
}
- rowProxy.setProperty(TiC.PROPERTY_SECTION, addedToSection);
- rowProxy.setProperty(TiC.PROPERTY_PARENT, addedToSection);
}
}
@@ -283,6 +289,7 @@ private void handleAppendSection(Object sections)
TableViewSectionProxy sectionProxy = sectionProxyFor(sectionList[i]);
if (sectionProxy != null) {
currentSections.add(sectionProxy);
+ sectionProxy.setParent(this);
}
}
@@ -291,29 +298,44 @@ private void handleAppendSection(Object sections)
}
@Kroll.method
- public void deleteRow(int index, @Kroll.argument(optional = true) KrollDict options)
+ public void deleteRow(Object row, @Kroll.argument(optional = true) KrollDict options)
{
if (TiApplication.isUIThread()) {
- handleDeleteRow(index);
+ handleDeleteRow(row);
return;
}
- Object asyncResult = TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_DELETE_ROW), index);
+ Object asyncResult = TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_DELETE_ROW), row);
if (asyncResult instanceof IllegalStateException) {
throw (IllegalStateException) asyncResult;
}
}
- private void handleDeleteRow(int index) throws IllegalStateException
+ private void handleDeleteRow(Object row) throws IllegalStateException
{
- RowResult rr = new RowResult();
- if (locateIndex(index, rr)) {
- rr.section.removeRowAt(rr.rowIndexInSection);
- getTableView().setModelDirty();
- updateView();
+ if (row instanceof Integer) {
+ int index = (Integer) row;
+ RowResult rr = new RowResult();
+ if (locateIndex(index, rr)) {
+ rr.section.removeRowAt(rr.rowIndexInSection);
+ getTableView().setModelDirty();
+ updateView();
+ } else {
+ throw new IllegalStateException("Unable to delete row. Index out of range. Non-existent row at " + index);
+ }
+ } else if (row instanceof TableViewRowProxy) {
+ TableViewRowProxy rowProxy = (TableViewRowProxy) row;
+ TiViewProxy section = rowProxy.getParent();
+ if (section instanceof TableViewSectionProxy) {
+ ((TableViewSectionProxy) section).remove(rowProxy);
+ getTableView().setModelDirty();
+ updateView();
+ } else {
+ Log.e(TAG, "Unable to delete row. The row is not added to the table yet.");
+ }
} else {
- throw new IllegalStateException("Index out of range. Non-existent row at " + index);
+ Log.e(TAG, "Unable to delete row. Invalid type of row: " + row);
}
}
@@ -336,7 +358,11 @@ private void handleDeleteSection(int index) throws IllegalStateException
{
ArrayList<TableViewSectionProxy> currentSections = getSectionsArray();
try {
+ TableViewSectionProxy section = currentSections.get(index);
currentSections.remove(index);
+ if (section.getParent() == this) {
+ section.setParent(null);
+ }
getTableView().setModelDirty();
updateView();
} catch (IndexOutOfBoundsException e) {
@@ -432,6 +458,7 @@ private void handleInsertSectionBefore(int index, Object data) throws IllegalSta
try {
ArrayList<TableViewSectionProxy> currentSections = getSectionsArray();
currentSections.add(index, sectionProxy);
+ sectionProxy.setParent(this);
getTableView().setModelDirty();
updateView();
} catch (IndexOutOfBoundsException e) {
@@ -499,6 +526,7 @@ private void handleInsertSectionAfter(int index, Object data) throws IllegalStat
try {
ArrayList<TableViewSectionProxy> currentSections = getSectionsArray();
currentSections.add(index+1, sectionProxy);
+ sectionProxy.setParent(this);
getTableView().setModelDirty();
updateView();
} catch (IndexOutOfBoundsException e) {
@@ -564,13 +592,15 @@ public void processData(Object[] data)
currentSection = new TableViewSectionProxy();
currentSection.setActivity(getActivity());
sections.add(currentSection);
+ currentSection.setParent(this);
currentSection.setProperty(TiC.PROPERTY_HEADER_TITLE, getProperty(TiC.PROPERTY_HEADER_TITLE));
}
if (hasProperty(TiC.PROPERTY_FOOTER_TITLE)) {
if (currentSection == null) {
currentSection = new TableViewSectionProxy();
currentSection.setActivity(getActivity());
sections.add(currentSection);
+ currentSection.setParent(this);
}
currentSection.setProperty(TiC.PROPERTY_FOOTER_TITLE, getProperty(TiC.PROPERTY_FOOTER_TITLE));
}
@@ -583,6 +613,7 @@ public void processData(Object[] data)
if (currentSection == null || !currentSection.equals(addedToSection)) {
currentSection = addedToSection;
sections.add(currentSection);
+ currentSection.setParent(this);
}
} else if (o instanceof TableViewSectionProxy) {
currentSection = (TableViewSectionProxy) o;
@@ -661,7 +692,6 @@ private TableViewRowProxy rowProxyFor(Object row)
return null;
}
- rowProxy.setParent(this);
return rowProxy;
}
@@ -696,12 +726,13 @@ private TableViewSectionProxy sectionProxyFor(Object section)
sectionProxy.setActivity(getActivity());
}
}
+
if (sectionProxy == null) {
Log.e(TAG,
"Unable to create table view section proxy for object, likely an error in the type of the object passed in...");
return null;
}
- sectionProxy.setParent(this);
+
return sectionProxy;
}
@@ -814,7 +845,7 @@ public boolean handleMessage(Message msg)
} else if (msg.what == MSG_DELETE_ROW) {
AsyncResult result = (AsyncResult) msg.obj;
try {
- handleDeleteRow((Integer) result.getArg());
+ handleDeleteRow(result.getArg());
result.setResult(null);
} catch (IllegalStateException e) {
result.setResult(e);
@@ -9,6 +9,7 @@
import java.util.ArrayList;
import org.appcelerator.kroll.annotations.Kroll;
+import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiContext;
import org.appcelerator.titanium.proxy.TiViewProxy;
@@ -22,6 +23,7 @@
})
public class TableViewSectionProxy extends TiViewProxy
{
+ private static final String TAG = "TableViewSectionProxy";
protected ArrayList<TableViewRowProxy> rows = new ArrayList<TableViewRowProxy>();
public TableViewSectionProxy()
@@ -67,9 +69,7 @@ public void add(TableViewRowProxy rowProxy)
{
if (rowProxy != null) {
rows.add(rowProxy);
- if (rowProxy.getParent() == null) {
- rowProxy.setParent(this);
- }
+ rowProxy.setParent(this);
}
}
@@ -95,19 +95,48 @@ public TableViewRowProxy rowAtIndex(int index)
}
@Kroll.method
- public void insertRowAt(int index, TableViewRowProxy row) {
- rows.add(index, row);
+ public void insertRowAt(int index, TableViewRowProxy row)
+ {
+ if (index > -1 && index <= rows.size()) {
+ rows.add(index, row);
+ row.setParent(this);
+ } else {
+ Log.e(TAG, "Index out of range. Unable to insert row at index " + index, Log.DEBUG_MODE);
+ }
}
@Kroll.method
- public void removeRowAt(int index) {
- rows.remove(index);
+ public void removeRowAt(int index)
+ {
+ if (index > -1 && index < rows.size()) {
+ TableViewRowProxy rowProxy = rows.get(index);
+ rows.remove(index);
+ if (rowProxy.getParent() == this) {
+ rowProxy.setParent(null);
+ }
+ } else {
+ Log.e(TAG, "Index out of range. Unable to remove row at index " + index, Log.DEBUG_MODE);
+ }
}
+
@Kroll.method
- public void updateRowAt(int index, TableViewRowProxy row) {
- rows.set(index, row);
+ public void updateRowAt(int index, TableViewRowProxy row)
+ {
+ TableViewRowProxy oldRow = rows.get(index);
+ if (row == oldRow) {
+ return;
+ }
+ if (index > -1 && index < rows.size()) {
+ rows.set(index, row);
+ row.setParent(this);
+ if (oldRow.getParent() == this) {
+ oldRow.setParent(null);
+ }
+ } else {
+ Log.e(TAG, "Index out of range. Unable to update row at index " + index, Log.DEBUG_MODE);
+ }
}
-
+
@Override
public String toString() {
return "[object TableViewSectionProxy]";
@@ -440,14 +440,14 @@ protected boolean rowClicked(TiBaseTableViewItem rowView, int position, boolean
if(item.proxy != null && item.proxy instanceof TableViewRowProxy) {
TableViewRowProxy rp = (TableViewRowProxy) item.proxy;
event.put(TiC.EVENT_PROPERTY_SOURCE, rp);
- if (rp.hasListeners(eventName)) {
+ // The event will bubble up to the parent.
+ if (rp.hierarchyHasListener(eventName)) {
rp.fireEvent(eventName, event);
}
}
if (longClick) {
return itemLongClickListener.onLongClick(event);
} else {
- itemClickListener.onClick(event);
return false; // standard (not-long) click handling has no return value.
}
}
@@ -700,10 +700,11 @@ methods:
summary: Deletes an existing row.
description: |
On iOS, the row can be deleted with animation by specifying a `properties` parameter.
+ Starting in SDK 3.1.0, the row can be specified using <Titanium.UI.TableViewRow> on Android and iOS.
parameters:
- name: row
- summary: Index of the row to delete.
- type: Number
+ summary: Index of the row to delete, or the row object to delete.
+ type: [Number, Titanium.UI.TableViewRow]
- name: animation
summary: Animation properties. (iOS only.)

0 comments on commit acf0f29

Please sign in to comment.