[英]Load ListView with JSON data from URL
I have following scenario: 我有以下情况:
I found this article: Android AsyncTask ListView - JSON|Surviving w/ Android which is good, but not excatly what I want. 我找到了这篇文章: Android AsyncTask ListView-JSON |通过Android生存 ,这很好,但并不是我想要的。 I do not want to execute the AsyncTask each time the user comes back to the MainActivity. 我不想每次用户回到MainActivity时都执行AsyncTask。
I thought of storing the ListView items in a globally available parameter of the Application. 我想到了将ListView项目存储在Application的全局可用参数中。 This parameter would then be some sort of custom container which holds the ListItems and the timestamp at which they expire. 然后,此参数将是某种自定义容器,其中包含ListItems及其到期的时间戳。 Then it would be possible to check in the "doInBackground" method of the AsyncTask if the ListView items are valid. 然后可以检查AsyncTask的“ doInBackground”方法是否有效。 If yes they are returned, if not they are loaded from URL and saved to the global variables. 如果是,则返回它们;如果不是,则从URL加载它们并保存到全局变量。
My question: Does this solution make sense? 我的问题:这种解决方案有意义吗? How would you solve this? 您将如何解决? Any ideas on making this better? 有什么更好的办法吗?
Well, yours make sense; 好吧,你有道理; however, I'd like to add something. 但是,我想添加一些内容。
For coding part, 对于编码部分,
LruCache
that should be defined in your custom Application
class. 您可能需要使用应该在自定义Application
类中定义的LruCache
。 Each key/value pair store the data model after parsing JSON. 每个键/值对在解析JSON后都会存储数据模型。 For example, cache.put(model.id, model)
. 例如, cache.put(model.id, model)
。 For UX part, 对于UX部分,
You don't need to update JSON every time, but provide an action trigger to do it. 您不需要每次都更新JSON,但是需要提供一个动作触发器来执行。 Some common ways is: 一些常见的方法是:
ActionBar
...) (可以是ActionBar
上的一个项目...) ListView
to refresh content. 用户下拉列表ListView
以刷新内容。 Since the ListView data is already serialized as JSON when you retrieve it, I think your best best is to persist the JSON. 由于ListView数据在检索时已经被序列化为JSON,因此我认为最好的办法是保留JSON。 Android handles persisting strings in SharedPreferences . Android处理SharedPreferences中的持久字符串。 You could also use SharedPreferences to persist the keepalive timestamp, allowing you to determine when you should refresh the data. 您还可以使用SharedPreferences来保存保持活动的时间戳,从而确定何时刷新数据。
This will allow you to populate the ListView between application loads as well as between activities without querying the server. 这将允许您在应用程序加载之间以及活动之间填充ListView,而无需查询服务器。
var api={ getapicallForCategories(){ var url='your url'; return fetch(url).then((res)=>res.json()); }, }; module.exports=api;
componentDidMount() { api.getapicallForCategories().then((res) => { this.setState({ listdata: res, }) }); } //inside render for (var m = 0; m < this.state.listdata.length; m++) { this.state.listdataCategory[m] = this.state.listdata[m].Category; this.state.listdataPrice[m] = this.state.listdata[m].Amt; const details = { name: this.state.listdataCategory[m], price: this.state.listdataPrice[m], } list.push(details); } // inside return { list.map((l, i) => ( <ListItem key={i} title={l.name} subtitle={l.price} rightIcon={<Icon name={'add-circle'} style={{ color: '#006400', fontSize: 40 }} onPress={() => this.onIncrease(l.price, i)} />} avatar={<Icon name={'remove-circle'} style={{ color: '#FF0000', fontSize: 40 }} onPress={() => this.onRemove(l.price, i)} />} /> )) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.