Android 如何将项目动态添加到listview中

Android 如何将项目动态添加到listview中,android,android-listview,baseadapter,Android,Android Listview,Baseadapter,我想使用异步任务将项添加到listview中,因此在DoinBackground中,它将逐个处理和获取数据,然后在listview上逐个显示。 但对于我的应用程序,doinbackground会处理所有数据,然后显示到listview中 public class NewGetContacts extends AsyncTask<String[], Void, Void> { private static final String TAG_TX = "txid";

我想使用异步任务将项添加到listview中,因此在DoinBackground中,它将逐个处理和获取数据,然后在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();

     }

  }
公共类NewGetContacts扩展异步任务{
专用静态最终字符串标记_TX=“txid”;
私有静态最终字符串标记_FEE=“fees”;
MyCustomAdapter mAdapter=新的MyCustomAdapter();
ListView listViewHandle1=(ListView)findViewById(R.id.listView2);
@凌驾
受保护的Void doInBackground(字符串[]…参数){
//TODO自动生成的方法存根
int len=params[0]。长度;
ServiceHandler sh=新的ServiceHandler();
//向url发出请求并获得响应
//字符串jsonStr;
mAdapter.addSeparatorItem(“事务…”);

对于(inti=0;i在您的活动/片段上调用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();

 }
受保护的Void doInBackground(字符串[]…参数){//与您的相同
//TODO自动生成的方法存根
int len=params[0]。长度;
ServiceHandler sh=新的ServiceHandler();
//向url发出请求并获得响应
//字符串jsonStr;
mAdapter.addSeparatorItem(“事务…”);

对于(int i=0;iHi@Aniruddh),谢谢。但在这种情况下,适配器和listview将不会被解析。如果要逐个添加数据,请调用mAdapter.notifyDataSetChanged()仅在进行中。还有一件事,例如,如果您的适配器中有100个项目,则适配器将仅绘制在当前视图端口中可见的前n个项目,在您滚动iti时将创建其他项目。我收到此错误“只有创建视图层次结构的原始线程才能接触其视图”@Rick,你的Asynctaskclass是在活动类中还是在活动类之外