我正在使用平面列表来显示数组的内容。当array.length = 1时,不显示内容,但如果它的长度> 1,则按预期显示内容。
这是我的代码:
return (
<FlatList data={this.state.services}
renderItem={({item})=> {
var query = "select * from service_details where service_id = " + item;
var all_national_numbers = [];
db.transaction((tx) =>{
tx.executeSql(query,[],(tx,results) =>{
for(let i = 0 ; i < results.rows.length ; i++){
let row = results.rows.item(i);
all_national_numbers[i] = row.national_number;
}
var len = all_national_numbers.length ;
console.log(len)
});
});
return(
<FlatList
data={all_national_numbers}
renderItem={({item}) => {
if(all_national_numbers.length ==1){
return(
<View>
<Text>{item}</Text>
</View>
)
} else{
return(
<View>
<Text>{item}</Text>
</View>
)
}
}}
/>
)
}}
/>
)