Pergunta

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();

    }
    }
Foi útil?

Solução

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();
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top