oratis
oratis

Reputation: 828

android thread not running

I am trying to insert some data into the database if the app is running for its first time, so I create a progressbar and a thread to do that ,but the thread seems not running and the progressbar just keep showing, can anyone explain why, thx

my code:

public class HDtvs extends Activity implements Runnable {
    /** Called when the activity is first created. */

    private Button likebutton;
    private ImageButton about;
    private ChannelDB mDB;
    private ListView channellist;
    private Cursor c;
    private ProgressDialog d;
    private String channelS_TABLE;

    @Override    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        likebutton=(Button) findViewById(R.id.share);
        about =(ImageButton)findViewById(R.id.about);
        channellist = (ListView) findViewById(R.id.Channel);

        if (tabIsExist(channelS_TABLE) != true){
        d=new ProgressDialog(HDtvs.this);
        d.setMessage("由于您第一次使用,本程序正在创建数据库,请稍后···");
        d.show();
        /* 启动另一个Thread,运行run() */
        Thread thread = new Thread(HDtvs.this);
        thread.start();
        }else{
            filldata();
            Log.d(this.toString(),"database exist");
        }

    }


    public void filldata(){
        mDB = new ChannelDB(this);

        String[] columns = {mDB.KEY_ID, mDB.KEY_POSTER, mDB.KEY_CHANNEL, mDB.KEY_PATH, mDB.KEY_DBLINK};
        String   table   = mDB.channelS_TABLE;

        c = mDB.getHandle().query(table, columns, null, null, null, null, null);

        startManagingCursor(c);

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.channelview,
                c,
                new String[] {mDB.KEY_POSTER, mDB.KEY_CHANNEL, mDB.KEY_DBLINK},
                new int[] {R.id.poster, R.id.channel, R.id.douban});

        adapter.setViewBinder(new ChannelViewBinder());

        channellist.setAdapter(adapter);

        channellist.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                c.moveToPosition(position);
                Intent intent = new Intent();
                intent.setClass(HDtvs.this,Showlist.class);
                Bundle bunde = new Bundle();
                bunde.putString("path",mDB.KEY_PATH);
                bunde.putString("cname",mDB.KEY_CHANNEL);
                bunde.putString("dblink",mDB.KEY_DBLINK);
                /* 将Bundle对象assign给Intent */
                intent.putExtras(bunde);

                startActivity(intent);
            }
        }); 
    }
    private Handler handler = new Handler()
    {
      @Override 
      public void handleMessage(Message msg)
      { 
        d.dismiss();

      }
    };

    @Override
    public void run()
    {
      try
      {
          mDB.Reset();

          insert some data

          handler.sendEmptyMessage(0);          
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }

 } 

Upvotes: 0

Views: 543

Answers (1)

Thommy
Thommy

Reputation: 5407

it is easier to use the AsyncTask for that: Create a new inner class which extends from AsyncTask:

  private class MyTask extends AsyncTask<String, String, String> {...

In the AsyncTask-Method doInBackground you can put your Database code.

Simply start the AsyncTask with new MyTask().execute(); from e.g. onCreate. If you want to have a ProgressBar you need to take a look at the onPreExecute, onPostExecute and onProgressUpdate Methods.

Upvotes: 2

Related Questions