将数据库处理程序对象的值传递给onClickListener

时间:2012-03-06 19:37:31

标签: android

我需要帮助将数据库助手对象的值传递给onCilckListener ..

如果数据库助手在侦听器之外,它可以正常工作。但是当应用程序在侦听器中使用时,应用程序会因异常而停止..请帮助我..

package com.example.helloandroid;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        DatabaseHandler dbpin = new DatabaseHandler(this);


        // Inserting Pin
        Log.d("Insert: ", "Inserting Pin ..");
        dbpin.addPin(new Pin("1111"));
        Log.d("Reading: ", "Reading pin at id=0..");

        Pin cn1 = dbpin.getPin(0);
        String log1 = cn1.getPin() ;
          Log.d("Name: ", log1);     //this works.. i want the value of log1 inside OnClickListener of imageButton1

        ImageButton next2 = (ImageButton) findViewById(R.id.imageButton1);
        next2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

            EditText pinget = (EditText)findViewById(R.id.editText1);


                 Log.v("EditText", pinget.getText().toString());



             //pin validator

          //value of log1 should be retrieved here to compare it in the if condition

                      if (pinget.getText().toString().equals("0000")) {
                Intent myIntent = new Intent(view.getContext(), home.class);
                startActivityForResult(myIntent, 1);
                }
                 else {
                     Intent myIntent = new Intent(view.getContext(), Main1.class);
                     startActivityForResult(myIntent, 1);
                 }

            }                

        });      
    }      
      }

2 个答案:

答案 0 :(得分:0)

如果你改变了

String log1 = cn1.getPin() ;

final String log1 = cn1.getPin();

您可以访问onClickListener中的log1

答案 1 :(得分:0)

替换

String log1 = cn1.getPin(); 

到这个

final String log1 = cn1.getPin();