简体   繁体   中英

onClickListener doesnt work in second activity

I got an activity that is called from main activity. In the activity, i got database connections established and i fill a spinner according to db data. When a user selects one of the spinner items and press the button, the activity finishes and returns a value to the main activity (activity on result). But the button in the activity doesnt work, so the activity never returns a value to main activity. Here is the code:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class mySpinnerClass extends Activity implements
        OnItemSelectedListener {

    private VeriTabani veritabani;
    private SQLiteDatabase db;

    Spinner spinner;

    Button btnAdd;

    String dataBaseName;
    int selectedValue;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinner);

        dataBaseName = "";
        selectedValue = -1;

        Intent i = getIntent();
        dataBaseName = i.getStringExtra("dataBaseName");

        veritabani = new VeriTabani(this);
        db = veritabani.getWritableDatabase();

        // Spinner element
        spinner = (Spinner) findViewById(R.id.mySpinner);

        // add button
        btnAdd = (Button) findViewById(R.id.addButton);

        // Spinner click listener
        spinner.setOnItemSelectedListener(this);

        // Loading spinner data from database
        loadSpinnerData();


        btnAdd.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {

                    if (dataBaseName == "phoneOrientation") {
                        Intent intent = getIntent();
                        intent.putExtra("phoneOrientation", selectedValue);
                        setResult(RESULT_OK, intent);
                        finish();

                    }
                    else if(dataBaseName== "phonePosition") {
                        Intent intent = getIntent();
                        intent.putExtra("phonePosition", selectedValue);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                    else if(dataBaseName =="Label"){
                        Intent intent=getIntent();
                        intent.putExtra("label",selectedValue);
                        setResult(RESULT_OK, intent);
                        finish();
                    }


            }
        });
    }

    private void loadSpinnerData() {

        // Spinner Drop down elements
        List<String> lables = getAllLabels();

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, lables);

        // Drop down layout style - list view with radio button
        dataAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinner.setAdapter(dataAdapter);
    }

    public List<String> getAllLabels() {
        List<String> labels = new ArrayList<String>();

        // Select All Query
        String selectQuery = "SELECT  * FROM " + dataBaseName;

        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                labels.add(cursor.getString(cursor.getColumnIndex("isim")));
            } while (cursor.moveToNext());
        }

        // closing connection

        // returning lables
        return labels;
    }

    public int returnID(String item) {
        int toBereturnedID;
        Cursor myCursor = db.rawQuery("SELECT _id from " +dataBaseName+ " WHERE isim="+"'"+item+"'", null);
        myCursor.moveToFirst();
        toBereturnedID = myCursor.getInt(0);
        return toBereturnedID;
    }

    public void onItemSelected(AdapterView<?> parent, View arg1, int position, long arg3) {
        // TODO Auto-generated method stub
        String label = parent.getItemAtPosition(position).toString();
        selectedValue = returnID(label);
    }

    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

}

Try creating a new Intent instead of getIntent().

Intent intent=new Intent();
intent.putExtra("label",selectedValue);
setResult(RESULT_OK, intent);
finish();

Hope it helps.

You are calling loadSpinnerData(); method in onCreate() of activity so dataBaseName is always going to be empty, so you want to have value of spinner at time click you need to call that with on onClick() method of button, which will give you correct dataBaseName and use dataBaseName.equals("phoneOrientation") instead of dataBaseName == "phoneOrientation"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM