0

I want to add item into listview using async task, so in doinbackgroud it will process and get the data one by one and then display it on listview one by one . But for my app doinbackground process all the data and then it will display into listview.

 public class NewGetContacts extends AsyncTask<String[], Void, Void> {



       private static final String TAG_TX = "txid";
       private static final String TAG_FEE = "fees";


      MyCustomAdapter  mAdapter=new MyCustomAdapter();

      ListView listViewHandle1 = (ListView) findViewById(R.id.listView2);



    @Override
    protected Void doInBackground(String[]... params) {
        // TODO Auto-generated method stub
        int len = params[0].length;
        ServiceHandler sh = new ServiceHandler();
        // Making a request to url and getting response
       // String jsonStr;
        mAdapter.addSeparatorItem("Transaction ...");
        for(int i=0;i<len ;i++){
            String turl = "https://coin/api/tx/"+params[0][i];
            try {

                  String jsonStr1 = sh.makeServiceCall(turl, ServiceHandler.GET);
                     JSONObject jsonObj2 = new JSONObject(jsonStr1);
                     txtid = jsonObj2.getString(TAG_TX);

                     mAdapter.addItem("Transaction ID : "+txtid);

                    publishProgress();

            }catch(Exception e){
                    Log.d("Exception In TXID -- >",e.getMessage());
                }
        }
        return null;
    }
    protected void onProgressUpdate(Void... r) {
        super.onProgressUpdate(r);
        Log.d("Txid 14546465 ","--->");

         mAdapter.notifyDataSetChanged();
         listViewHandle1.requestLayout();

         super.onProgressUpdate(r);

    }
     protected void onPostExecute(Void result) {
          super.onPostExecute(result);

                      listViewHandle1.setAdapter(mAdapter);
          mAdapter.notifyDataSetChanged();

     }

  }
Rick
  • 33
  • 7

1 Answers1

1

Call this in oncreate on your activity/fragment

Class TestActivity extends Activty {
MyCustomAdapter  mAdapter ;
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    mAdapter=new MyCustomAdapter();
    ListView listViewHandle1 = (ListView) findViewById(R.id.listView2);
    listViewHandle1.setAdapter(mAdapter);
    (new NewGetContacts()).execute();
}

}

Then do following in your AsyncTask class

protected Void doInBackground(String[]... params) { //Same as yours
    // TODO Auto-generated method stub
    int len = params[0].length;
    ServiceHandler sh = new ServiceHandler();
    // Making a request to url and getting response
   // String jsonStr;
    mAdapter.addSeparatorItem("Transaction ...");
    for(int i=0;i<len ;i++){
        String turl = "https://coin/api/tx/"+params[0][i];
        try {

              String jsonStr1 = sh.makeServiceCall(turl, ServiceHandler.GET);
                 JSONObject jsonObj2 = new JSONObject(jsonStr1);
                 txtid = jsonObj2.getString(TAG_TX);

                 mAdapter.addItem("Transaction ID : "+txtid);

                publishProgress();

        }catch(Exception e){
                Log.d("Exception In TXID -- >",e.getMessage());
            }
    }
    return null;
}
protected void onProgressUpdate(Void... r) { 
    super.onProgressUpdate(r);
    mAdapter.notifyDataSetChanged();
    Log.d("Txid 14546465 ","--->");

}


 protected void onPostExecute(Void result) {
      super.onPostExecute(result);
      //Removed set adapter from here     
      mAdapter.notifyDataSetChanged();

 }
  • Hi @Aniruddh thanks for it. But in this case the adapter and listview will not be resolved . – Rick Sep 10 '14 at 05:16
  • if you want to add data one by one then call mAdapter.notifyDataSetChanged() in onProgress only . – Aniruddh Rathore Sep 10 '14 at 05:29
  • One more thing for example if you are having 100 items in your adapter then adapter will draw first n items only which is visible in current view port , other items will be created once you scroll it – Aniruddh Rathore Sep 10 '14 at 05:31
  • i am getting this error "Only the original thread that created a view hierarchy can touch its views". – Rick Sep 10 '14 at 06:09
  • @Rick, have you made your Asynctaskclass in your activity class or outside the acitivity class – Aniruddh Rathore Sep 10 '14 at 06:12
  • @Rick, Please remove any UI update statement from DoinBackground. Might be you are calling adapter.notifydatasetchanged in Doinbackground – Aniruddh Rathore Sep 10 '14 at 06:15