android线程没有运行

时间:2011-12-01 09:43:56

标签: android

如果应用程序第一次运行,我试图将一些数据插入到数据库中,所以我创建了一个进度条和一个线程来执行此操作,但线程似乎没有运行且进度条只是一直显示,任何人都可以解释原因,thx

我的代码:

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

 } 

1 个答案:

答案 0 :(得分:2)

使用AsyncTask更容易: 创建一个从AsyncTask扩展的新内部类:

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

在AsyncTask-Method doInBackground中,您可以放置​​数据库代码。

只需使用new MyTask().execute();启动AsyncTask即可。的onCreate。 如果您想要一个ProgressBar,您需要查看onPreExecute, onPostExecute and onProgressUpdate方法。