gpt4 book ai didi

listview - 重新加载 ListView 不工作 React Native

转载 作者:行者123 更新时间:2023-12-02 22:03:12 24 4
gpt4 key购买 nike

重新表述我的问题...(使用单独的类(class)进行测试)

我试图在按下按钮时使用随机数组重新加载listview。但这不起作用!尝试使用以下 LOC 刷新数据源:

_buttonPressed() {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(this._randomArray()),
});
}

_randomArray() {

var jsonRes;

switch(this.getRandomInt()) // number returned is valid. I checked
{
case 1:
{
jsonRes = '[' +
'{ "firstName":"Michel" , "lastName":"John" ,"age":18},' +
'{ "firstName":"Richard" , "lastName":"Joe","age":20 },' +
'{ "firstName":"James" , "lastName":"Henry","age":15 },' +
'{ "firstName":"e" , "lastName":"u","age":15 }]';
break;
}
case 2:
{
jsonRes = '[' +
'{ "firstName":"bacde" , "lastName":"John" ,"age":18},' +
'{ "firstName":"bacde" , "lastName":"Joe","age":20 },' +
'{ "firstName":"bacde" , "lastName":"Henry","age":15 },' +
'{ "firstName":"ebacde" , "lastName":"u","age":15 }]';
break;
}
case 3:
{
jsonRes = '[' +
'{ "firstName":"mmmmm" , "lastName":"John" ,"age":18},' +
'{ "firstName":"mmmmm" , "lastName":"Joe","age":20 },' +
'{ "firstName":"mmmmm" , "lastName":"Henry","age":15 },' +
'{ "firstName":"etttttt" , "lastName":"u","age":15 }]';
break;
}
default:
{
jsonRes = '[' +
'{ "firstName":"dddd" , "lastName":"John" ,"age":18},' +
'{ "firstName":"dddd" , "lastName":"Joe","age":20 },' +
'{ "firstName":"dddd" , "lastName":"Henry","age":15 },' +
'{ "firstName":"ehhgh" , "lastName":"u","age":15 }]';
break;
}
}

var myObject = eval('(' + jsonRes + ')');
return myObject;

}

我已经看过 React Native 的示例 ListView 项目。他们使用 dataSource.cloneWithRows 来实现此目的。不知道我错过了什么小东西。请帮我解决这个问题...谢谢!

最佳答案

我已经能够成功调试该问题。我刚刚打开了 ListViewDataSource.js 的源代码,并跟踪了沿不同函数传递的 datablob 变量值。

方法_calculateDirtyArrays标识哪些行已更改并且需要更新。为此,三个条件之一必须与一行匹配。

 ........
// dirty if the section is new, row is new or _rowHasChanged is true
dirty =
!prevSectionsHash[sectionID] ||
!prevRowsHash[sectionID][rowID] ||
this._rowHasChanged(
this._getRowData(prevDataBlob, sectionID, rowID),
this._getRowData(this._dataBlob, sectionID, rowID)
);
this._dirtyRows[sIndex].push(!!dirty);
........

_rowHasChanged是传递给List Datasource构造函数的。

var dataSource = new ListView.DataSource(
{rowHasChanged: (r1, r2) => r1.guid !== r2.guid});

在我的例子中这是错误的,因为当相应的数据更改时应该更改行。我根据我的要求更改了上述条件。

var dataSource = new ListView.DataSource(
{
rowHasChanged: function(r1, r2) : bool {
return (
(r1["firstName"] !== r2["firstName"]) ||
(r1["lastName"] !== r2["lastName"]) ||
(r1["age"] !== r2["age"])
);
}
});

瞧!现在效果很完美。希望它对将来的人有所帮助!

关于listview - 重新加载 ListView 不工作 React Native,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30069985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com