它使用JSON在android系统屏幕清爽 [英] Refreshing screen which uses json in android
问题描述
我在我的应用程序的模块,它使用JSON。
将JSON被不断更新。
在这里,我想刷新屏幕作为新数据到达。
有没有什么方法呢?
我要刷新的屏幕作为新的数据在每5秒或东西(固定的时间量)。如何做到这一点到达或一次?
我只是用一个本地JSON文件来测试这一点。
我只需要code对提神。
这是我的code为JSON解析:
公共类MainActivity扩展活动
{
串myjsonstring; ArrayList的<数据>网络=新的ArrayList<数据>(); @覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
JsonParser();
//尝试解析JSON
尝试{
JSONObject的jsonObjMain =新的JSONObject(myjsonstring);
JSONArray jsonArray = jsonObjMain.getJSONArray(PGM); 的for(int i = 0; I< jsonArray.length();我++){ //从JSONArray创建的JSONObject
JSONObject的jsonObj = jsonArray.getJSONObject(I) 从单个的JSONObject //获取数据 数据数据=新的Data(jsonObj.getString(name)的,jsonObj.getString(观众)); web.add(数据);
} 最后customtest1适配器=新customtest1(MainActivity.this,R.layout.list_single,网页);
ListView控件列表=(ListView控件)findViewById(R.id.list);
list.setAdapter(适配器); list.setOnItemClickListener(新AdapterView.OnItemClickListener()
{ @覆盖
公共无效onItemClick(适配器视图<>母公司,观景,
INT位置,长的id){
Toast.makeText(MainActivity.this,测试.........,Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
} }); }赶上(JSONException E){
// TODO自动生成catch块
e.printStackTrace();
}
} 私人无效JsonParser()
{
//读取从资产文件夹文本文件
StringBuffer的SB =新的StringBuffer();
BR的BufferedReader = NULL;
尝试{
BR =新的BufferedReader(新的InputStreamReader(getAssets()。打开(
main.json)));
串温度;
而((TEMP = br.readLine())!= NULL)
sb.append(临时);
}赶上(IOException异常五){
e.printStackTrace();
} {最后
尝试{
br.close(); //停止阅读
}赶上(IOException异常五){
e.printStackTrace();
}
} myjsonstring = sb.toString(); }
}
执行此操作。
创建一个类
公共类数据
{
字符串名称=;
串观众=;公开数据(字符串N,弦乐五)
{
名称= N;
观众= V;
}
}
然后在 MainActivity
只创建一个的ArrayList
类型数据
的ArrayList<数据>网络=新的ArrayList<数据>();
,然后分析你的JSON像
//从字符串创建的JSONObject
JSONObject的jsonObjMain =新的JSONObject(myjsonstring); //从创建的JSONObject JSONArray
JSONArray jsonArray = jsonObjMain.getJSONArray(PGM); // JSONArray有四个的JSONObject
的for(int i = 0; I< jsonArray.length();我++){ //从JSONArray创建的JSONObject
JSONObject的jsonObj = jsonArray.getJSONObject(I) 从单个的JSONObject //获取数据 数据数据=新的Data(jsonObj.getString(name)的,jsonObj.getString(观众)); web.add(数据);
现在,而不是传递了两个数组列表,你定义适配器,仅通过单一的ArrayList即网络
customtest适配器=新customtest(MainActivity.this,R.layout.list_single,网页);
ListView控件列表=(ListView控件)findViewById(R.id.list);
list.setAdapter(适配器);
在 customtest
类,在 getview
当你将数据绑定,你会做
数据DT = web.get(位置);
字符串名称= dt.name;
字符串观众= dt.viewers;
,然后做你做之前。
和后这一切,现在ehnever要更新列表只需拨打
adapter.notifyDataSetChanged();
您 customtest
类现在会像
公共类customtest扩展ArrayAdapter<数据> {上下文语境;
INT layoutResourceId;
ArrayList的<数据>数据= NULL;公共customList(上下文的背景下,诠释layoutResourceId,ArrayList的<数据>数据){
超级(上下文,layoutResourceId,数据);
this.layoutResourceId = layoutResourceId;
this.context =背景;
this.data =数据;
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
查看排= convertView;
CustomHolder支架=无效; 如果(行== NULL)
{
LayoutInflater充气=((活动)上下文).getLayoutInflater();
行= inflater.inflate(layoutResourceId,父母,假); 持有人=新CustomHolder();
holder.txtName =(TextView中)row.findViewById(R.id.txtName); //这是你要设置名称的TextView的ID
holder.txtViewers =(TextView中)row.findViewById(R.id.txtViewers); //这是你要设置的观众的TextView的ID row.setTag(保持器);
}
其他
{
支架=(CustomHolder)row.getTag();
} 数据DT = data.get(postition);
holder.txtName.setText(dt.name);
holder.txtViewers.setText(dt.viewers); 返回行;
} 静态类CustomHolder
{
TextView的txtName的;
TextView的txtViewers;
}
}
/////////////////////////////////////////////// ///////////////////////////////////////////
新的Thread(){公共无效的run()
{
而(真)
{
视频下载(3000);
jsonParse(); //这是你的JSON解析方法
}
}}。开始();
I have a module in my application which uses json.
The json gets updated always.
Here i want to refresh the screen as new data arrives.
Is there any method for this ?
I want to refresh the screen as new data arrives or once in every 5 seconds or something(a fixed amount of time).How to do this ?
I simply used a local json file to test this.
I just need the code for refreshing.
This is my code for json parsing :
public class MainActivity extends Activity
{
String myjsonstring;
ArrayList<Data> web = new ArrayList<Data>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
JsonParser();
// Try to parse JSON
try {
JSONObject jsonObjMain = new JSONObject(myjsonstring);
JSONArray jsonArray = jsonObjMain.getJSONArray("pgm");
for (int i = 0; i < jsonArray.length(); i++) {
// Creating JSONObject from JSONArray
JSONObject jsonObj = jsonArray.getJSONObject(i);
// Getting data from individual JSONObject
Data data = new Data(jsonObj.getString("name") , jsonObj.getString("viewers"));
web.add(data);
}
final customtest1 adapter = new customtest1(MainActivity.this,R.layout.list_single,web);
ListView list = (ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "Test.........", Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
}
});
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void JsonParser()
{
// Reading text file from assets folder
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open(
"main.json")));
String temp;
while ((temp = br.readLine()) != null)
sb.append(temp);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close(); // stop reading
} catch (IOException e) {
e.printStackTrace();
}
}
myjsonstring = sb.toString();
}
}
Do this. create a class
public class Data
{
String name ="";
String viewers ="";
public Data(String n,String v)
{
name = n;
viewers=v;
}
}
then in MainActivity
create only single arraylist
with type Data
ArrayList<Data> web = new ArrayList<Data>();
and then parse your json like
// Creating JSONObject from String
JSONObject jsonObjMain = new JSONObject(myjsonstring);
// Creating JSONArray from JSONObject
JSONArray jsonArray = jsonObjMain.getJSONArray("pgm");
// JSONArray has four JSONObject
for (int i = 0; i < jsonArray.length(); i++) {
// Creating JSONObject from JSONArray
JSONObject jsonObj = jsonArray.getJSONObject(i);
// Getting data from individual JSONObject
Data data = new Data(jsonObj.getString("name") , jsonObj.getString("viewers"));
web.add(data);
now instead of passing two array list to your custom adapter , pass only single arraylist i.e web
customtest adapter = new customtest(MainActivity.this,R.layout.list_single,web);
ListView list = (ListView)findViewById(R.id.list);
list.setAdapter(adapter);
in your customtest
class , inside getview
when you will bind data, you will do
Data dt = web.get(position);
String name = dt.name;
String viewers = dt.viewers;
and then do what you were doing before.
and after all this, now ehnever you want to update your list simply call
adapter.notifyDataSetChanged();
your customtest
class now will be like
public class customtest extends ArrayAdapter<Data>{
Context context;
int layoutResourceId;
ArrayList<Data> data = null;
public customList(Context context, int layoutResourceId, ArrayList<Data> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
CustomHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new CustomHolder();
holder.txtName = (TextView)row.findViewById(R.id.txtName); //this is id of textview where you want to set name
holder.txtViewers = (TextView)row.findViewById(R.id.txtViewers); //this is id of textview where you want to set viewers
row.setTag(holder);
}
else
{
holder = (CustomHolder)row.getTag();
}
Data dt = data.get(postition);
holder.txtName.setText(dt.name);
holder.txtViewers.setText(dt.viewers);
return row;
}
static class CustomHolder
{
TextView txtName;
TextView txtViewers;
}
}
//////////////////////////////////////////////////////////////////////////////////////////
new Thread(){
public void run()
{
While(true)
{
Thread.sleep(3000);
jsonParse(); //this is your method for parsing json
}
}
}.start();
这篇关于它使用JSON在android系统屏幕清爽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!