Setting up async task for loading Json into a listview

前端 未结 5 1724
予麋鹿
予麋鹿 2020-12-09 23:22

I take json object generate from a PHP script on my server and then parse it into a listview using a lazy load for the images. The problem is that the json will load relativ

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-09 23:42

    If you want to use thread then this is simple solution

        public class ActivityClass extends Activity implements Runnable, OnItemClickListener {
        ProgressDialog pd;
        ListView list;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            list=(ListView) findViewById(R.id.listview);
            list.setOnItemClickListener(this);
            pd=new ProgressDialog(this);
            pd.setTitle("Please wait");
            pd.setMessage("Loading....");
            pd.setCancelable(false);pd.show();
            Thread th=new Thread(this);
            th.start();
        }
    
           AlertDialog dialog1;
           private ArrayList titleVal=new ArrayList();
           private ArrayList pubDateVal=new ArrayList();
           private ArrayList linkVal=new ArrayList();
    
            @Override
        public void run() {
            GetTheFout();
        }
        /**
         * Update If Needed
         */
        ArrayList count;
            AdapterClass adb;
        public void GetTheFout(){
            try{
            Do json parsing here and store values in arraylist,
            }
            hanlder.sendEmptyMessage(sendMsg);
        }
        private Handler hanlder=new Handler(){
    
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                    adb=new AdapterClass(BBNewsMain.this,titleVal,pubDateVal);
                list.setAdapter(adb);
            }
    
        };
    //  BroadCast broad;
        @Override
        public void onItemClick(AdapterView arg0, View arg1, int pos, long arg3) {
                   try{
               ///Handle click event here
                   }catch(ArrayIndexOutOfBoundsException e){
                       e.getMessage();
                   }
        }
    }
    

    Note Never Update UI inside thread.That's why i have taken one handler class.After executing thread it will come to handler then set all value to list adapter and dismiss Progress Dialog

提交回复
热议问题