试图在其他按钮上设置onClick()

时间:2014-04-25 06:25:49

标签: android sqlite android-intent android-asynctask

我是android开发的新手。首先,我使用AsyncTask通过按钮将数据保存在数据库中。而现在我试图在同一活动的其他按钮上调用意图,但是当我点击第二个按钮时没有任何反应。任何人都可以帮助我PLZ ..在这里我也发布了一些代码。

MainActivity.java

     //initialize all view objects
    et=(EditText)findViewById(R.id.editText1);
 AddBtn=(Button)findViewById(R.id.btn_add);
    spn = (Spinner) findViewById(R.id.spinner1);
    NextBtn=(Button) findViewById(R.id.button1);

SQLcon = new SQLController(this);
    // opening database
    SQLcon.open();

    loadtospinner();
}

    public void onClick(View v){

        if(v.getId() == R.id.btn_add){
          new MyAsync().execute();

            }
        else {
            Intent  in=new Intent(Intent, Second.class);
        startActivity(in);

        }



    }

    public void loadtospinner() {

        Cursor c = SQLcon.readData();
        ArrayList<String> al = new ArrayList<String>();

        c.moveToFirst();
        while (!c.isAfterLast()) {

            String name = c.getString(c.getColumnIndex(DBhelper.MEMBER_NAME));
            al.add(name);
            c.moveToNext();
        }

        ArrayAdapter<String> aa1 = new ArrayAdapter<String>(
                getApplicationContext(), R.layout.spinner_item, R.id.textView1,
                al);

        spn.setAdapter(aa1);

        // closing database
        SQLcon.close();

    }




    private class MyAsync extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {

            super.onPreExecute();
            PD = new ProgressDialog(MainActivity.this);
            PD.setTitle("Please Wait..");
            PD.setMessage("Loading...");
            PD.setCancelable(false);
            PD.show();
        }

        @Override
        protected Void doInBackground(Void... params) {

            String name = et.getText().toString();
            // opening database
            SQLcon.open();
            // insert data into table
            SQLcon.insertData(name);

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            loadtospinner();
            PD.dismiss();
        }



    }

}

3 个答案:

答案 0 :(得分:1)

改变下面的事情

public class FirstActivity extends Activity implements OnClickListener {

之后在创建

secondButton = (Button) findViewById(R.id.rightButton);
        secondButton.setOnClickListener(this);

覆盖以下方法

@Override
    public void onClick(View v) { // Parameter v stands for the view that was clicked.  

        // getId() returns this view's identifier.
        if(v.getId() == R.id.leftButton){
            // setText() sets the string value of the TextView
            changingTextView.setText("You clicked First");
        }else if(v.getId() == R.id.rightButton){
            changingTextView.setText("You clicked Second");
        }
    }

答案 1 :(得分:1)

活动中的第一个implements OnClickListener

setOnClickListener更像<{1}}

button

现在在 AddBtn=(Button)findViewById(R.id.btn_add); _AddBtn.setOnClickListener(this); NextBtn=(Button) findViewById(R.id.button1); _NextBtn.setOnClickListener(this);

onClick

答案 2 :(得分:0)

您正在传递Intent而不是活动,通过更改此

传递活动
Intent  in=new Intent(Intent, Second.class);
        startActivity(in);

并尝试

Intent  in=new Intent(getApplicationContext(), Second.class);
            startActivity(in);

希望这项工作