Android 在搜索中键入内容时,如何更新列表?

Android 在搜索中键入内容时,如何更新列表?,android,json,reactjs,react-native,Android,Json,Reactjs,React Native,当我在搜索框中输入内容时,如何使用搜索功能更新列表 这就是我在文本输入(搜索框)中尝试的内容。 search = () => { fetch(strings.baseUri+"search_by_name", { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json'

当我在搜索框中输入内容时,如何使用搜索功能更新列表

这就是我在文本输入(搜索框)中尝试的内容。

 search = () => {
      fetch(strings.baseUri+"search_by_name", {
       method: 'POST',
       headers: {
              Accept: 'application/json',
              'Content-Type': 'application/json'
            },
            body: JSON.stringify({ 
              "name": this.state.name,
            })
          })
          .then((response) => response.json())
          .then((responseJson) => {

             this.setState({data: responseJson});     

         })
          .catch((error) => {
              console.error(error);
          });    
      }
{
this.setState({name});
这是search();
}
}
/>

首先
responseJson
是一个实际的json对象,您不需要对其进行字符串化然后对其进行解析

您可以将响应JSON存储在状态中

<TextInput style={styles.searchInput} 
 placeholder="Search"
 placeholderTextColor= {colors.whiteColor}
 onChangeText={ 
 name => { 
 this.setState({name}); 
 this.search(); 
}
 }
 />
只需确保在组件的构造函数中设置初始状态

search = () => {
      fetch(strings.baseUri+"search_by_name", {
        method: 'POST',
        headers: {
          Accept: 'application/json',
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({ 
          "name": this.state.name,
        })
      })
      .then((response) => response.json())
      .then((responseJson) => {
        this.setState({data: responseJson});    
     })
      .catch((error) => {
          console.error(error);
      });    
  }
然后,您应该能够使用
this.state.data

更新问题的最新答案 问题是您没有意识到setState是异步的。这需要时间来更新状态。这意味着您正在设置状态,然后希望在调用下一个函数时状态已经更新。不幸的是,在TextInput中调用setState没有达到预期效果

我会将您的搜索功能更新为以下内容

constructor(props) {
  super(props);
  this.state = { data: [] }
}
然后在
文本输入中
将函数调用更新为

search = (name) => {
      fetch(strings.baseUri+"search_by_name", {
        method: 'POST',
        headers: {
          Accept: 'application/json',
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({ 
          "name": name,
        })
      })
      .then((response) => response.json())
      .then((responseJson) => {
        this.setState({data: responseJson, name: name});    
     })
      .catch((error) => {
          this.setState({name: name});
          console.error(error);
      });    
  }
{
如果(name.length>0){
本.搜索(名称);
}否则{
this.setState({data:[],名称:''});
}
}
}
/>
这将减少调用setState的次数。

uhm。。。 我不知道“身体”部分是干什么用的。。 我指的是那个角色 正文:JSON.stringify({“name”:this.state.name}) 也许像这样更好 正文:[{'name':bla}]

无论如何,请尝试以下操作(并将“Accept”头作为字符串写入): (如果对您非常重要,请添加身体部位)

const fetchConfig={
方法:“POST”,
标题:{
“接受”:“应用程序/json”,
“内容类型”:“应用程序/json”
}
}
fetch(strings.baseUri+“按名称搜索”,fetchConfig)
。然后((响应)=>{
//使用*return*语句将json对象返回给part
返回JSON.parse(响应)
})
.然后((responseJson)=>{
让jsonObj=responseJson;//responseJson*已经*是一个JSON对象。
//您不能警告JSON数据,它将显示为[object]
//您可以执行console.log()或

//console.warn()@Pretasoc我已经编辑了我的问题。请检查一下。另外,你能不能从我的问题中删除“不像”,这样人们就可以回答我的问题。提前谢谢。)我没有否决你的问题,因此我无法删除该否决票。@Pretasoc哦,对不起。另外,如果可能的话,请你回答我的问题。如果我通过
alert(this.state.data)提醒我的数据
。我在警报框中看不到任何内容。我现在该怎么办?这真的取决于调用this.state.data的位置。设置数据需要时间。此外,
alert
采用字符串值,this.state.data是一个数组,因此您应该使用
JSON.stringify
使其工作。如果您想在
中调用它,那么((ejson)=>…
call block然后您应该这样做:
this.setState({data:responseJson},()=>alert(JSON.stringify(this.state.data));
这将在setState完成后调用警报。您还可以控制台.log(this.state.data);在你的渲染函数中,以便你看到它何时更新。请在setState上签出这篇文章。这篇文章是关于setState如何接受回调的。谢谢你。我刚刚在平面列表中使用了我的数据,现在我可以看到了。我刚刚更新了我的问题。如果你不介意,你也可以回答这个问题吗我刚刚更新了我的问题。如果你不介意的话,你也可以回答这个问题。:)试试
onChangeText={name=>{this.setState({name},()=>{this.search();});}
记住在这里添加“return”
。然后((response)=>{return response.json})
另外,你试过用“GET”代替“POST”吗?
<TextInput style={styles.searchInput} 
 placeholder="Search"
 placeholderTextColor={colors.whiteColor}
 onChangeText={ name => { 
   if (name.length > 0) {
    this.search(name); 
   } else {
    this.setState({data: [], name: ''});
   }
  }
 }
/>
const fetchConfig = {
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    }
}
fetch(strings.baseUri+"search_by_name",fetchConfig)
.then((response) => {
    //use *return* statement to return a json object to THEN part
    return JSON.parse(response)
})
.then((responseJson) => {
    let jsonObj = responseJson;//responseJson is *already* a JSON object.
    //you cannot alert a JSON data, it's will show as [object]
    //you can do console.log() or 
    //console.warn() <--- this will showed on device in yellowbox, but i'm not sure about a json data.
})