Skip to content Skip to sidebar Skip to footer

Refreshing Screen Which Uses Json In Android

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 re

Solution 1:

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

Post a Comment for "Refreshing Screen Which Uses Json In Android"