title: ListView Refresh with web service Response in android
body: <p></p>
  
  <p><br /><strong>I have problem to set data into listview. Problem occurs after set message, message is set first after come 8th message from thread.</strong></p>
  
  <p><br />**below is my whole code :**<br /><br />**(Here is my "Activity and adapter")**</p>
  
  <pre><code>public class Inboxreadmsg extends ActionBarActivity {
    // &lt;strong&gt;Here is my global variable&lt;/strong&gt;
  
    ListView lv;
  
    Handler h;
  
    Custom_Inbox_Adapter ccAdpt;
  
    Custom_Inbox_Adapter inadapter;
    Runnable checker;
    List&lt;Dataset&gt; dataset;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.messagesublistlayout);
  
      // map id from layout&lt;/strong&gt;
      lv =(ListView)findViewById(R.id.readmessagelist);
  
  
      /*****************************call thread of webservice with database****************************/
      runThread();
      startHandler();
      /*****************************call thread of webservice with database****************************/
  
  
    }
    public void runThread()
    {
      h=new Handler();
  
      checker=new Runnable()
      {
        @Override
        public void run() {
          // call webservice for fetch data
          forthread();
  
          h.postDelayed(checker,15000);    
        }
      };
    }
    public void forthread()
    {
      // call webservice to fetch data  
      new InboxReadChat(null, InboxReadChat.TotalMessagesOfSingleSenderUser.geturl( recipientid,InAppUserid), InboxReadChat.TYPE_GET, InboxReadChat.TYPE_RECEIVE_MESSAGE_INBOX, new ServiceHitListenerInboxChat() {
        @Override
        public void onSuccess(Object Result, int id) 
        {
          // After success of webservice response come this function     
          callFxnInSuccess(Result);
        }
  
        @Override
        public void onError(String Error, int id) 
        {
          // AfterError of webservice response come this function   
          // By this fxn set data from local database to listview
          DBvaluesSet();
        }
      });
    }
    private void callFxnInSuccess(Object Result) {
      dataset = new ArrayList&lt;Dataset&gt;();
  
      String message="",alldatetime="",time="",date="",type="";
      InboxDeliveredModel ibx=(InboxDeliveredModel) Result;
  
      if(ibx.getTotalMessagesOfSingleSenderUser().size()&gt;0)
      {    
        // here webservice response data will add on local database    
        dbObject.Open();
  
        for(int i=0;i&lt;ibx.getTotalMessagesOfSingleSenderUser().size();i++)
        {
          message =ibx.getTotalMessagesOfSingleSenderUser().get(i).getMessage();
          .....
  
          dbObject.InboxMessageAll(message,InAppUsermobile,time,recipientid,type,date);
        }
  
        dbObject.close();
        // After add data into database call below fxn to fetch data from database and set data in listview with adapter
        try 
        {
          DBvaluesSet();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
  
    }
    public void DBvaluesSet() {
  
      dataset = new ArrayList&lt;Dataset&gt;();
  
      try 
      {
        // By this code fetch data from local database       
        Cursor c;
  
        dbObject.Open();
  
        c=dbObject.getallmessages(recipientid);
  
        int countRow = c.getCount();
        int counter = 0;
  
        while(c.moveToNext())
        {
          msgboxitem = c.getString(0);
          // number = c.getString(1);
          timeitem = c.getString(2);
          typeitem = c.getString(4);
          datedbitem = c.getString(5);
  
          try {
            dataset.add(db.new Dataset(datedbitem, msgboxitem, timeitem, typeitem));
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        dbObject.close();
  
        // by below set data into listview into adapter 
        lv.setAdapter(ccAdpt=new Custom_Inbox_Adapter( getApplicationContext(),dataset,R.layout.row));
        ccAdpt.notifyDataSetChanged();
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }
  }
  </code></pre>
  
  <p>Here my adapter coding...</p>
  
  <pre><code>public class Custom_Inbox_Adapter extends BaseAdapter{
    private Context gContext;
    private List&lt;Dataset&gt; gData;
    private int rEsource;
  
    public Custom_Inbox_Adapter(Context cnt, List&lt;Dataset&gt; data ,int resource){
      this.gData = data;           
      this.gContext = cnt;
      this.rEsource = resource;
    }
    @Override
    public int getCount() {
      return gData.size();
    }
    @Override
    public Dataset getItem(int position) {
      return gData.get(position);
    }
    @Override
    public long getItemId(int position) {&gt;            return gData.get(position).hashCode();
    }
  
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      if(convertView == null){
  
        LayoutInflater inflater = (LayoutInflater) gContext.getSystemService(gContext.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(rEsource, null);
  
        TextView txtReceiveMsg = (TextView) convertView.findViewById(R.id.ReceiveMsg);
        TextView txtReceiveTime = (TextView) convertView.findViewById(R.id.ReceiveTime);
        TextView txtSendMsg = (TextView) convertView.findViewById(R.id.sendMsg);
        TextView txtSendTime = (TextView) convertView.findViewById(R.id.senttime);            
        TextView date = (TextView) convertView.findViewById(R.id.date);
  
        // layout text chat
        RelativeLayout relSend = (RelativeLayout) convertView.findViewById(R.id.LinearReceive);
        RelativeLayout relreceive = (RelativeLayout) convertView.findViewById(R.id.LinearSend);
  
        // layout date chat
        RelativeLayout LinearDATE= (RelativeLayout) convertView.findViewById(R.id.LinearDATE);
  
        if(position == 0){
          fetchdata= gData.get(position).getDate().trim();
  
          date.setText(fetchdata);
        }
        else{
          fetchdata = gData.get(position).getDate().trim();
          dd = gData.get((position-1)).getDate().trim();
  
          if(fetchdata.equalsIgnoreCase(dd))
          {
            LinearDATE.setVisibility(View.GONE);
  
          }
          else
          {
            LinearDATE.setVisibility(View.VISIBLE);
            Log.w("INBOX READ", "INBOX_READ_ADAPTER::::(date for position '1'):"+fetchdata);
            date.setText(fetchdata);
          }
        }
  
        relreceive.setVisibility(View.GONE);
        relSend.setVisibility(View.VISIBLE);        
        //txtReceiveNumber.setText(number);
        txtReceiveMsg.setText(cutmsg);
        txtReceiveTime.setText(time);    
      }
      return convertView;
    }
  }
  </code></pre>