在获取静态文件的视图之前,新的Thread在android中启动

时间:2013-05-02 07:28:59

标签: android performance android-layout

import android.widget.SimpleAdapter;

@SuppressLint("NewApi")
public class StockListing extends Activity 
{
    int flag=0;
    protected static final String NULL = null;
    public ListView lst_stockListing;
    Button btn_leftPanel, btn_addstock;
    LinearLayout layoutMain;
    SimpleAdapter adapter;
    mItems planet;
    PeriodicChecker p;
    String msg=null,user=null;
    SelectViewHolder viewHolder;
    View view_check;
    float val = 0;
    boolean dataNotFound;
    ProgressDialog progressDialog,progressDialog1;
    ArrayList<Boolean> b=new ArrayList<Boolean>();
    AlertDialog diaBox;
    private mItems[] itemss;
    private ArrayAdapter<mItems> listAdapter;
    private ArrayAdapter<com.indusa.stockapp.stockListing.Read_quotes.mItems> listAdapter1;
    ArrayList<String> checked = new ArrayList<String>();
    ArrayList<String> lstcompanyname=new ArrayList<String>();
    List<HashMap<String, String>> fillMaps ;
    ArrayList<String> lstprice=new ArrayList<String>();
    ArrayList<mItems> planetList = new ArrayList<mItems>();
    String[] from = new String[] {"companyName","currentPrice", "checkBox"};
    int[] to = new int[] { R.id.txtcompanyname, R.id.txtprice, R.id.CheckBox};

