如何动态地将项添加到listview中

时间:2014-09-09 11:59:47

标签: android android-listview baseadapter

我想使用异步任务将项目添加到listview中,因此在doinbackgroud中它将逐个处理并获取数据,然后逐个显示在listview上。 但是对于我的应用程序,doinbackground会处理所有数据,然后它将显示在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();

     }

  }

1 个答案:

答案 0 :(得分:1)

在你的活动/片段的oncreate中调用它

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

}

然后在AsyncTask类中执行以下操作

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

 }