    DownloadFileviaHTTP d;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stock_listting);
        btn_leftPanel = (Button) findViewById(R.id.btn_leftpanel);
        btn_addstock = (Button) findViewById(R.id.btn_addstock);
        lst_stockListing = (ListView) findViewById(R.id.lst_stocklisting);
        lst_stockListing.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        itemss = (mItems[]) getLastNonConfigurationInstance();

        user=getIntent().getExtras().getString("USERNAME");
        d=new DownloadFileviaHTTP();
        dataNotFound = false;

        InputStream is = getResources().openRawResource(R.raw.quotes);
        Log.d("inputstream ",is.toString());
        planetList=d.downloadFileViaHTTP(is,0);

        listAdapter = new com.indusa.stockapp.stockListing.DownloadFileviaHTTP.SelectArralAdapter(this,planetList);
        lst_stockListing.setAdapter(listAdapter);

        p=new PeriodicChecker();
        p.start();

        function();
    }

    public class PeriodicChecker extends Thread
    {
        @Override
        public void run()
        {
            while(true) 
            {
               try 
               {
                   final Handler refresh = new Handler(Looper.getMainLooper());
                refresh.post(new Runnable() 
                {
                    public void run()
                    {
                            try
                            {
                                Log.d("StockListing","StockListing");
                                Log.d("thread","thread");
                                if (isNetworkAvailable())
                                {

                                    planetList.clear();
                                    lstcompanyname.clear();
                                    try
                                    {
                                        Log.d("called","called");
                                        planetList=d.downloadFileViaHTTP(null,1);
                                    }
                                    catch (Exception e)
                                    {
                                        Log.d("exception thread","Exception Thread");
                                        InputStream is = getResources().openRawResource(R.raw.quotes);
                                        Log.d("inputstream ",is.toString());
                                        planetList=d.downloadFileViaHTTP(is,0);
                                        e.printStackTrace();
                                    }
                                    Log.d("outside","outside");
                                    Thread.sleep(500);
                                    progressDialog.dismiss();
                                }
                                else
                                {
                                    InputStream is = getResources().openRawResource(R.raw.quotes);
                                    Log.d("inputstream ",is.toString());
                                    planetList=d.downloadFileViaHTTP(is,0);
                                }
                                listAdapter = new com.indusa.stockapp.stockListing.DownloadFileviaHTTP.SelectArralAdapter(StockListing.this,planetList);
                                lst_stockListing.setAdapter(listAdapter);
                            }
                            catch (Exception e) 
                            {
                                e.printStackTrace();
                            }
                    }
                });
                Thread.sleep(50000);
                } 
               catch (InterruptedException e) 
               {
                   e.printStackTrace();
               }
            }
        }
    }
    private boolean isNetworkAvailable() 
    {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo) 
        {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected())
                    haveConnectedWifi = true;
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected())
                    haveConnectedMobile = true;
        }
        return haveConnectedWifi || haveConnectedMobile;
    }

        void function() 
    {

        btn_leftPanel.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v) 
            {
                if (p!=null)
                    p=null;

                Intent in = new Intent(StockListing.this, ListingItem.class);
                in.putExtra("USERNAME", user);
                startActivity(in);
            }
        });

        lst_stockListing.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView<?> arg0, View item, int position,long arg3)
            {   
                planet = listAdapter.getItem(position);
                planet.toggleChecked();
                viewHolder = (SelectViewHolder) item.getTag();
                viewHolder.getCheckBox().setChecked(planet.isChecked());                

                if (lstcompanyname.size()==0)
                {
                    lstcompanyname.add(viewHolder.getTextView().getText().toString());
                    lstprice.add(viewHolder.getPrice().getText().toString());
                    Log.d("added",viewHolder.getTextView().getText().toString());
                }
                else
                {
                    for (int i=0;i<lstcompanyname.size();i++);
                    {
                        if (lstcompanyname.contains(viewHolder.getTextView().getText().toString())==true)
                        {
                            lstcompanyname.remove(viewHolder.getTextView().getText().toString());
                            lstprice.remove(viewHolder.getPrice().getText().toString());
                            Log.d("removed",viewHolder.getTextView().getText().toString());
                        }
                        else
                        {
                            lstcompanyname.add(viewHolder.getTextView().getText().toString());
                            lstprice.add(viewHolder.getPrice().getText().toString());
                            Log.d("added",viewHolder.getTextView().getText().toString());
                        }
                    }
                }
            }
        });
        btn_addstock.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {

                int flag=0;
                DatabaseHelper db = new DatabaseHelper(StockListing.this,"StockApp");
                db.getReadableDatabase();
                b=db.match_record_mystock("TBL_MYSTOCK",lstcompanyname,db);
                db.close();

                progressDialog1 = ProgressDialog.show(StockListing.this, "", "Adding Record...");
                for(int i=0;i<b.size();i++)
                { 
                    Log.d("availability"," " +b.get(i));
                    if (b.get(i)==false)
                    {
                        flag=1;
                        DatabaseHelper db1 = new DatabaseHelper(StockListing.this,"StockApp");
                        db1.getWritableDatabase();

                        ////SYMBOL,NAME,CURR_PRICE,PREV_PRICE,AVG_PRICE,TRADESIDE,QUANTITY,GAIN,HIGH,LOW,PREVIOUS_CLOSE};
                        db1.addRecord_mystock("TBL_MYSTOCK",NULL,lstcompanyname.get(i),lstprice.get(i),lstprice.get(i), "0"," 0","0", "0","0", "0", "0", db);
                        db1.close();
                    }
                }
                progressDialog1.dismiss();
                if (flag==0)
                {

                    diaBox = makeAndShowDialogBox();
                    diaBox.show();
                }
                if (flag==1)
                {
                    Intent in=new Intent(StockListing.this,StockListing.class);
                    in.putExtra("USERNAME",user);
                    startActivity(in);
                }

                SparseBooleanArray checked = lst_stockListing.getCheckedItemPositions();
                ArrayList<String> selectedItems = new ArrayList<String>();

                    for (int i = 0; i < checked.size(); i++) 
                    {
                        // Item position in adapter
                        final boolean ischecked = checked.valueAt(i);
                        if (ischecked)
                        {
                            try
                            {
                                mItems tag = (mItems) lst_stockListing.getItemAtPosition(checked.keyAt(i));
                                String selectedName=tag.getName();   

                                if (checked.valueAt(i)) 
                                {
                                    int position = checked.keyAt(i);            
                                }
                            } 
                            catch (Exception e) 
                            {
                                e.printStackTrace();
                            }
                        }
                    }                   
            }
        });
    }

    private AlertDialog makeAndShowDialogBox() 
    {
        AlertDialog myQuittingDialogBox = new AlertDialog.Builder(this)
                .setTitle("Select Stocks")
                .setMessage("Selected stocks already in script or no stocks selected")

                .setPositiveButton("OK", new DialogInterface.OnClickListener() 
                {
                    public void onClick(DialogInterface dialog, int whichButton) 
                    {
                        // whatever should be done when answering "YES" goes
                        // here
                        msg = "YES " + Integer.toString(whichButton);
                        Log.d("msg", msg);
                        // txtMsg.setText(msg);
                    }
                })// setPositiveButton
                .create();
        return myQuittingDialogBox;
    }
}

//注 //在StockListing文件中,我希望在线程//启动之前在UI上显示静态文件,但它不显示静态文件,并在显示//来自网络的另一个文件时延迟

0 个答案:

没有答案